Implement TypeSpecifierContext->getReturnType()#3878
Closed
staabm wants to merge 17 commits intophpstan:2.1.xfrom
Closed
Implement TypeSpecifierContext->getReturnType()#3878staabm wants to merge 17 commits intophpstan:2.1.xfrom
staabm wants to merge 17 commits intophpstan:2.1.xfrom
Conversation
staabm
commented
Mar 13, 2025
staabm
commented
Mar 13, 2025
| public static function createTrue(?Type $returnType = null): self | ||
| { | ||
| return self::create(self::CONTEXT_TRUE); | ||
| return self::create(self::CONTEXT_TRUE, $returnType); |
Contributor
Author
There was a problem hiding this comment.
todo: bit-field cleanup after the main-point of this PR works
About true/truthy - let’s get rid of the bit-masking, I think they will be implemented as getReturnType()->isTrue()->yes()
The other one as getReturnType()->toBoolean()->isTrue()->yes()
24676ef to
2c615a7
Compare
staabm
commented
Mar 13, 2025
| $negatedReturnType = null; | ||
| if ($this->returnType !== null) { | ||
| $baseType = $this->returnType->generalize(GeneralizePrecision::lessSpecific()); | ||
| $negatedReturnType = TypeCombinator::remove($baseType, $this->returnType); |
Contributor
Author
There was a problem hiding this comment.
This won't work for unions of int. We will need 2 two return types in the context and toggle between them on negate()
Contributor
Author
|
had a different try with #3881 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
for expression like
if (doFoo() > 0)we want to pass the context specific return-type information into type-specifying extensions so they can decide not just on true/truethy/false/falsy but also on "remaining return type values" (e.g. preg_match returns
0|1|false)this should help to reduce the number of hardcoded hacks within the TypeSpecifier und move over some cases into extensions (which also allows 3rd party extensions todo similar things, as they can't hard-code hack the TypeSpecifier)
ondrej quoted: