Skip to content

chore: introduce phpstan level 6#7269

Merged
soyuka merged 5 commits into
api-platform:mainfrom
VincentLanglet:phpstan6
Jul 3, 2025
Merged

chore: introduce phpstan level 6#7269
soyuka merged 5 commits into
api-platform:mainfrom
VincentLanglet:phpstan6

Conversation

@VincentLanglet

@VincentLanglet VincentLanglet commented Jul 1, 2025

Copy link
Copy Markdown
Contributor
Q A
Branch? main for features
Tickets Closes #..., closes #...
License MIT
Doc PR api-platform/docs#...

Comment thread phpstan.neon.dist
Comment on lines +118 to +120
identifier: missingType.iterableValue
-
identifier: missingType.generics

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.

iterableValue and generics are creating a lot of error, so I prefer to fix missing param/property/return type first

Comment thread phpstan.neon.dist
-
identifier: missingType.property
paths:
- src/Doctrine/Common/Tests

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.

I ignored all the tests folder.

Comment thread phpstan.neon.dist
- src/Symfony/Bundle/Test
- src/Symfony/Tests
- tests
- src # TODO

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.

Parameter is ~150 error, I'll do it in the next PR.

Comment thread phpstan.neon.dist
paths:
- src/Symfony/Bundle/Test
- tests
- src # TODO

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's around 50 error, I'll try in another PR.

}

private function buildArrayValue(?\SimpleXMLElement $resource, string $key, mixed $default = null)
private function buildArrayValue(?\SimpleXMLElement $resource, string $key): ?array

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.

default is never used

@VincentLanglet

Copy link
Copy Markdown
Contributor Author

@soyuka The PHPStan error seems legit to me

 ------ ----------------------------------------------------------------------- 
  Line   Exception/ErrorHandler.php                                             
 ------ ----------------------------------------------------------------------- 
  80     Argument of an invalid type array<int|string,                          
         array<string>|string>|string supplied for foreach, only iterables are  
         supported.                                                             
         🪪 foreach.nonIterable 

HttpOperation accepts the following param

     * @param array<int|string, string|string[]>|string|null $formats       {@see https://api-platform.com/docs/core/content-negotiation/#configuring-formats-for-a-specific-resource-or-operation}
     * @param array<int|string, string|string[]>|string|null $inputFormats  {@see https://api-platform.com/docs/core/content-negotiation/#configuring-formats-for-a-specific-resource-or-operation}
     * @param array<int|string, string|string[]>|string|null $outputFormats

but then we're doing foreach on it.

If someone pass a string instead of string[] you'll get a weird behavior.
Not sure what solution you prefer:

  • Casting the format into an array (in the constructor ? in the getter ?)
  • Casting the format when iterating on it (but it's not the only place)
  • Updating the param to only accept array|null and not string

@soyuka

soyuka commented Jul 1, 2025

Copy link
Copy Markdown
Member

Indeed, to me we should cast the string into an array, the foreach should be changed to op->getOutputFormats() ?? null and the getter should only return:

list<string>, array<string, list[string]>|null

@VincentLanglet VincentLanglet marked this pull request as ready for review July 1, 2025 15:51
$uriVariables = [];
foreach ($operation->getUriVariables() ?? [] as $parameterName => $_) {
$parameter = $request->route($parameterName);
$parameter = $request->route((string) $parameterName);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

mhh actually this is weird, it's a hard problem though maybe we'll leave that for later ^^

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.

There is an error saying that route is called with int|string instead of string, so I casted the value.
This value is also casted on the following line.

You prefer a phpstan-ignore-line ?

*
* @return string[]
*/
public function getIris()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

can we change the return type to ?array as well?

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.

Indeed, done with c29eb43

Comment thread src/Metadata/ApiProperty.php Outdated
*
* @return bool
*/
public function getGenId()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

same we can return ?bool here

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.

Indeed, done with c29eb43

@soyuka soyuka merged commit cb60937 into api-platform:main Jul 3, 2025
113 of 114 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants