1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Support analysing preg_match_all args in reverse to infer matches type

This commit is contained in:
Matthew Brown 2020-09-04 20:33:02 -04:00
parent 681eff6dd4
commit 3605eeee04
2 changed files with 13 additions and 3 deletions

View File

@ -599,6 +599,10 @@ class ArgumentsAnalyzer
}
}
if ($method_id === 'preg_match_all' && count($args) > 3) {
$args = array_reverse($args, true);
}
foreach ($args as $argument_offset => $arg) {
$function_param = $function_param_count > $argument_offset
? $function_params[$argument_offset]

View File

@ -1325,16 +1325,22 @@ class FunctionCallTest extends TestCase
'pregMatchAll' => [
'<?php
/**
* @psalm-pure
*
* @return array<list<string>>
*/
function extractUsernames(string $input): array {
preg_match_all(\'/@[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}(?!\w)/\', $input, $matches);
preg_match_all(\'/([a-zA-Z])*/\', $input, $matches);
return $matches;
}'
],
'pregMatchAllOffsetCapture' => [
'<?php
function foo(string $input): array {
preg_match_all(\'/([a-zA-Z])*/\', $input, $matches, PREG_OFFSET_CAPTURE);
return $matches[0];
}'
],
];
}