-
Notifications
You must be signed in to change notification settings - Fork 574
Narrow types after strlen() #5703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
staabm
merged 7 commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-97bj22r
May 20, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cabe470
Skip string-with-integer-offset decomposition in `NonexistentOffsetIn…
staabm 5fe4185
Narrow string argument to non-empty-string via strlen/mb_strlen in tr…
phpstan-bot 7a0fca9
Move strlen/mb_strlen type narrowing to StrlenFunctionTypeSpecifyingE…
phpstan-bot fa7d3fa
Add comprehensive tests for StrlenFunctionTypeSpecifyingExtension con…
phpstan-bot 8f35bdc
Add non-string input tests for float and bool types in strlen narrowing
phpstan-bot d48d6cc
Rename $mixed to $intOrString in nonStringInput test case
phpstan-bot fa6d0ac
Update bug-13688.php
staabm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Type\Php; | ||
|
|
||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Analyser\SpecifiedTypes; | ||
| use PHPStan\Analyser\TypeSpecifier; | ||
| use PHPStan\Analyser\TypeSpecifierAwareExtension; | ||
| use PHPStan\Analyser\TypeSpecifierContext; | ||
| use PHPStan\DependencyInjection\AutowiredService; | ||
| use PHPStan\Reflection\FunctionReflection; | ||
| use PHPStan\Type\Accessory\AccessoryNonEmptyStringType; | ||
| use PHPStan\Type\FunctionTypeSpecifyingExtension; | ||
| use PHPStan\Type\StaticTypeFactory; | ||
| use function count; | ||
| use function in_array; | ||
|
|
||
| #[AutowiredService] | ||
| final class StrlenFunctionTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension, TypeSpecifierAwareExtension | ||
| { | ||
|
|
||
| private TypeSpecifier $typeSpecifier; | ||
|
|
||
| public function isFunctionSupported( | ||
| FunctionReflection $functionReflection, | ||
| FuncCall $node, | ||
| TypeSpecifierContext $context, | ||
| ): bool | ||
| { | ||
| return !$context->null() | ||
| && count($node->getArgs()) >= 1 | ||
| && in_array($functionReflection->getName(), ['strlen', 'mb_strlen'], true); | ||
| } | ||
|
|
||
| public function specifyTypes( | ||
| FunctionReflection $functionReflection, | ||
| FuncCall $node, | ||
| Scope $scope, | ||
| TypeSpecifierContext $context, | ||
| ): SpecifiedTypes | ||
| { | ||
| if (!$scope->getType($node->getArgs()[0]->value)->isString()->yes()) { | ||
| return new SpecifiedTypes([], []); | ||
| } | ||
|
|
||
| $argSpecifiedTypes = $this->typeSpecifier->create($node->getArgs()[0]->value, new AccessoryNonEmptyStringType(), $context, $scope); | ||
|
|
||
| if ($context->truthy()) { | ||
|
Check warning on line 49 in src/Type/Php/StrlenFunctionTypeSpecifyingExtension.php
|
||
| return $this->typeSpecifier->create($node, StaticTypeFactory::falsey(), TypeSpecifierContext::createFalse(), $scope) | ||
| ->setRootExpr($node) | ||
| ->unionWith($argSpecifiedTypes); | ||
| } | ||
|
|
||
| return $this->typeSpecifier->create($node, StaticTypeFactory::truthy(), TypeSpecifierContext::createFalse(), $scope) | ||
| ->setRootExpr($node) | ||
| ->unionWith($argSpecifiedTypes); | ||
| } | ||
|
|
||
| public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void | ||
| { | ||
| $this->typeSpecifier = $typeSpecifier; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug13688Nsrt; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| function doFoo(string $input): void | ||
| { | ||
| $inputLen = strlen($input); | ||
| assertType('string', $input); | ||
|
|
||
| if ($inputLen > 0) { | ||
| assertType('non-empty-string', $input); | ||
| } | ||
| } | ||
|
|
||
| function doBar(string $s): void | ||
| { | ||
| $len = strlen($s); | ||
| if ($len) { | ||
| assertType('non-empty-string', $s); | ||
| } else { | ||
| assertType("''", $s); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param ''|':' $input | ||
| */ | ||
| function doBaz(string $input): void | ||
| { | ||
| $inputLen = strlen($input); | ||
| if ($inputLen > 0) { | ||
| assertType("':'", $input); | ||
| assertType('1', $inputLen); | ||
| } | ||
| } | ||
|
|
||
| function directTruthy(string $s): void | ||
| { | ||
| if (strlen($s)) { | ||
| assertType('non-empty-string', $s); | ||
| } else { | ||
| assertType("''", $s); | ||
| } | ||
| assertType('string', $s); | ||
| } | ||
|
|
||
| function directNegation(string $s): void | ||
| { | ||
| if (!strlen($s)) { | ||
| assertType("''", $s); | ||
| } else { | ||
| assertType('non-empty-string', $s); | ||
| } | ||
| assertType('string', $s); | ||
| } | ||
|
|
||
| function mbStrlenTruthy(string $s): void | ||
| { | ||
| if (mb_strlen($s)) { | ||
| assertType('non-empty-string', $s); | ||
| } else { | ||
| assertType("''", $s); | ||
| } | ||
| } | ||
|
|
||
| function mbStrlenNegation(string $s): void | ||
| { | ||
| if (!mb_strlen($s)) { | ||
| assertType("''", $s); | ||
| } else { | ||
| assertType('non-empty-string', $s); | ||
| } | ||
| } | ||
|
|
||
| function booleanAnd(string $a, string $b): void | ||
| { | ||
| if (strlen($a) && strlen($b)) { | ||
| assertType('non-empty-string', $a); | ||
| assertType('non-empty-string', $b); | ||
| } | ||
| } | ||
|
|
||
| function booleanOr(string $a, string $b): void | ||
| { | ||
| if (!strlen($a) || !strlen($b)) { | ||
| return; | ||
| } | ||
| assertType('non-empty-string', $a); | ||
| assertType('non-empty-string', $b); | ||
| } | ||
|
|
||
| function ternary(string $s): void | ||
| { | ||
| $result = strlen($s) ? 'non-empty' : 'empty'; | ||
| assertType("'empty'|'non-empty'", $result); | ||
| } | ||
|
|
||
| /** @param int|string $intOrString */ | ||
| function nonStringInput($intOrString): void | ||
| { | ||
| if (strlen($intOrString)) { | ||
| assertType('int|string', $intOrString); | ||
| } | ||
| } | ||
|
|
||
| /** @param float|string $floatOrString */ | ||
| function nonStringInputFloat($floatOrString): void | ||
| { | ||
| if (strlen($floatOrString)) { | ||
| assertType('float|string', $floatOrString); | ||
| } | ||
| } | ||
|
|
||
| /** @param bool|string $boolOrString */ | ||
| function nonStringInputBool($boolOrString): void | ||
| { | ||
| if (strlen($boolOrString)) { | ||
| assertType('bool|string', $boolOrString); | ||
| } | ||
| } | ||
|
|
||
| function looseComparisonTrue(string $s): void | ||
| { | ||
| if (strlen($s) == true) { | ||
| assertType('non-empty-string', $s); | ||
| } else { | ||
| assertType("''", $s); | ||
| } | ||
| } | ||
|
|
||
| function looseComparisonFalse(string $s): void | ||
| { | ||
| if (strlen($s) == false) { | ||
| assertType("''", $s); | ||
| } else { | ||
| assertType('non-empty-string', $s); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug13688; | ||
|
|
||
| $inputs = [ '', ':' ]; | ||
|
|
||
| foreach ( $inputs as $input ) | ||
| { | ||
| $inputLen = \strlen($input); | ||
| $hasTrailingColon = $inputLen > 0 && $input[$inputLen-1] === ':'; | ||
| echo $hasTrailingColon ? "{$input} has trailing colon\n" : "{$input} does not have trailing colon\n"; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.