Skip to content

chore: bump phpstan version #7239

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

VincentLanglet
Copy link
Contributor

Q A
Branch? main
Tickets Closes #..., closes #...
License MIT
Doc PR api-platform/docs#...

Hi @soyuka,

I wanted to bump PHPStan level to 6 and saw it was still using v1 ; so I started by bumping to v2.
Since there is lot of new errors I used the following strategy

  • Fix the easy ones
  • Ignore some of the new errors reported by PHPStan v2

Then I'll provide PR to fix some identifiers error, but I think it's better to split PR for readability.

@@ -17,7 +17,6 @@ parameters:
excludePaths:
# uses larastan
- src/Laravel
- src/Symfony/Bundle/Command/OpenApiCommand.php
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Those files doesn't exists and are reported by PHPStan v2 now.

@@ -75,6 +71,15 @@ parameters:
# Expected, due to backward compatibility
- '#Method GraphQL\\Type\\Definition\\WrappingType::getWrappedType\(\) invoked with 1 parameter, 0 required\.#'
- '#Access to an undefined property GraphQL\\Type\\Definition\\NamedType&GraphQL\\Type\\Definition\\Type::\$name\.#'
- "#Call to function method_exists\\(\\) with GraphQL\\\\Type\\\\Definition\\\\Type&GraphQL\\\\Type\\\\Definition\\\\WrappingType and 'getInnermostType' will always evaluate to true\\.#"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is a lot of method_exists call added for BC, so I updated the ignored errors.

# Allow extra assertions in tests: https://github.com/phpstan/phpstan-strict-rules/issues/130
- '#^Call to (static )?method PHPUnit\\Framework\\Assert::.* will always evaluate to true\.$#'

# TODO For PHPStan 2.0
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a list of errors which I'd like to fix in following PR.

@@ -56,10 +56,6 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
$manager = $this->managerRegistry->getManagerForClass($entityClass);

$repository = $manager->getRepository($entityClass);
if (!method_exists($repository, 'createQueryBuilder')) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

$repository is always an EntityRepository according to PHPStan and I think it's right.

@@ -65,10 +65,6 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
}

$repository = $manager->getRepository($entityClass);
if (!method_exists($repository, 'createQueryBuilder')) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same idea

@@ -41,6 +41,7 @@ public function testParameterFactory(): void
$filterLocator->method('get')->willReturn(new class implements FilterInterface {
public function getDescription(string $resourceClass): array
{
// @phpstan-ignore-next-line return.type
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems like we're passing volontary a wrong type to check it's handle in the method.

@@ -101,7 +101,7 @@ public function normalize(mixed $object, ?string $format = null, array $context
*/
protected function normalizeRawCollection(iterable $object, ?string $format = null, array $context = []): array|\ArrayObject
{
if (!$object && ($context[Serializer::EMPTY_ARRAY_AS_OBJECT] ?? false) && \is_array($object)) {
if ([] === $object && ($context[Serializer::EMPTY_ARRAY_AS_OBJECT] ?? false)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

!$object already enforce it's [].

But I rewrote the condition to be explicit

@@ -1163,9 +1163,10 @@ private function createAndValidateAttributeValue(string $attribute, mixed $value
}

if (
($t instanceof CollectionType && $collectionValueType instanceof ObjectType && (null !== ($className = $collectionValueType->getClassName())))
|| ($t instanceof LegacyType && $t->isCollection() && null !== $collectionValueType && null !== ($className = $collectionValueType->getClassName()))
($t instanceof CollectionType && $collectionValueType instanceof ObjectType)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this condition (null !== ($className = $collectionValueType->getClassName())) is always true cause getClassName returns a string.

So we need to remove it from the condition, and it force declaring the variable after the if.

@@ -91,9 +91,6 @@ private function normalizeJson(mixed $document): object|array
}

$document = json_encode($document, \JSON_THROW_ON_ERROR);
if (!\is_string($document)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

$document is always a string because of JSON_THROW_ON_ERROR

@@ -47,7 +47,6 @@ public static function getResources(): array
public function testRequest(): void
{
$client = self::createClient();
$client->getKernelBrowser();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Useless call

@VincentLanglet VincentLanglet force-pushed the bumpPhpstan branch 2 times, most recently from 1f70618 to 299f4cb Compare June 20, 2025 16:17
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant