1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +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 committed by Daniil Gentili
parent 72f64692ae
commit 4dc045f30d
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
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];
}'
],
];
}