Skip to content

Commit 9bdeec3

Browse files
schlndhondrejmirtes
authored andcommitted
bound TK of array_intersect_ukey etc to array-key
1 parent 416bdb3 commit 9bdeec3

12 files changed

+46
-43
lines changed

src/Type/TypehintHelper.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,11 @@ public static function decideType(
201201
}
202202

203203
if (
204-
(!$phpDocType instanceof NeverType || ($type instanceof MixedType && !$type->isExplicitMixed()))
205-
&& $type->isSuperTypeOf(TemplateTypeHelper::resolveToBounds($phpDocType))->yes()
204+
($type->isCallable()->yes() && $phpDocType->isCallable()->yes())
205+
|| (
206+
(!$phpDocType instanceof NeverType || ($type instanceof MixedType && !$type->isExplicitMixed()))
207+
&& $type->isSuperTypeOf(TemplateTypeHelper::resolveToBounds($phpDocType))->yes()
208+
)
206209
) {
207210
$resultType = $phpDocType;
208211
} else {

stubs/arrayFunctions.stub

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function array_udiff(
7777
function array_is_list(array $value): bool {}
7878

7979
/**
80-
* @template TK of mixed
80+
* @template TK of array-key
8181
* @template TV of mixed
8282
*
8383
* @param array<TK, TV> $one
@@ -92,7 +92,7 @@ function array_diff_uassoc(
9292
): array {}
9393

9494
/**
95-
* @template TK of mixed
95+
* @template TK of array-key
9696
* @template TV of mixed
9797
*
9898
* @param array<TK, TV> $one
@@ -107,7 +107,7 @@ function array_diff_ukey(
107107
): array {}
108108

109109
/**
110-
* @template TK of mixed
110+
* @template TK of array-key
111111
* @template TV of mixed
112112
*
113113
* @param array<TK, TV> $one
@@ -122,7 +122,7 @@ function array_intersect_uassoc(
122122
): array {}
123123

124124
/**
125-
* @template TK of mixed
125+
* @template TK of array-key
126126
* @template TV of mixed
127127
*
128128
* @param array<TK, TV> $one
@@ -138,7 +138,7 @@ function array_intersect_ukey(
138138
): array {}
139139

140140
/**
141-
* @template TK of mixed
141+
* @template TK of array-key
142142
* @template TV of mixed
143143
*
144144
* @param array<TK, TV> $one
@@ -154,7 +154,7 @@ function array_udiff_assoc(
154154
): array {}
155155

156156
/**
157-
* @template TK of mixed
157+
* @template TK of array-key
158158
* @template TV of mixed
159159
*
160160
* @param array<TK, TV> $one
@@ -171,7 +171,7 @@ function array_udiff_uassoc(
171171
): array {}
172172

173173
/**
174-
* @template TK of mixed
174+
* @template TK of array-key
175175
* @template TV of mixed
176176
*
177177
* @param array<TK, TV> $one
@@ -186,7 +186,7 @@ function array_uintersect_assoc(
186186
): array {}
187187

188188
/**
189-
* @template TK of mixed
189+
* @template TK of array-key
190190
* @template TV of mixed
191191
*
192192
* @param array<TK, TV> $one
@@ -203,7 +203,7 @@ function array_uintersect_uassoc(
203203
): array {}
204204

205205
/**
206-
* @template TK of mixed
206+
* @template TK of array-key
207207
* @template TV of mixed
208208
*
209209
* @param array<TK, TV> $one

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,11 +1844,11 @@ public function testArrayDiffUassoc(): void
18441844
$this->checkExplicitMixed = true;
18451845
$this->analyse([__DIR__ . '/data/array_diff_uassoc.php'], [
18461846
[
1847-
'Parameter #3 $data_comp_func of function array_diff_uassoc expects callable(string, string): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
1847+
'Parameter #3 $data_comp_func of function array_diff_uassoc expects callable(\'a\'|\'b\'|\'c\'|\'d\', \'a\'|\'b\'|\'c\'|\'d\'): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
18481848
22,
18491849
],
18501850
[
1851-
'Parameter #3 $data_comp_func of function array_diff_uassoc expects callable(int, int): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
1851+
'Parameter #3 $data_comp_func of function array_diff_uassoc expects callable(0|1|2|3, 0|1|2|3): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
18521852
30,
18531853
],
18541854
]);
@@ -1859,11 +1859,11 @@ public function testArrayDiffUkey(): void
18591859
$this->checkExplicitMixed = true;
18601860
$this->analyse([__DIR__ . '/data/array_diff_ukey.php'], [
18611861
[
1862-
'Parameter #3 $key_comp_func of function array_diff_ukey expects callable(string, string): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
1862+
'Parameter #3 $key_comp_func of function array_diff_ukey expects callable(\'a\'|\'b\'|\'c\'|\'d\', \'a\'|\'b\'|\'c\'|\'d\'): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
18631863
22,
18641864
],
18651865
[
1866-
'Parameter #3 $key_comp_func of function array_diff_ukey expects callable(int, int): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
1866+
'Parameter #3 $key_comp_func of function array_diff_ukey expects callable(0|1|2|3, 0|1|2|3): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
18671867
30,
18681868
],
18691869
]);
@@ -1874,11 +1874,11 @@ public function testArrayIntersectUassoc(): void
18741874
$this->checkExplicitMixed = true;
18751875
$this->analyse([__DIR__ . '/data/array_intersect_uassoc.php'], [
18761876
[
1877-
'Parameter #3 $key_compare_func of function array_intersect_uassoc expects callable(string, string): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
1877+
'Parameter #3 $key_compare_func of function array_intersect_uassoc expects callable(\'a\'|\'b\'|\'c\'|\'d\', \'a\'|\'b\'|\'c\'|\'d\'): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
18781878
22,
18791879
],
18801880
[
1881-
'Parameter #3 $key_compare_func of function array_intersect_uassoc expects callable(int, int): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
1881+
'Parameter #3 $key_compare_func of function array_intersect_uassoc expects callable(0|1|2|3, 0|1|2|3): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
18821882
30,
18831883
],
18841884
]);
@@ -1889,11 +1889,11 @@ public function testArrayIntersectUkey(): void
18891889
$this->checkExplicitMixed = true;
18901890
$this->analyse([__DIR__ . '/data/array_intersect_ukey.php'], [
18911891
[
1892-
'Parameter #3 $key_compare_func of function array_intersect_ukey expects callable(string, string): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
1892+
'Parameter #3 $key_compare_func of function array_intersect_ukey expects callable(\'a\'|\'b\'|\'c\'|\'d\', \'a\'|\'b\'|\'c\'|\'d\'): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
18931893
22,
18941894
],
18951895
[
1896-
'Parameter #3 $key_compare_func of function array_intersect_ukey expects callable(int, int): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
1896+
'Parameter #3 $key_compare_func of function array_intersect_ukey expects callable(0|1|2|3, 0|1|2|3): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
18971897
30,
18981898
],
18991899
]);
@@ -1904,34 +1904,34 @@ public function testArrayUdiffAssoc(): void
19041904
$this->checkExplicitMixed = true;
19051905
$this->analyse([__DIR__ . '/data/array_udiff_assoc.php'], [
19061906
[
1907-
'Parameter #3 $key_comp_func of function array_udiff_assoc expects callable(int, int): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
1907+
'Parameter #3 $key_comp_func of function array_udiff_assoc expects callable(1|2, 1|2): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
19081908
22,
19091909
],
19101910
[
1911-
'Parameter #3 $key_comp_func of function array_udiff_assoc expects callable(int, int): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
1911+
'Parameter #3 $key_comp_func of function array_udiff_assoc expects callable(1|2|3|4|5, 1|2|3|4|5): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
19121912
30,
19131913
],
19141914
]);
19151915
}
19161916

1917-
public function testArrayUdiffUsssoc(): void
1917+
public function testArrayUdiffUasssoc(): void
19181918
{
19191919
$this->checkExplicitMixed = true;
19201920
$this->analyse([__DIR__ . '/data/array_udiff_uassoc.php'], [
19211921
[
1922-
'Parameter #3 $data_comp_func of function array_udiff_uassoc expects callable(string, string): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
1922+
'Parameter #3 $data_comp_func of function array_udiff_uassoc expects callable(\'a\'|\'b\'|\'c\'|\'d\', \'a\'|\'b\'|\'c\'|\'d\'): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
19231923
28,
19241924
],
19251925
[
1926-
'Parameter #4 $key_comp_func of function array_udiff_uassoc expects callable(string, string): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
1926+
'Parameter #4 $key_comp_func of function array_udiff_uassoc expects callable(\'a\'|\'b\'|\'c\'|\'d\', \'a\'|\'b\'|\'c\'|\'d\'): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
19271927
31,
19281928
],
19291929
[
1930-
'Parameter #3 $data_comp_func of function array_udiff_uassoc expects callable(int, int): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
1930+
'Parameter #3 $data_comp_func of function array_udiff_uassoc expects callable(1|2|3|4|5, 1|2|3|4|5): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
19311931
39,
19321932
],
19331933
[
1934-
'Parameter #4 $key_comp_func of function array_udiff_uassoc expects callable(int, int): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
1934+
'Parameter #4 $key_comp_func of function array_udiff_uassoc expects callable(0|1|2|3, 0|1|2|3): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
19351935
42,
19361936
],
19371937
]);
@@ -1942,11 +1942,11 @@ public function testArrayUintersectAssoc(): void
19421942
$this->checkExplicitMixed = true;
19431943
$this->analyse([__DIR__ . '/data/array_uintersect_assoc.php'], [
19441944
[
1945-
'Parameter #3 $data_compare_func of function array_uintersect_assoc expects callable(string, string): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
1945+
'Parameter #3 $data_compare_func of function array_uintersect_assoc expects callable(\'a\'|\'b\'|\'c\'|\'d\', \'a\'|\'b\'|\'c\'|\'d\'): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
19461946
22,
19471947
],
19481948
[
1949-
'Parameter #3 $data_compare_func of function array_uintersect_assoc expects callable(int, int): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
1949+
'Parameter #3 $data_compare_func of function array_uintersect_assoc expects callable(1|2|3|4, 1|2|3|4): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
19501950
30,
19511951
],
19521952
]);
@@ -1957,19 +1957,19 @@ public function testArrayUintersectUassoc(): void
19571957
$this->checkExplicitMixed = true;
19581958
$this->analyse([__DIR__ . '/data/array_uintersect_uassoc.php'], [
19591959
[
1960-
'Parameter #3 $data_compare_func of function array_uintersect_uassoc expects callable(string, string): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
1960+
'Parameter #3 $data_compare_func of function array_uintersect_uassoc expects callable(\'a\'|\'b\'|\'c\'|\'d\', \'a\'|\'b\'|\'c\'|\'d\'): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
19611961
28,
19621962
],
19631963
[
1964-
'Parameter #4 $key_compare_func of function array_uintersect_uassoc expects callable(string, string): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
1964+
'Parameter #4 $key_compare_func of function array_uintersect_uassoc expects callable(\'a\'|\'b\'|\'c\'|\'d\', \'a\'|\'b\'|\'c\'|\'d\'): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
19651965
31,
19661966
],
19671967
[
1968-
'Parameter #3 $data_compare_func of function array_uintersect_uassoc expects callable(int, int): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
1968+
'Parameter #3 $data_compare_func of function array_uintersect_uassoc expects callable(1|2|3|4|5, 1|2|3|4|5): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
19691969
39,
19701970
],
19711971
[
1972-
'Parameter #4 $key_compare_func of function array_uintersect_uassoc expects callable(int, int): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
1972+
'Parameter #4 $key_compare_func of function array_uintersect_uassoc expects callable(0|1|2|3, 0|1|2|3): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
19731973
42,
19741974
],
19751975
]);
@@ -1980,11 +1980,11 @@ public function testArrayUintersect(): void
19801980
$this->checkExplicitMixed = true;
19811981
$this->analyse([__DIR__ . '/data/array_uintersect.php'], [
19821982
[
1983-
'Parameter #3 $data_compare_func of function array_uintersect expects callable(string, string): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
1983+
'Parameter #3 $data_compare_func of function array_uintersect expects callable(\'a\'|\'b\'|\'c\'|\'d\', \'a\'|\'b\'|\'c\'|\'d\'): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
19841984
22,
19851985
],
19861986
[
1987-
'Parameter #3 $data_compare_func of function array_uintersect expects callable(int, int): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
1987+
'Parameter #3 $data_compare_func of function array_uintersect expects callable(1|2|3|4, 1|2|3|4): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
19881988
30,
19891989
],
19901990
]);

tests/PHPStan/Rules/Functions/data/array_diff_uassoc.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php namespace ParamsArrayDiffUassoc;
22

33
array_diff_uassoc(
44
['a' => 1, 'b' => 2],

tests/PHPStan/Rules/Functions/data/array_diff_ukey.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php namespace ParamsArrayDiffUkey;
22

33
array_diff_ukey(
44
['a' => 1, 'b' => 2],

tests/PHPStan/Rules/Functions/data/array_intersect_uassoc.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php namespace ParamsArrayIntersectUassoc;
22

33
array_intersect_uassoc(
44
['a' => 1, 'b' => 2],

tests/PHPStan/Rules/Functions/data/array_intersect_ukey.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php namespace ParamsArrayIntersectUkey;
22

33
array_intersect_ukey(
44
['a' => 1, 'b' => 2],

tests/PHPStan/Rules/Functions/data/array_udiff_assoc.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php namespace ParamsArrayUdiffAssoc;
22

33
array_udiff_assoc(
44
['a' => 1, 'b' => 2],

tests/PHPStan/Rules/Functions/data/array_udiff_uassoc.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php namespace ParamsArrayUDiffUassoc;
22

33
array_udiff_uassoc(
44
['a' => 'a', 'b' => 'b'],

tests/PHPStan/Rules/Functions/data/array_uintersect.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php namespace ParamsArrayUintersect;
22

33
array_uintersect(
44
['a', 'b'],

0 commit comments

Comments
 (0)