mirror of
https://github.com/danog/psalm.git
synced 2025-01-22 05:41:20 +01:00
PHP 7 format with only placeholders isn't falsable if valid
limit tests to PHP 8 to avoid having to create them twice and add specific test for Issue 9941
This commit is contained in:
parent
c5fee532f3
commit
0535c6b877
@ -78,6 +78,8 @@ class SprintfReturnTypeProvider implements FunctionReturnTypeProviderInterface
|
||||
}
|
||||
}
|
||||
|
||||
// PHP 7 handling for formats that do not contain anything but placeholders
|
||||
$is_falsable = true;
|
||||
foreach ($call_args as $index => $call_arg) {
|
||||
$type = $node_type_provider->getType($call_arg->value);
|
||||
if ($type === null && $index === 0 && $event->getFunctionId() === 'printf') {
|
||||
@ -259,11 +261,15 @@ class SprintfReturnTypeProvider implements FunctionReturnTypeProviderInterface
|
||||
return Type::getNonEmptyString();
|
||||
}
|
||||
|
||||
// if we didn't have a valid result
|
||||
// if we didn't have any valid result
|
||||
// the pattern is invalid or not yet supported by the return type provider
|
||||
if ($initial_result === null || $initial_result === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// the initial result is an empty string
|
||||
// which means the format is valid and it depends on the args, whether it is non-empty-string or not
|
||||
$is_falsable = false;
|
||||
}
|
||||
|
||||
if ($index === 0 && $event->getFunctionId() === 'printf') {
|
||||
@ -307,6 +313,10 @@ class SprintfReturnTypeProvider implements FunctionReturnTypeProviderInterface
|
||||
return Type::getNonEmptyString();
|
||||
}
|
||||
|
||||
if ( $is_falsable === false ) {
|
||||
return Type::getString();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -130,6 +130,8 @@ class SprintfTest extends TestCase
|
||||
'assertions' => [
|
||||
'$val===' => 'int<0, max>',
|
||||
],
|
||||
'ignored_issues' => [],
|
||||
'php_version' => '8.0',
|
||||
];
|
||||
|
||||
yield 'sprintfEmptyStringFormat' => [
|
||||
@ -163,6 +165,8 @@ class SprintfTest extends TestCase
|
||||
'assertions' => [
|
||||
'$val===' => 'string',
|
||||
],
|
||||
'ignored_issues' => [],
|
||||
'php_version' => '8.0',
|
||||
];
|
||||
|
||||
yield 'sprintfComplexPlaceholderNotYetSupported2' => [
|
||||
@ -172,6 +176,8 @@ class SprintfTest extends TestCase
|
||||
'assertions' => [
|
||||
'$val===' => 'string',
|
||||
],
|
||||
'ignored_issues' => [],
|
||||
'php_version' => '8.0',
|
||||
];
|
||||
|
||||
yield 'sprintfComplexPlaceholderNotYetSupported3' => [
|
||||
@ -181,6 +187,8 @@ class SprintfTest extends TestCase
|
||||
'assertions' => [
|
||||
'$val===' => 'string',
|
||||
],
|
||||
'ignored_issues' => [],
|
||||
'php_version' => '8.0',
|
||||
];
|
||||
|
||||
yield 'sprintfSplatUnpackingArray' => [
|
||||
@ -191,6 +199,8 @@ class SprintfTest extends TestCase
|
||||
'assertions' => [
|
||||
'$val===' => 'string',
|
||||
],
|
||||
'ignored_issues' => [],
|
||||
'php_version' => '8.0',
|
||||
];
|
||||
|
||||
yield 'sprintfSplatUnpackingArrayNonEmpty' => [
|
||||
@ -202,6 +212,28 @@ class SprintfTest extends TestCase
|
||||
'$val===' => 'non-empty-string',
|
||||
],
|
||||
];
|
||||
|
||||
yield 'sprintfMultiplePlaceholdersNoErrorsIssue9941PHP7' => [
|
||||
'code' => '<?php
|
||||
$val = sprintf("Handling product %d => %d (%d)", 123, 456, 789);
|
||||
',
|
||||
'assertions' => [
|
||||
'$val===' => 'non-empty-string',
|
||||
],
|
||||
'ignored_issues' => [],
|
||||
'php_version' => '7.4',
|
||||
];
|
||||
|
||||
yield 'sprintfMultiplePlaceholdersNoErrorsIssue9941PHP8' => [
|
||||
'code' => '<?php
|
||||
$val = sprintf("Handling product %d => %d (%d)", 123, 456, 789);
|
||||
',
|
||||
'assertions' => [
|
||||
'$val===' => 'non-empty-string',
|
||||
],
|
||||
'ignored_issues' => [],
|
||||
'php_version' => '8.0',
|
||||
];
|
||||
}
|
||||
|
||||
public function providerInvalidCodeParse(): iterable
|
||||
|
Loading…
x
Reference in New Issue
Block a user