-
-
Notifications
You must be signed in to change notification settings - Fork 375
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
[TypeDeclaration] Adds AddClosureParamTypeFromArgRector & AddClosureParamTypeFromArgClassStringRector rule #6198
Conversation
05a193a
to
5d62403
Compare
5d62403
to
18bc621
Compare
rules/TypeDeclaration/ValueObject/AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration.php
Outdated
Show resolved
Hide resolved
if (($node->name ?? null) === null) { | ||
continue; | ||
} | ||
|
||
if (! $node->name instanceof Identifier) { | ||
continue; | ||
} |
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.
if (($node->name ?? null) === null) { | |
continue; | |
} | |
if (! $node->name instanceof Identifier) { | |
continue; | |
} | |
if (! $node->name instanceof Identifier) { | |
continue; | |
} |
I think this has the same result
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 fails PHPStan instead I've done:
if (! ($node->name ?? null) instanceof Identifier) {
continue;
}
public function __construct( | ||
private string $className, | ||
private string $methodName, | ||
private int|string $callLikePosition, |
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.
Support for int
is enough here
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.
Are you sure? If it's an arg it could be a named one and that might cause an issue for some people. It makes little difference to the rule but make rules more compatible with code.
public function onlyAcceptClassString(): bool | ||
{ | ||
return $this->onlyAcceptClassString; | ||
} |
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.
How useful is this practise? I'd expect type should be filled in any case
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.
I've solved this by just splitting the rules into two unique ones.
The problem was without a mechanism to seperate the two, Class const would mean sometimes you might want a string for a type, it just seemed messy to not be specific
...Rector/FunctionLike/AddParamTypeForFunctionLikeWithinCallLikeArgFromArgDeclarationRector.php
Outdated
Show resolved
Hide resolved
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.
Looks good 👍 I've added few notes
33dd198
to
2615b17
Compare
I'm looking into this 👍 |
I've rebased and tidy up added few changes to improve type detection in #6258 Added few tips I use to write rules 🚀 Cool addition, thank you Peter 👏 |
Changes
AddClosureParamTypeFromArgRector
andAddClosureParamTypeFromArgClassStringRector
rule.AddClosureParamTypeFromArgRector
value object for the rule.AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector
Why
Sometimes you want to apply the type to a FunctionLike Param based on other Args to the parent method, an example in Laravel:
Notes
Now implemented as a single rule that can detect either a class constant string or it can use the type from the argument.