Skip to content

Commit

Permalink
infer non-falsy-string in string replace functions
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Aug 30, 2022
1 parent 41be802 commit 0f79dd4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Type/Php/ReplaceFunctionsDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\IntersectionType;
Expand Down Expand Up @@ -84,6 +85,9 @@ private function getPreliminarilyResolvedTypeFromFunctionCall(
if (count($functionCall->getArgs()) > $replaceArgumentPosition) {
$replaceArgumentType = $scope->getType($functionCall->getArgs()[$replaceArgumentPosition]->value);

if ($replaceArgumentType->isNonFalsyString()->yes()) {
return new IntersectionType([new StringType(), new AccessoryNonFalsyStringType()]);
}
if ($replaceArgumentType->isNonEmptyString()->yes()) {
return new IntersectionType([new StringType(), new AccessoryNonEmptyStringType()]);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7344,15 +7344,15 @@ public function dataReplaceFunctions(): array
{
return [
[
'non-empty-string',
'non-falsy-string',
'$expectedString',
],
[
'string|null',
'$expectedString2',
],
[
'non-empty-string|null',
'non-falsy-string|null',
'$anotherExpectedString',
],
[
Expand Down

0 comments on commit 0f79dd4

Please sign in to comment.