-
Notifications
You must be signed in to change notification settings - Fork 546
Cover non-empty-string in substr
#577
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
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
1d69bd4
Cover non-empty-string in `substr`
staabm f874836
Create SubstrDynamicReturnTypeExtension.php
staabm 0db389d
Update config.neon
staabm 1cb919d
Update non-empty-string.php
staabm 3fe65c5
Update SubstrDynamicReturnTypeExtension.php
staabm 28049b4
Update SubstrDynamicReturnTypeExtension.php
staabm f350a2b
Update non-empty-string.php
staabm 59874d8
Update config.neon
staabm 07beeec
Update SubstrDynamicReturnTypeExtension.php
staabm c197dcb
Update non-empty-string.php
staabm 309127a
Update non-empty-string.php
staabm 6f0bddc
Update SubstrDynamicReturnTypeExtension.php
staabm a68607c
Update non-empty-string.php
staabm 19e83a4
Update non-empty-string.php
staabm 922f0c3
Update SubstrDynamicReturnTypeExtension.php
staabm b788db6
Update non-empty-string.php
staabm 33ebef4
Update non-empty-string.php
staabm 868f7dc
Update SubstrDynamicReturnTypeExtension.php
staabm a578274
Typo
staabm 6553b7c
fix CS
clxmstaab 6fef8bd
fix CS in test
clxmstaab 0adb9ed
cleanup after review
clxmstaab 7c1385f
added integer-range tests
clxmstaab c1d2128
simplify
clxmstaab d90b84c
removed duplicated test
clxmstaab 1eef6d3
fix CS
clxmstaab 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
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,59 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Type\Php; | ||
|
|
||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Reflection\FunctionReflection; | ||
| use PHPStan\Reflection\ParametersAcceptorSelector; | ||
| use PHPStan\Type\Accessory\AccessoryNonEmptyStringType; | ||
| use PHPStan\Type\Constant\ConstantIntegerType; | ||
| use PHPStan\Type\DynamicFunctionReturnTypeExtension; | ||
| use PHPStan\Type\IntegerRangeType; | ||
| use PHPStan\Type\IntersectionType; | ||
| use PHPStan\Type\StringType; | ||
|
|
||
| class SubstrDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension | ||
| { | ||
|
|
||
| public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
| { | ||
| return $functionReflection->getName() === 'substr'; | ||
| } | ||
|
|
||
| public function getTypeFromFunctionCall( | ||
| FunctionReflection $functionReflection, | ||
| FuncCall $functionCall, | ||
| Scope $scope | ||
| ): \PHPStan\Type\Type | ||
| { | ||
| $args = $functionCall->args; | ||
| if (count($args) === 0) { | ||
| return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType(); | ||
| } | ||
|
|
||
| if (count($args) >= 2) { | ||
| $string = $scope->getType($args[0]->value); | ||
| $offset = $scope->getType($args[1]->value); | ||
|
|
||
| $negativeOffset = IntegerRangeType::fromInterval(null, -1)->isSuperTypeOf($offset)->yes(); | ||
| $zeroOffset = (new ConstantIntegerType(0))->isSuperTypeOf($offset)->yes(); | ||
| $positiveLength = false; | ||
|
|
||
| if (count($args) === 3) { | ||
| $length = $scope->getType($args[2]->value); | ||
| $positiveLength = IntegerRangeType::fromInterval(1, null)->isSuperTypeOf($length)->yes(); | ||
| } | ||
|
|
||
| if ($string->isNonEmptyString()->yes() && ($negativeOffset || $zeroOffset && $positiveLength)) { | ||
| return new IntersectionType([ | ||
| new StringType(), | ||
| new AccessoryNonEmptyStringType(), | ||
| ]); | ||
| } | ||
| } | ||
|
|
||
| return new StringType(); | ||
| } | ||
|
|
||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should ask about whether a number is positive or negative this way:
It also accounts for unions of ConstantIntegerType, like
1|3.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thx, fixed.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this mean I should check for a zero offset with
IntegerRangeType::fromInterval(0, 0)->isSuperTypeOf($offset)->yes()?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can check for that with:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also added testcases to cover that