Skip to content

Add missing type to solve PHPStan errors#6979

Merged
javiereguiluz merged 1 commit intoEasyCorp:4.xfrom
VincentLanglet:addTypes
Jun 11, 2025
Merged

Add missing type to solve PHPStan errors#6979
javiereguiluz merged 1 commit intoEasyCorp:4.xfrom
VincentLanglet:addTypes

Conversation

@VincentLanglet
Copy link
Contributor

This solve ~30 PHPStan error of level 6 which represent 10% of missing types.

I started with a small PR to validate it's the way you want to solve them (and you agree it's BC).

'nullable_type_declaration_for_default_null_value' => true,
'phpdoc_to_comment' => false,
// Override @Symfony ruleset to keep mixed return type for PHPStan
'no_superfluous_phpdoc_tags' => ['allow_hidden_params' => true, 'allow_mixed' => true, 'remove_inheritdoc' => 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.

Without this, php-cs-fixer removes

@param mixed $foo

or

@return mixed

Which is needed for phpstan.

private array $numberFormatters = [];

public function formatCurrency($amount, string $currency, array $attrs = [], ?string $locale = null): string
public function formatCurrency(int|float $amount, string $currency, array $attrs = [], ?string $locale = null): string
Copy link
Contributor Author

Choose a reason for hiding this comment

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

  • Class is final
  • The interface ask for int|float
  • $formatter->formatCurrency need int|float

So it's ok to type amount.

}

/**
* @param \DateTimeZone|string|bool|null $timezone $timezone
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Param is coming from formatDateTime which only use phpdoc so I have to do the same

* @param mixed[] &$array
*/
private function recursiveKsort(&$array): void
private function recursiveKsort(array &$array): void
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's always called with an array

Comment on lines +27 to +28
private ?iterable $results = null;
private ?int $numResults = null;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have

    public function getNumResults(): int
    {
        return $this->numResults;
    }

    public function getResults(): ?iterable
    {
        return $this->results;
    }

So I'm not sure with which value I have to initialize the $numResult (there will a be a ?int vs int level 8) but I copied the existing behavior (without type).

}

public function updateProperty(EntityDto $entityDto, string $propertyName, $value): void
public function updateProperty(EntityDto $entityDto, string $propertyName, mixed $value): void
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far I known, adding mixed as param type is BC even if the class is not final.

}

public function isGranted($permission, $subject = null, ?AccessDecision $accessDecision = null): bool
public function isGranted(mixed $attribute, mixed $subject = null, ?AccessDecision $accessDecision = null): bool
Copy link
Contributor Author

Choose a reason for hiding this comment

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

$attribute is the param name of the parent.

* Transforms ['a' => 'foo', 'b' => ['c' => ['d' => 7]]] into ['a' => 'foo', 'b[c][d]' => 7]
* It's useful to submit nested arrays (e.g. query string parameters) as form fields.
*
* @param mixed[] $array
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To stay BC, I only added phpdoc

@VincentLanglet VincentLanglet marked this pull request as ready for review June 9, 2025 21:53
@VincentLanglet VincentLanglet mentioned this pull request Jun 9, 2025
@javiereguiluz javiereguiluz added this to the 4.x milestone Jun 10, 2025
Copy link
Collaborator

@javiereguiluz javiereguiluz left a comment

Choose a reason for hiding this comment

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

Thanks a lot Vincent.

I like this a lot and, after reviewing it, I think it's correct (including the new mixed types). I'll wait a bit more before merging this in case someone else wants to review it.

Thanks!

@javiereguiluz
Copy link
Collaborator

Merged! Thanks.

@javiereguiluz javiereguiluz merged commit 1d8b03a into EasyCorp:4.x Jun 11, 2025
15 checks passed
javiereguiluz added a commit that referenced this pull request Jun 17, 2025
This PR was squashed before being merged into the 4.x branch.

Discussion
----------

Add typing for PHPStan level 6

Follow up of #6979

After this PR it will miss 60 types, I'll do them in a last PR.

Commits
-------

12eb330 Add typing for PHPStan level 6
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