Repository navigation
Conversation
|
I guess I'd try using xdebug here, in case you didn't do that already. |
|
I tried, but wasn't sure where to look tbh |
|
after a bit more trying, I was able to reduce the repro further this PR now includes a proper NodeScopeResolverTest for the problem |
| ConstantScalarType $constantType | ||
| ): SpecifiedTypes | ||
| { | ||
| if ($constantType->getValue() === null) { |
There was a problem hiding this comment.
it seems this nullcheck is somehow messing it up
maybe @rvanvelzen has an idea?
There was a problem hiding this comment.
That's interesting. Null isn't even a scalar, right? Is this ever triggered and is there even a ConstantType that implements returning null via getValue? nevermind NullType implements this of course..
Might be unrelated to the bug though.. 😅
There was a problem hiding this comment.
Ah one more thing came to mind: could it be related to the fact that phpstan remembers what the method returned? Maybe this needs to he invalidated below in the if scope.
The bug might then be that method return types are falsely remembered / not invalidated, even if the class instance changes. Or smth like that
There was a problem hiding this comment.
@rajyan do you have an idea on how to approach this problem? maybe you even have time for a fix?
There was a problem hiding this comment.
https://phpstan.org/r/a7200b8c-8482-4fd2-b782-ecdb8052a28a
This bug looks very interesting, the commented out line changes the result. Rough guess, the type specification with instanceof is not working or moreSpecificType $var->getValue() remembered by !== null is breaking something.
I don’t have time this week (and this weekend) but I think I can fix it next week.
There was a problem hiding this comment.
Another guess is that the moreSpecificType is not correctly invalidated correctly on type specification by instanceof.
|
Check this strange behavior as well:
|
|
@rvanvelzen @rajyan might one of you guys has time to look into this problem? |
|
I've looked into this recently but it didn't look trivial to solve. I'm a bit swamped at work right now but I'll look into it more when that settles down again :) |
|
@staabm |
db11b2d to
815caf2
Compare
|
after working thru this I implemented several different solutions, which made the test pass, but regressed other things. by doing this I got a better understanding how things work and how a possible solution could look like. atm I think we need to adjust this block: phpstan-src/src/Analyser/MutatingScope.php Lines 3551 to 3579 in b0babd0 .. so it takes into account when a variable get specified to a new ObjectType - e.g. via class HelloWorld
{
public function doFoo(
ConstantScalarType $constantType
)
{
if ($constantType->getValue() === null) {
return;
}
assertType('bool|float|int|string', $constantType->getValue());
if (
$constantType instanceof ConstantStringType
) {
assertType('string', $constantType->getValue());
}
}
}in the end I think this is a similar thing, this line achieves for array-offset-types: phpstan-src/src/Analyser/MutatingScope.php Line 3577 in b0babd0 |
6f7c064 to
befadc3
Compare
befadc3 to
b8e3e92
Compare
|
finally, this one is ready for review. |
b8e3e92 to
f511470
Compare
There was a problem hiding this comment.
Can you please explain what the bug is and why this change fixes it? It seems too specific and too hardcoded for this specific test case...
There was a problem hiding this comment.
I tried explaining it here: #1396 (comment)
There was a problem hiding this comment.
it boils down to
- phpstan knows a moreSpecificType for a method call
$constant->getValue(), based on$constantbeeing of typeConstantScalarType - after that the
$constantobject-type get narrowed toConstantStringTypeviainstanceof - phpstans already known moreSpecificType is not changed, and therefore phpstan doesn't realize that
$constant->getValue()can only returnstringafter theinstanceof ConstantStringTypecase
There was a problem hiding this comment.
the fix should unblock
|
Is this still a problem? |
|
The underlying bug should still be present, but in phpstan-src I worked arround it in a related PR, so the phpstan-src codebase is no longer blocked by it right now |
f511470 to
14452a8
Compare
1d8abd5 to
d590419
Compare
085e094 to
15b02f4
Compare
36dea2b to
8f37360
Compare
|
It'd be better to reproduce this on phpstan.org/try and open an issue. |
with https://github.com/phpstan/phpstan-src/pull/1383/files#r887647098 I was running into this bug.
the problem is, that phpstan should dump the type
stringbut reportsbool|float|int|stringfor thedumpTypecall seen here:I am not yet sure how to debug this issue further. I also tried reproducing the case in a smaler repro script, but this did not work out yet.
any ideas/hints?