Skip to content

Commit 104d3fb

Browse files
committed
Add tests for array_find() and array_find_key() to CallToFunctionParametersRuleTest
1 parent 9a75d3d commit 104d3fb

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,58 @@ public function testArrayFilterCallback(bool $checkExplicitMixed): void
886886
$this->analyse([__DIR__ . '/data/array_filter_callback.php'], $errors);
887887
}
888888

889+
public function testArrayFindCallback(): void
890+
{
891+
$this->analyse([__DIR__ . '/data/array_find.php'], [
892+
[
893+
'Parameter #2 $callback of function array_find expects callable(1|2, \'bar\'|\'foo\'): bool, Closure(string, int): bool given.',
894+
22,
895+
],
896+
[
897+
'Parameter #2 $callback of function array_find expects callable(1|2, \'bar\'|\'foo\'): bool, Closure(string, int): bool given.',
898+
30,
899+
],
900+
[
901+
'Parameter #2 $callback of function array_find expects callable(1|2, \'bar\'|\'foo\'): bool, Closure(int, string): (\'bar\'|\'foo\') given.',
902+
36,
903+
],
904+
[
905+
'Parameter #2 $callback of function array_find expects callable(mixed, int|string): bool, Closure(string, array): false given.',
906+
49,
907+
],
908+
[
909+
'Parameter #2 $callback of function array_find expects callable(mixed, int|string): bool, Closure(string, int): array{} given.',
910+
52,
911+
],
912+
]);
913+
}
914+
915+
public function testArrayFindKeyCallback(): void
916+
{
917+
$this->analyse([__DIR__ . '/data/array_find_key.php'], [
918+
[
919+
'Parameter #2 $callback of function array_find_key expects callable(1|2, \'bar\'|\'foo\'): bool, Closure(string, int): bool given.',
920+
22,
921+
],
922+
[
923+
'Parameter #2 $callback of function array_find_key expects callable(1|2, \'bar\'|\'foo\'): bool, Closure(string, int): bool given.',
924+
30,
925+
],
926+
[
927+
'Parameter #2 $callback of function array_find_key expects callable(1|2, \'bar\'|\'foo\'): bool, Closure(int, string): (\'bar\'|\'foo\') given.',
928+
36,
929+
],
930+
[
931+
'Parameter #2 $callback of function array_find_key expects callable(mixed, int|string): bool, Closure(string, array): false given.',
932+
49,
933+
],
934+
[
935+
'Parameter #2 $callback of function array_find_key expects callable(mixed, int|string): bool, Closure(string, int): array{} given.',
936+
52,
937+
],
938+
]);
939+
}
940+
889941
public function testBug5356(): void
890942
{
891943
$this->analyse([__DIR__ . '/data/bug-5356.php'], [
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php declare(strict_types = 1); // lint >= 8.4
2+
3+
// ok
4+
array_find(
5+
['foo' => 1, 'bar' => 2],
6+
function($value, $key) {
7+
return $key === 0;
8+
}
9+
);
10+
11+
// ok
12+
array_find(
13+
['foo' => 1, 'bar' => 2],
14+
function(int $value, string $key) {
15+
return $key === 0;
16+
}
17+
);
18+
19+
// bad parameters
20+
array_find(
21+
['foo' => 1, 'bar' => 2],
22+
function(string $value, int $key): bool {
23+
return $key === 0;
24+
}
25+
);
26+
27+
// bad parameters
28+
array_find(
29+
['foo' => 1, 'bar' => 2],
30+
fn (string $item, int $key) => $key === 0,
31+
);
32+
33+
// bad return type
34+
array_find(
35+
['foo' => 1, 'bar' => 2],
36+
function(int $value, string $key) {
37+
return $key;
38+
},
39+
);
40+
41+
if (is_array($array)) {
42+
// ok
43+
array_find($array, fn ($value, $key) => $key === 0);
44+
45+
// ok
46+
array_find($array, fn (string $value, int $key) => $key === 0);
47+
48+
// bad parameters
49+
array_find($array, fn (string $item, array $key) => $key === 0);
50+
51+
// bad return type
52+
array_find($array, fn (string $value, int $key): array => []);
53+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php declare(strict_types = 1); // lint >= 8.4
2+
3+
// ok
4+
array_find_key(
5+
['foo' => 1, 'bar' => 2],
6+
function($value, $key) {
7+
return $key === 0;
8+
}
9+
);
10+
11+
// ok
12+
array_find_key(
13+
['foo' => 1, 'bar' => 2],
14+
function(int $value, string $key) {
15+
return $key === 0;
16+
}
17+
);
18+
19+
// bad parameters
20+
array_find_key(
21+
['foo' => 1, 'bar' => 2],
22+
function(string $value, int $key): bool {
23+
return $key === 0;
24+
}
25+
);
26+
27+
// bad parameters
28+
array_find_key(
29+
['foo' => 1, 'bar' => 2],
30+
fn (string $item, int $key) => $key === 0,
31+
);
32+
33+
// bad return type
34+
array_find_key(
35+
['foo' => 1, 'bar' => 2],
36+
function(int $value, string $key) {
37+
return $key;
38+
},
39+
);
40+
41+
if (is_array($array)) {
42+
// ok
43+
array_find_key($array, fn ($value, $key) => $key === 0);
44+
45+
// ok
46+
array_find_key($array, fn (string $value, int $key) => $key === 0);
47+
48+
// bad parameters
49+
array_find_key($array, fn (string $item, array $key) => $key === 0);
50+
51+
// bad return type
52+
array_find_key($array, fn (string $value, int $key): array => []);
53+
}

0 commit comments

Comments
 (0)