Skip to content

Commit

Permalink
ignore complex placeholders for now
Browse files Browse the repository at this point in the history
Fix #9900
Fix #9901
Fix original core stubs sprintf returning a wrong type too (unrelated to return type provider) for placeholders that are always empty
  • Loading branch information
kkmuffme committed Jun 12, 2023
1 parent 28f8cbc commit 92ccad7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
}

// there are probably additional formats that return an empty string, this is just a starting point
if (preg_match('/^%(?:\d+\$)?[-+]?0(\.0)?s$/', $type->getSingleStringLiteral()->value) === 1) {
if (preg_match('/^%(?:\d+\$)?[-+]?0(?:\.0)?s$/', $type->getSingleStringLiteral()->value) === 1) {
IssueBuffer::maybeAdd(
new InvalidArgument(
'The pattern of argument 1 of ' . $event->getFunctionId() . ' will always return an empty string',
Expand All @@ -107,6 +107,16 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
return Type::getString('');
}

// placeholders are too complex to handle for now
if (preg_match('/%(?:\d+\$)?[-+]?(?:\d+|\*)(?:\.(?:\d+|\*))?[bcdouxXeEfFgGhHs]/', $type->getSingleStringLiteral()->value) === 1) {
if ($event->getFunctionId() === 'printf') {
return null;
}

// the core stubs are wrong for these too, since these might be empty strings, e.g. sprintf(\'%0.*s\', 0, "abc")
return Type::getString();
}

$args_count = count($call_args) - 1;
$dummy = array_fill(0, $args_count, '');

Expand Down
27 changes: 27 additions & 0 deletions tests/ReturnTypeProvider/SprintfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,33 @@ public function providerValidCodeParse(): iterable
'InvalidArgument',
],
];

yield 'sprintfComplexPlaceholderNotYetSupported1' => [
'code' => '<?php
$val = sprintf(\'%*.0s\', 0, "abc");
',
'assertions' => [
'$val===' => 'string',
],
];

yield 'sprintfComplexPlaceholderNotYetSupported2' => [
'code' => '<?php
$val = sprintf(\'%0.*s\', 0, "abc");
',
'assertions' => [
'$val===' => 'string',
],
];

yield 'sprintfComplexPlaceholderNotYetSupported3' => [
'code' => '<?php
$val = sprintf(\'%*.*s\', 0, 0, "abc");
',
'assertions' => [
'$val===' => 'string',
],
];
}

public function providerInvalidCodeParse(): iterable
Expand Down

0 comments on commit 92ccad7

Please sign in to comment.