-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: resolve phpstan errors method.nonObject in tests for Types #7253
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
Closed
shakaran
wants to merge
5
commits into
doctrine:4.4.x
from
shakaran:feat/resolve-phpstan-method-nonobject
Closed
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b06a2ae
feat: resolve phpstan errors method.nonObject in tests for Types
shakaran 1abb899
feat: force to use abstract method for convertToPHPValue
shakaran 9e6dffa
fix: add remaining method convertToPHPValue missed in children class …
shakaran 87cc91c
fix: remove inheritdoc detected by phpsniffer
shakaran c3c9416
fix: typo use integer instead string
shakaran 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
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.
The problem here is that PHPStan does not understand the type of
$this->type. Your annotation is a workaround, not a solution.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.
Which I see meanwhile I fix this, it is src/Types/Type.php is an abstract class.
And the problem using convertToPHPValue in that class, it is that return as mixed, but other clases like tests/Types/TimeTest.php are overriding the abstract return, for example with ?DateTime
In src/Types/VarDateTimeImmutableType.php is ?DateTimeImmutable.
So I think that the problem is that the method should be abtract in src/Types/Type.php and no contain body
That means which src/Types/TypeWithConstructor.php should have also the method to avoid:
Line TypeWithConstructor.php
:10 Non-abstract class Doctrine\DBAL\Tests\Types\TypeWithConstructor contains abstract method convertToPHPValue() from class
Doctrine\DBAL\Types\Type.
I made that changes at 1abb899
So phpstan is a static analyzer, it doesn't execute code or infer types based on which concrete subclass is instantiated at runtime.
In TimeTest.php, $this->type is declared as Type (the base class), even though it's initialized as new TimeType().
When you call $this->type->convertToPHPValue('01:23:34', $this->platform), phpstan sees the return as mixed because that's what the base class declares.
It doesn't "know" that $this->type is actually a TimeType instance that returns ?DateTime.
So I understan which this is a limitation of static analysis: it prioritizes declared types over inferred runtime types to avoid false positives.
And level 9 requires explicit types where ambiguity exists, even if the code is "obviously" correct at runtime.
Other options that I can try:
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.
No. A subclass may narrow the return type of a method. This has nothing to do with the parent class/method being abstract or not.
BaseDateTypeTestCasegeneric. ✌🏻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.
The problem is not with the tests. The problem is with the types.
Here, one test covers one type, so we have a luxury of parameterizing the tests by guessing what PHP types the given data type operates. But users interact with multiple types at a time – what are they going to do? The test should reflect the intended experience of the consumer, and that's not the experience we intend for.
Instead of tests, types should be parameterized, but that's likely a Pandora's box (our types are a mix of database types, user-defined types, mapping from PHP types to database data types, etc.).
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.
#7268