Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support narrowing down array_filter return type via string callable
- Loading branch information
1 parent
34587e5
commit 1789915
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
tests/PHPStan/Analyser/data/array-filter-string-callables.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace ArrayFilter; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
/** | ||
* @param array<int, int> $list1 | ||
* @param array<int, int> $list2 | ||
* @param array<int, int> $list3 | ||
*/ | ||
function alwaysEvaluatesToFalse(array $list1, array $list2, array $list3): void | ||
{ | ||
$filtered1 = array_filter($list1, 'is_string'); | ||
assertType('array{}', $filtered1); | ||
|
||
$filtered2 = array_filter($list2, 'is_string', ARRAY_FILTER_USE_KEY); | ||
assertType('array{}', $filtered2); | ||
|
||
$filtered3 = array_filter($list3, 'is_string', ARRAY_FILTER_USE_BOTH); | ||
assertType('array{}', $filtered3); | ||
} | ||
|
||
/** | ||
* @param array<int|string, int|string> $map1 | ||
* @param array<int|string, int|string> $map2 | ||
* @param array<int|string, int|string> $map3 | ||
*/ | ||
function filtersString(array $map1, array $map2, array $map3): void | ||
{ | ||
$filtered1 = array_filter($map1, 'is_string'); | ||
assertType('array<int|string, string>', $filtered1); | ||
|
||
$filtered2 = array_filter($map2, 'is_string', ARRAY_FILTER_USE_KEY); | ||
assertType('array<string, int|string>', $filtered2); | ||
|
||
$filtered3 = array_filter($map3, 'is_string', ARRAY_FILTER_USE_BOTH); | ||
assertType('array<int|string, string>', $filtered3); | ||
} | ||
|
||
/** | ||
* @param array<int, int> $list1 | ||
*/ | ||
function nonCallableStringIsIgnored(array $list1): void | ||
{ | ||
$filtered1 = array_filter($list1, 'foo'); | ||
assertType('array<int, int>', $filtered1); | ||
} | ||
|
||
/** | ||
* @param array<int|string, int|string> $map1 | ||
* @param array<int|string, int|string> $map2 | ||
*/ | ||
function nonBuiltInFunctionsAreNotSupportedYetAndThereforeIgnored(array $map1, array $map2): void | ||
{ | ||
$filtered1 = array_filter($map1, '\ArrayFilter\isString'); | ||
assertType('array<int|string, int|string>', $filtered1); | ||
|
||
$filtered2 = array_filter($map2, '\ArrayFilter\Filters::isString'); | ||
assertType('array<int|string, int|string>', $filtered2); | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
*/ | ||
function isString($value): bool | ||
{ | ||
return is_string($value); | ||
} | ||
|
||
class Filters { | ||
/** | ||
* @param mixed $value | ||
*/ | ||
public static function isString($value): bool | ||
{ | ||
return is_string($value); | ||
} | ||
} |