Skip to content

Commit 1ff2ba3

Browse files
committed
bound TK of array_intersect_ukey etc to array-key
1 parent 9b634cf commit 1ff2ba3

12 files changed

+46
-43
lines changed

Diff for: src/Type/TypehintHelper.php

+5-2
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 {

Diff for: stubs/arrayFunctions.stub

+9-9
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function array_udiff(
6969
function array_is_list(array $value): bool {}
7070

7171
/**
72-
* @template TK of mixed
72+
* @template TK of array-key
7373
* @template TV of mixed
7474
*
7575
* @param array<TK, TV> $one
@@ -84,7 +84,7 @@ function array_diff_uassoc(
8484
): array {}
8585

8686
/**
87-
* @template TK of mixed
87+
* @template TK of array-key
8888
* @template TV of mixed
8989
*
9090
* @param array<TK, TV> $one
@@ -99,7 +99,7 @@ function array_diff_ukey(
9999
): array {}
100100

101101
/**
102-
* @template TK of mixed
102+
* @template TK of array-key
103103
* @template TV of mixed
104104
*
105105
* @param array<TK, TV> $one
@@ -114,7 +114,7 @@ function array_intersect_uassoc(
114114
): array {}
115115

116116
/**
117-
* @template TK of mixed
117+
* @template TK of array-key
118118
* @template TV of mixed
119119
*
120120
* @param array<TK, TV> $one
@@ -130,7 +130,7 @@ function array_intersect_ukey(
130130
): array {}
131131

132132
/**
133-
* @template TK of mixed
133+
* @template TK of array-key
134134
* @template TV of mixed
135135
*
136136
* @param array<TK, TV> $one
@@ -146,7 +146,7 @@ function array_udiff_assoc(
146146
): array {}
147147

148148
/**
149-
* @template TK of mixed
149+
* @template TK of array-key
150150
* @template TV of mixed
151151
*
152152
* @param array<TK, TV> $one
@@ -163,7 +163,7 @@ function array_udiff_uassoc(
163163
): array {}
164164

165165
/**
166-
* @template TK of mixed
166+
* @template TK of array-key
167167
* @template TV of mixed
168168
*
169169
* @param array<TK, TV> $one
@@ -178,7 +178,7 @@ function array_uintersect_assoc(
178178
): array {}
179179

180180
/**
181-
* @template TK of mixed
181+
* @template TK of array-key
182182
* @template TV of mixed
183183
*
184184
* @param array<TK, TV> $one
@@ -195,7 +195,7 @@ function array_uintersect_uassoc(
195195
): array {}
196196

197197
/**
198-
* @template TK of mixed
198+
* @template TK of array-key
199199
* @template TV of mixed
200200
*
201201
* @param array<TK, TV> $one

Diff for: tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

+23-23
Original file line numberDiff line numberDiff line change
@@ -1721,11 +1721,11 @@ public function testArrayDiffUassoc(): void
17211721
$this->checkExplicitMixed = true;
17221722
$this->analyse([__DIR__ . '/data/array_diff_uassoc.php'], [
17231723
[
1724-
'Parameter #3 $data_comp_func of function array_diff_uassoc expects callable(string, string): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
1724+
'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.',
17251725
22,
17261726
],
17271727
[
1728-
'Parameter #3 $data_comp_func of function array_diff_uassoc expects callable(int, int): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
1728+
'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.',
17291729
30,
17301730
],
17311731
]);
@@ -1736,11 +1736,11 @@ public function testArrayDiffUkey(): void
17361736
$this->checkExplicitMixed = true;
17371737
$this->analyse([__DIR__ . '/data/array_diff_ukey.php'], [
17381738
[
1739-
'Parameter #3 $key_comp_func of function array_diff_ukey expects callable(string, string): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
1739+
'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.',
17401740
22,
17411741
],
17421742
[
1743-
'Parameter #3 $key_comp_func of function array_diff_ukey expects callable(int, int): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
1743+
'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.',
17441744
30,
17451745
],
17461746
]);
@@ -1751,11 +1751,11 @@ public function testArrayIntersectUassoc(): void
17511751
$this->checkExplicitMixed = true;
17521752
$this->analyse([__DIR__ . '/data/array_intersect_uassoc.php'], [
17531753
[
1754-
'Parameter #3 $key_compare_func of function array_intersect_uassoc expects callable(string, string): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
1754+
'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.',
17551755
22,
17561756
],
17571757
[
1758-
'Parameter #3 $key_compare_func of function array_intersect_uassoc expects callable(int, int): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
1758+
'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.',
17591759
30,
17601760
],
17611761
]);
@@ -1766,11 +1766,11 @@ public function testArrayIntersectUkey(): void
17661766
$this->checkExplicitMixed = true;
17671767
$this->analyse([__DIR__ . '/data/array_intersect_ukey.php'], [
17681768
[
1769-
'Parameter #3 $key_compare_func of function array_intersect_ukey expects callable(string, string): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
1769+
'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.',
17701770
22,
17711771
],
17721772
[
1773-
'Parameter #3 $key_compare_func of function array_intersect_ukey expects callable(int, int): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
1773+
'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.',
17741774
30,
17751775
],
17761776
]);
@@ -1781,34 +1781,34 @@ public function testArrayUdiffAssoc(): void
17811781
$this->checkExplicitMixed = true;
17821782
$this->analyse([__DIR__ . '/data/array_udiff_assoc.php'], [
17831783
[
1784-
'Parameter #3 $key_comp_func of function array_udiff_assoc expects callable(int, int): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
1784+
'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.',
17851785
22,
17861786
],
17871787
[
1788-
'Parameter #3 $key_comp_func of function array_udiff_assoc expects callable(int, int): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
1788+
'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.',
17891789
30,
17901790
],
17911791
]);
17921792
}
17931793

1794-
public function testArrayUdiffUsssoc(): void
1794+
public function testArrayUdiffUasssoc(): void
17951795
{
17961796
$this->checkExplicitMixed = true;
17971797
$this->analyse([__DIR__ . '/data/array_udiff_uassoc.php'], [
17981798
[
1799-
'Parameter #3 $data_comp_func of function array_udiff_uassoc expects callable(string, string): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
1799+
'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.',
18001800
28,
18011801
],
18021802
[
1803-
'Parameter #4 $key_comp_func of function array_udiff_uassoc expects callable(string, string): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
1803+
'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.',
18041804
31,
18051805
],
18061806
[
1807-
'Parameter #3 $data_comp_func of function array_udiff_uassoc expects callable(int, int): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
1807+
'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.',
18081808
39,
18091809
],
18101810
[
1811-
'Parameter #4 $key_comp_func of function array_udiff_uassoc expects callable(int, int): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
1811+
'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.',
18121812
42,
18131813
],
18141814
]);
@@ -1819,11 +1819,11 @@ public function testArrayUintersectAssoc(): void
18191819
$this->checkExplicitMixed = true;
18201820
$this->analyse([__DIR__ . '/data/array_uintersect_assoc.php'], [
18211821
[
1822-
'Parameter #3 $data_compare_func of function array_uintersect_assoc expects callable(string, string): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
1822+
'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.',
18231823
22,
18241824
],
18251825
[
1826-
'Parameter #3 $data_compare_func of function array_uintersect_assoc expects callable(int, int): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
1826+
'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.',
18271827
30,
18281828
],
18291829
]);
@@ -1834,19 +1834,19 @@ public function testArrayUintersectUassoc(): void
18341834
$this->checkExplicitMixed = true;
18351835
$this->analyse([__DIR__ . '/data/array_uintersect_uassoc.php'], [
18361836
[
1837-
'Parameter #3 $data_compare_func of function array_uintersect_uassoc expects callable(string, string): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
1837+
'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.',
18381838
28,
18391839
],
18401840
[
1841-
'Parameter #4 $key_compare_func of function array_uintersect_uassoc expects callable(string, string): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
1841+
'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.',
18421842
31,
18431843
],
18441844
[
1845-
'Parameter #3 $data_compare_func of function array_uintersect_uassoc expects callable(int, int): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
1845+
'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.',
18461846
39,
18471847
],
18481848
[
1849-
'Parameter #4 $key_compare_func of function array_uintersect_uassoc expects callable(int, int): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
1849+
'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.',
18501850
42,
18511851
],
18521852
]);
@@ -1857,11 +1857,11 @@ public function testArrayUintersect(): void
18571857
$this->checkExplicitMixed = true;
18581858
$this->analyse([__DIR__ . '/data/array_uintersect.php'], [
18591859
[
1860-
'Parameter #3 $data_compare_func of function array_uintersect expects callable(string, string): int<-1, 1>, Closure(int, int): int<-1, 1> given.',
1860+
'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.',
18611861
22,
18621862
],
18631863
[
1864-
'Parameter #3 $data_compare_func of function array_uintersect expects callable(int, int): int<-1, 1>, Closure(string, string): int<-1, 1> given.',
1864+
'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.',
18651865
30,
18661866
],
18671867
]);

Diff for: tests/PHPStan/Rules/Functions/data/array_diff_uassoc.php

+1-1
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],

Diff for: tests/PHPStan/Rules/Functions/data/array_diff_ukey.php

+1-1
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],

Diff for: tests/PHPStan/Rules/Functions/data/array_intersect_uassoc.php

+1-1
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],

Diff for: tests/PHPStan/Rules/Functions/data/array_intersect_ukey.php

+1-1
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],

Diff for: tests/PHPStan/Rules/Functions/data/array_udiff_assoc.php

+1-1
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],

Diff for: tests/PHPStan/Rules/Functions/data/array_udiff_uassoc.php

+1-1
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'],

Diff for: tests/PHPStan/Rules/Functions/data/array_uintersect.php

+1-1
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'],

Diff for: tests/PHPStan/Rules/Functions/data/array_uintersect_assoc.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php namespace ParamsArrayUintersectAssoc;
22

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

Diff for: tests/PHPStan/Rules/Functions/data/array_uintersect_uassoc.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php namespace ParamsArrayUIntersectUassoc;
22

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

0 commit comments

Comments
 (0)