feat: add CallLike::getArg() method#1089
Conversation
|
I don't think this logic is correct if you have something like |
b12eeb9 to
1ee4b02
Compare
|
@nikic, that is illegal: https://3v4l.org/Prr6W |
1ee4b02 to
d4f040e
Compare
|
@calebdw True, but I still think you have to consider unpacks in general. Let's turn it around and say |
d4f040e to
adc049c
Compare
|
Ah that makes sense, I've updated the logic to skip over unpacks. For the $arg = $node->getArg('bar', 1); // 0 based
$arg = $node->getArg('bar', 2); // 1 basedAlso, it looks like the last ci failure wasn't caused by my changes? |
This method returns the named argument that matches the given `$name`, or the positional (unnamed) argument that exists at the given `$position`, otherwise, returns `null` for first-class callables or if no match is found.
adc049c to
a1dd70e
Compare
|
@nikic, just wanted to touch base---do you think this will suffice? |
|
Thanks! |
| public function getArg(string $name, int $position): ?Arg { | ||
| if ($this->isFirstClassCallable()) { | ||
| return null; | ||
| } | ||
| foreach ($this->getRawArgs() as $i => $arg) { | ||
| if ($arg->unpack) { | ||
| continue; | ||
| } |
There was a problem hiding this comment.
@calebdw this seems cause error when using array_map flipped arg:
$result = array_map(array: $items, callback: fn ($item) => $item * 2);Tested on rector, got error:
assert($itemStartPos >= 0 && $itemEndPos >= 0 && $itemStartPos >= $pos)
/Users/samsonasik/www/rector-src/vendor/nikic/php-parser/lib/PhpParser/PrettyPrinterAbstract.php:842seems because array is unpack, array
The signature is:
function array_map(?callable $callback, array $array, array ...$arrays): array { }/cc @TomasVotruba
There was a problem hiding this comment.
PHPStan has a ArgumentsNormalizer for args reordering
There was a problem hiding this comment.
There was a problem hiding this comment.
@staabm I will check if this can be handled on nikic/php-parser itself without re-order argument, as on specific rule I included above, it nothing todo with ordering argument task.
There was a problem hiding this comment.
The issue seems probably on Printer, as it shown on the stack trace above when run phpunit, while on rector demo, it printed without error, just show repetitive that cause invalid result
https://getrector.com/demo/104834e1-c8bd-4266-99f5-361581e44448
There was a problem hiding this comment.
I checked even without getArg() call, the error still exists on Printer, so it nothing todo with the getArg() method.
So the bug seems somewhere in the Printer.
There was a problem hiding this comment.
If it know about the information on named argument and its unpacked on print, it seems the issue on Internal PHPStan printer...
Hello!
This method returns the named argument that matches the given
$name, or the positional (unnamed) argument that exists at the given$position, otherwise, returnsnullfor first-class callables or if no match is found.Motivation
I found myself duplicating this logic over and over again, so thought it would be a good idea to just PR it.
For a given CallLike, I often need to target a particular parameter. Take for example this function signature:
and let's say that I need to do something with
$bar(which has namebarand is at position1) if it's provided in the call. Now I can simply use:which would return the following:
Thanks!