Skip to content

Commit 99f760f

Browse files
phpstan-botclaude
authored andcommitted
Extract ArgumentsNormalizer::getArgByPositionOrName()
The intrinsic-override parser visitors each spelled out the same loop to find the argument bound to a parameter by position or by name. The native ArgumentsNormalizer mirrors the new method via visitors::argForParameter(). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ba0cf49 commit 99f760f

9 files changed

Lines changed: 69 additions & 42 deletions

‎src/Analyser/ArgumentsNormalizer.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,23 @@ public static function reorderNewArguments(
293293
);
294294
}
295295

296+
/**
297+
* The argument bound to the parameter at $position named $name: the
298+
* positional argument at that index or the one passed by that name.
299+
*
300+
* @param Arg[] $args
301+
*/
302+
public static function getArgByPositionOrName(array $args, int $position, string $name): ?Arg
303+
{
304+
foreach ($args as $i => $arg) {
305+
if ($arg->name === null ? $i === $position : $arg->name->toString() === $name) {
306+
return $arg;
307+
}
308+
}
309+
310+
return null;
311+
}
312+
296313
/**
297314
* @param Arg[] $callArgs
298315
* @return ?list<Arg>

‎src/Parser/ArrayFilterArgVisitor.php‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Override;
66
use PhpParser\Node;
77
use PhpParser\NodeVisitorAbstract;
8+
use PHPStan\Analyser\ArgumentsNormalizer;
89
use PHPStan\DependencyInjection\AutowiredService;
910
use PHPStan\Turbo\ShadowedByTurboExtension;
1011

@@ -21,13 +22,9 @@ public function enterNode(Node $node): ?Node
2122
if ($node instanceof Node\Expr\FuncCall && $node->name instanceof Node\Name && !$node->isFirstClassCallable()) {
2223
$functionName = $node->name->toLowerString();
2324
if ($functionName === 'array_filter') {
24-
foreach ($node->getArgs() as $i => $arg) {
25-
if ($arg->name === null ? $i !== 0 : $arg->name->toString() !== 'array') {
26-
continue;
27-
}
28-
25+
$arg = ArgumentsNormalizer::getArgByPositionOrName($node->getArgs(), 0, 'array');
26+
if ($arg !== null) {
2927
$arg->setAttribute(self::ATTRIBUTE_NAME, true);
30-
break;
3128
}
3229
}
3330
}

‎src/Parser/ArrayFindArgVisitor.php‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Override;
66
use PhpParser\Node;
77
use PhpParser\NodeVisitorAbstract;
8+
use PHPStan\Analyser\ArgumentsNormalizer;
89
use PHPStan\DependencyInjection\AutowiredService;
910
use PHPStan\Turbo\ShadowedByTurboExtension;
1011
use function in_array;
@@ -22,13 +23,9 @@ public function enterNode(Node $node): ?Node
2223
if ($node instanceof Node\Expr\FuncCall && $node->name instanceof Node\Name && !$node->isFirstClassCallable()) {
2324
$functionName = $node->name->toLowerString();
2425
if (in_array($functionName, ['array_all', 'array_any', 'array_find', 'array_find_key'], true)) {
25-
foreach ($node->getArgs() as $i => $arg) {
26-
if ($arg->name === null ? $i !== 0 : $arg->name->toString() !== 'array') {
27-
continue;
28-
}
29-
26+
$arg = ArgumentsNormalizer::getArgByPositionOrName($node->getArgs(), 0, 'array');
27+
if ($arg !== null) {
3028
$arg->setAttribute(self::ATTRIBUTE_NAME, true);
31-
break;
3229
}
3330
}
3431
}

‎src/Parser/ArrayWalkArgVisitor.php‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Override;
66
use PhpParser\Node;
77
use PhpParser\NodeVisitorAbstract;
8+
use PHPStan\Analyser\ArgumentsNormalizer;
89
use PHPStan\DependencyInjection\AutowiredService;
910
use PHPStan\Turbo\ShadowedByTurboExtension;
1011

@@ -21,13 +22,9 @@ public function enterNode(Node $node): ?Node
2122
if ($node instanceof Node\Expr\FuncCall && $node->name instanceof Node\Name && !$node->isFirstClassCallable()) {
2223
$functionName = $node->name->toLowerString();
2324
if ($functionName === 'array_walk') {
24-
foreach ($node->getArgs() as $i => $arg) {
25-
if ($arg->name === null ? $i !== 0 : $arg->name->toString() !== 'array') {
26-
continue;
27-
}
28-
25+
$arg = ArgumentsNormalizer::getArgByPositionOrName($node->getArgs(), 0, 'array');
26+
if ($arg !== null) {
2927
$arg->setAttribute(self::ATTRIBUTE_NAME, true);
30-
break;
3128
}
3229
}
3330
}

‎src/Parser/CurlSetOptArgVisitor.php‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Override;
66
use PhpParser\Node;
77
use PhpParser\NodeVisitorAbstract;
8+
use PHPStan\Analyser\ArgumentsNormalizer;
89
use PHPStan\DependencyInjection\AutowiredService;
910
use PHPStan\Turbo\ShadowedByTurboExtension;
1011

@@ -21,13 +22,9 @@ public function enterNode(Node $node): ?Node
2122
if ($node instanceof Node\Expr\FuncCall && $node->name instanceof Node\Name && !$node->isFirstClassCallable()) {
2223
$functionName = $node->name->toLowerString();
2324
if ($functionName === 'curl_setopt') {
24-
foreach ($node->getArgs() as $i => $arg) {
25-
if ($arg->name === null ? $i !== 0 : $arg->name->toString() !== 'handle') {
26-
continue;
27-
}
28-
25+
$arg = ArgumentsNormalizer::getArgByPositionOrName($node->getArgs(), 0, 'handle');
26+
if ($arg !== null) {
2927
$arg->setAttribute(self::ATTRIBUTE_NAME, true);
30-
break;
3128
}
3229
}
3330
}

‎src/Parser/CurlSetOptArrayArgVisitor.php‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Override;
66
use PhpParser\Node;
77
use PhpParser\NodeVisitorAbstract;
8+
use PHPStan\Analyser\ArgumentsNormalizer;
89
use PHPStan\DependencyInjection\AutowiredService;
910
use PHPStan\Turbo\ShadowedByTurboExtension;
1011

@@ -21,13 +22,9 @@ public function enterNode(Node $node): ?Node
2122
if ($node instanceof Node\Expr\FuncCall && $node->name instanceof Node\Name && !$node->isFirstClassCallable()) {
2223
$functionName = $node->name->toLowerString();
2324
if ($functionName === 'curl_setopt_array') {
24-
foreach ($node->getArgs() as $i => $arg) {
25-
if ($arg->name === null ? $i !== 1 : $arg->name->toString() !== 'options') {
26-
continue;
27-
}
28-
25+
$arg = ArgumentsNormalizer::getArgByPositionOrName($node->getArgs(), 1, 'options');
26+
if ($arg !== null) {
2927
$arg->setAttribute(self::ATTRIBUTE_NAME, true);
30-
break;
3128
}
3229
}
3330
}

‎turbo-ext/src/ArgumentsNormalizer.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace sigs = ptdecl::ArgumentsNormalizer::sig;
2424
#include "Engine.h"
2525
#include "ParameterValues.h"
2626
#include "AcceptorValues.h"
27+
#include "ParserVisitors.h"
2728

2829
zend_class_entry *pt_ce_arguments_normalizer = nullptr;
2930

@@ -871,6 +872,20 @@ PT_MINIT_REGISTRATION(pt_register_arguments_normalizer)
871872
PT_RETURN_VAL(ArgumentsNormalizer::reorderArgs(parametersAcceptor, callArgs));
872873
});
873874

875+
cls.method(sigs::getArgByPositionOrName, [](INTERNAL_FUNCTION_PARAMETERS) {
876+
zval *args;
877+
zend_long position;
878+
zend_string *name;
879+
ZEND_PARSE_PARAMETERS_START(3, 3)
880+
Z_PARAM_ARRAY(args)
881+
Z_PARAM_LONG(position)
882+
Z_PARAM_STR(name)
883+
ZEND_PARSE_PARAMETERS_END();
884+
zend_object *arg = position < 0 ? NULL : phpstanturbo::visitors::argForParameter(args, (zend_ulong) position, ZSTR_VAL(name), ZSTR_LEN(name));
885+
if (arg == NULL) RETURN_NULL();
886+
RETURN_OBJ_COPY(arg);
887+
});
888+
874889
cls.shadow(&pt_ce_arguments_normalizer);
875890
}
876891

‎turbo-ext/src/ParserVisitors.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,9 @@ inline zend_string *plainFuncCallName(zend_object *node, FuncCallProps &props, N
230230

231231
/*
232232
* The argument bound to the parameter at `position` named `name` — the
233-
* positional argument at that index or the one passed by that name — the
234-
* `$arg->name === null ? $i === $position : $arg->name->toString() === $name`
235-
* loop the twins spell out. NULL when no argument is bound to it.
233+
* positional argument at that index or the one passed by that name. Mirrors
234+
* ArgumentsNormalizer::getArgByPositionOrName(). NULL when no argument is
235+
* bound to it.
236236
*/
237237
inline zend_object *argForParameter(zval *args, zend_ulong position, const char *name, size_t nameLen)
238238
{

‎turbo-ext/src/generated/ArgumentsNormalizer.h‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,16 @@ inline constexpr char strings[] =
4343
"new\0" /* 386 */
4444
"PhpParser\\Node\\Expr\\New_\0" /* 390 */
4545
"reorderNewArguments\0" /* 415 */
46-
"callArgs\0" /* 435 */
47-
"reorderArgs\0" /* 444 */
48-
"node\0" /* 456 */
49-
"PhpParser\\Node\0" /* 461 */
50-
"attributesWithoutPrintedForm"; /* 476 */
46+
"args\0" /* 435 */
47+
"position\0" /* 440 */
48+
"name\0" /* 449 */
49+
"PhpParser\\Node\\Arg\0" /* 454 */
50+
"getArgByPositionOrName\0" /* 473 */
51+
"callArgs\0" /* 496 */
52+
"reorderArgs\0" /* 505 */
53+
"node\0" /* 517 */
54+
"PhpParser\\Node\0" /* 522 */
55+
"attributesWithoutPrintedForm"; /* 537 */
5156
inline constexpr reg::PackedArg args[] = {
5257
reg::packed(0, 0, 17), /* reorderCallUserFuncArguments $callUserFuncCall */
5358
reg::packed(46, 0, 52), /* reorderCallUserFuncArguments $scope */
@@ -67,10 +72,14 @@ inline constexpr reg::PackedArg args[] = {
6772
reg::packed(161, 0, 180), /* reorderNewArguments $parametersAcceptor */
6873
reg::packed(386, 0, 390), /* reorderNewArguments $new */
6974
reg::packed(75, MAY_BE_NULL, 390), /* reorderNewArguments return */
75+
reg::packed(435, MAY_BE_ARRAY), /* getArgByPositionOrName $args */
76+
reg::packed(440, MAY_BE_LONG), /* getArgByPositionOrName $position */
77+
reg::packed(449, MAY_BE_STRING), /* getArgByPositionOrName $name */
78+
reg::packed(75, MAY_BE_NULL, 454), /* getArgByPositionOrName return */
7079
reg::packed(161, 0, 180), /* reorderArgs $parametersAcceptor */
71-
reg::packed(435, MAY_BE_ARRAY), /* reorderArgs $callArgs */
80+
reg::packed(496, MAY_BE_ARRAY), /* reorderArgs $callArgs */
7281
reg::packed(75, MAY_BE_NULL | MAY_BE_ARRAY), /* reorderArgs return */
73-
reg::packed(456, 0, 461), /* attributesWithoutPrintedForm $node */
82+
reg::packed(517, 0, 522), /* attributesWithoutPrintedForm $node */
7483
reg::packed(75, MAY_BE_ARRAY), /* attributesWithoutPrintedForm return */
7584
};
7685
using Sig = reg::Sig<strings, args>;
@@ -84,8 +93,9 @@ inline constexpr sigtab::Sig reorderFuncArguments = { { 231 /* reorderFuncArgume
8493
inline constexpr sigtab::Sig reorderMethodArguments = { { 294 /* reorderMethodArguments */, 2, 9, 2, 11, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC } };
8594
inline constexpr sigtab::Sig reorderStaticCallArguments = { { 359 /* reorderStaticCallArguments */, 2, 12, 2, 14, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC } };
8695
inline constexpr sigtab::Sig reorderNewArguments = { { 415 /* reorderNewArguments */, 2, 15, 2, 17, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC } };
87-
inline constexpr sigtab::Sig reorderArgs = { { 444 /* reorderArgs */, 2, 18, 2, 20, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC } };
88-
inline constexpr sigtab::Sig attributesWithoutPrintedForm = { { 476 /* attributesWithoutPrintedForm */, 1, 21, 1, 22, ZEND_ACC_PRIVATE | ZEND_ACC_STATIC } };
96+
inline constexpr sigtab::Sig getArgByPositionOrName = { { 473 /* getArgByPositionOrName */, 3, 18, 3, 21, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC } };
97+
inline constexpr sigtab::Sig reorderArgs = { { 505 /* reorderArgs */, 2, 22, 2, 24, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC } };
98+
inline constexpr sigtab::Sig attributesWithoutPrintedForm = { { 537 /* attributesWithoutPrintedForm */, 1, 25, 1, 26, ZEND_ACC_PRIVATE | ZEND_ACC_STATIC } };
8999
} // namespace sig
90100

91101
} // namespace ptdecl::ArgumentsNormalizer

0 commit comments

Comments
 (0)