-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
Small tweaks for php generator, PHPStan level 3, Psalm level 7 #7616
Conversation
@@ -307,6 +314,7 @@ class ObjectSerializer | |||
} | |||
} | |||
|
|||
/** @psalm-suppress ParadoxicalCondition */ |
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.
This one is interesting, Psalm found it's nonsensical since it includes object
, which is already handled a few lines above, meaning this branch can never be reached with that specific "class".
This introduces the first tool-specific annotation, but I believe it's not a big issue since Doctrine and PHPUnit already use them natively.
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.
See example what it found here: https://psalm.dev/r/1035490c06
@@ -159,8 +165,9 @@ class ObjectSerializer | |||
*/ | |||
public static function toHeaderValue($value) | |||
{ | |||
if (method_exists($value, 'toHeaderValue')) { | |||
return $value->toHeaderValue(); | |||
$callable = [$value, 'toHeaderValue']; |
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.
Using is_callable
is safer than using method_exists
, while also being more correct.
@ybelenko this one might be more easy to digest. 😄 |
Travis CI reports the following errors:
Please take a look when you've time. For the CircleCI failure, it has been resolved in the master so please merge the latest master into this branch. |
@wing328 oh, great news we can rely on tests again. Will take a look ASAP. |
The issue was fixed, I'm still getting
but I guess this is expected since it's failing consistently to master. |
Just checked the readme and all CI tests passed. (the CI tests against a local Petstore server instead of using the public one) |
@wing328 OK, do we merge here? |
&& !in_array($value, $openAPIType::getAllowableEnumValues(), true)) { | ||
$imploded = implode("', '", $openAPIType::getAllowableEnumValues()); | ||
throw new \InvalidArgumentException("Invalid value for enum '$openAPIType', must be one of: '$imploded'"); | ||
if ($value !== null && !in_array($openAPIType, ['DateTime', 'bool', 'boolean', 'byte', 'double', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)) { |
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.
@dkarlovi So... instead of one big condition you decided to use three nested conditions? It's weird that PHPStan doesn't complain about that. It usually says to me "consider refactoring" when I have the third nested condition. 😕
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.
@ybelenko the nested ifs are equivalent to the original single if, the difference is the safer types, in this case, $callable
is really callable
, using it instead of method_exists
, see my other comment with links.
IMO the serializer should in the future likely be extracted from the package anyway since it's static and has tests which are outside of the generated package (hinting at its static nature). Refactoring it further until it's clean enough to split apart / replace is a good direction here.
if ($value !== null && !in_array($openAPIType, ['DateTime', 'bool', 'boolean', 'byte', 'double', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)) { | ||
$callable = [$openAPIType, 'getAllowableEnumValues']; | ||
if (is_callable($callable)) { | ||
/** array $callable */ |
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.
Oh no, copy paste. 🙈
Should be
/** array $allowedEnumTypes */
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.
Oh no, copy paste. 🙈
Should be
/** array $allowedEnumTypes */
Could you fix it in next PR?
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.
Didn't check locally, lgtm.
OK, great, I have some tweaks for the Markdown files next, used |
Summary
This is a followup to #7376, #7528, trying to get the generated code quality up to a standard where strong static analysis tools like PHPStan, Psalm can be used to verify the generated output in CI.
This reaches level 3 for PHPStan (0 is most lax, 8 is most strict), level 7 for Psalm (8 is most lax, 1 is most strict).
PR checklist
./bin/generate-samples.sh
to update all Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example./bin/generate-samples.sh bin/configs/java*
. For Windows users, please run the script in Git BASH.master
@jebentier (2017/07), @dkarlovi (2017/07), @mandrean (2017/08), @jfastnacht (2017/09), @ackintosh (2017/09) heart, @ybelenko (2018/07), @renepardon (2018/12)