Skip to content

Upgrade to PHPStan 2.1 #2761

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

Open
wants to merge 18 commits into
base: 2.12.x
Choose a base branch
from
Open

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Apr 28, 2025

Q A
Type improvement
BC Break no
Fixed issues -

Summary

The upgrade is necessary because phpstan 1.10 that is currently has issues with PHP 8.4.

@GromNaN GromNaN added the Task label Apr 28, 2025
@GromNaN GromNaN added this to the 2.11.2 milestone Apr 28, 2025
@@ -68,7 +68,7 @@ and a date.
#[Field(type: 'date_immutable')]
public DateTimeImmutable $date;
/** @var Collection<Item> */
/** @var Collection<int, Item> */
Copy link
Member Author

Choose a reason for hiding this comment

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

Updating all doc examples, the Collection class has 2 type templates.

@@ -60,7 +61,7 @@ class ParentObject
#[ODM\Id]
private $id;

/** @var Collection<int, ChildObject>|array<ChildObject> */
/** @var Collection<int, ChildObject> */
Copy link
Member Author

Choose a reason for hiding this comment

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

Collection was never instantiated; but I think that's the type used when the relation is hydrated from the database. So I changed line 100 to create an ArrayCollection.

@@ -31,7 +30,7 @@ public function setUp(): void

public function tearDown(): void
{
unset($this->driver);
Copy link
Member Author

Choose a reason for hiding this comment

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

PHPstan warns that the property may have hooks in the child classes, so unset is not allowed.
Marking all test classes final is an other option, but doesn't work when we need to extend it (AbstractDriverTestCase). Just replacing with with a null value to unset the object.

Comment on lines +17 to 18
/** @phpstan-ignore missingType.generics, missingType.generics */
public function intersection(Collection&ArrayCollection $param): Collection&ArrayCollection
Copy link
Member Author

Choose a reason for hiding this comment

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

The code is repeated because the error is present twice: for the parameter and for the return type.

Comment on lines -1955 to +1956
if (method_exists($prop, 'isInitialized') && ! $prop->isInitialized($document)) {
if (! $prop->isInitialized($document)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

ReflectionProperty::isInitialized is always available since PHP 7.4, and we require PHP 8.1.

@@ -11,7 +11,7 @@ parameters:
excludePaths:
- tests/Doctrine/ODM/MongoDB/Tests/Mapping/Driver/fixtures/User.php
- tests/Hydrators/
- tests/PersistentCollections/
- tests/PersistentCollections/ (?)
Copy link
Member Author

Choose a reason for hiding this comment

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

The directory doesn't exist before tests run, this syntax allows for missing directory path.

Comment on lines +29 to +30
- message: '#with generic class Doctrine\\ODM\\MongoDB\\Mapping\\ClassMetadata does not specify its types: T$#'
identifier: missingType.generics
Copy link
Member Author

Choose a reason for hiding this comment

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

The ClassMetadata is always used in generic way. We would have to use ClassMetadata<object> everywhere.

# To be removed when reaching phpstan level 6
checkMissingVarTagTypehint: true
checkMissingTypehints: true
checkMissingIterableValueType: true
Copy link
Member Author

Choose a reason for hiding this comment

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

This option was removed from PHPStan 2.

@GromNaN GromNaN marked this pull request as ready for review April 28, 2025 20:05
@GromNaN GromNaN changed the base branch from 2.11.x to 2.12.x April 28, 2025 20:07
@GromNaN GromNaN modified the milestones: 2.11.2, 2.12.0 Apr 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants