Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-9293.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/nullsafe-vs-scalar.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8517.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-1396.php');
}

/**
Expand Down
30 changes: 30 additions & 0 deletions tests/PHPStan/Analyser/data/bug-1396.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types = 1);

namespace Bug1396;

use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ConstantScalarType;
use function PHPStan\Testing\assertType;

class HelloWorld
{
public function doFoo(
ConstantScalarType $constantType
): SpecifiedTypes
{
assertType('bool|float|int|string|null', $constantType->getValue());

if ($constantType->getValue() === null) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems this nullcheck is somehow messing it up

maybe @rvanvelzen has an idea?

@herndlm herndlm Jun 4, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?
Might be unrelated to the bug though.. 😅
nevermind NullType implements this of course..

@herndlm herndlm Jun 4, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rajyan do you have an idea on how to approach this problem? maybe you even have time for a fix?

@rajyan rajyan Jun 9, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another guess is that the moreSpecificType is not correctly invalidated correctly on type specification by instanceof.

return new SpecifiedTypes();
}

assertType('bool|float|int|string', $constantType->getValue());

if (
$constantType instanceof ConstantStringType
) {
assertType('string', $constantType->getValue());
}
assertType('bool|float|int|string', $constantType->getValue());
}
}