Skip to content

Commit 4d328c9

Browse files
committed
Name the parameters of a magic method's partial application like PHP does
PHP calls a method it does not declare through the __call() or __callStatic() trampoline, (mixed ...$arguments), so the closure of its partial application takes its placeholders as $arguments0, $arguments1..., whatever names @method gives the parameters, and a named placeholder makes creating it throw. Calling the closure with the @method names was accepted although PHP rejects them as unknown. Amp-Thread-ID: https://ampcode.com/threads/T-01a1c541-317f-7386-a2ed-9cbd66178685
1 parent c25af9b commit 4d328c9

9 files changed

Lines changed: 135 additions & 2 deletions

‎src/Reflection/InitializerExprTypeResolver.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,11 @@ public function createFirstClassCallable(
969969
continue;
970970
}
971971
[$parameters, $isVariadic, $returnType, $templateTypeMap, $boundParameterNames] = $partialApplication;
972+
$magicMethodParameterNames = PartialFunctionApplicationParameters::getMagicMethodParameterNames($function, $partialApplicationArgs);
973+
if ($magicMethodParameterNames === null) {
974+
continue;
975+
}
976+
$parameters = self::renameParameters($parameters, $magicMethodParameterNames);
972977
if ($variantOrigin !== null) {
973978
$variantOrigin = $variantOrigin->withBoundParameterNames($boundParameterNames);
974979
}

‎src/Reflection/PartialFunctionApplicationParameters.php‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,42 @@ public static function apply(array $parameters, bool $isVariadic, array $args, a
127127
return [$closureParameters, $isVariadic, $returnType, new TemplateTypeMap($closureTemplateTypes), $boundNames];
128128
}
129129

130+
/**
131+
* PHP calls a method it does not declare through __call() or __callStatic(), so the
132+
* closure of its partial application is one of that trampoline, (mixed ...$arguments):
133+
* the placeholders become $arguments0, $arguments1..., and a named one throws.
134+
*
135+
* @param array<Arg|ArgPlaceholder|VariadicPlaceholder> $args
136+
* @return list<string>|null the names of the closure's first parameters, the ones of its
137+
* positional placeholders; null when creating the closure throws
138+
*/
139+
public static function getMagicMethodParameterNames(FunctionReflection|MethodReflection|null $function, array $args): ?array
140+
{
141+
if (!self::isMagicMethod($function)) {
142+
return [];
143+
}
144+
145+
$names = [];
146+
foreach ($args as $arg) {
147+
if (!$arg instanceof ArgPlaceholder) {
148+
continue;
149+
}
150+
if ($arg->name !== null) {
151+
return null;
152+
}
153+
$names[] = 'arguments' . count($names);
154+
}
155+
156+
return $names;
157+
}
158+
159+
/** @phpstan-assert-if-true MethodReflection $function */
160+
public static function isMagicMethod(FunctionReflection|MethodReflection|null $function): bool
161+
{
162+
return $function instanceof MethodReflection
163+
&& !$function->getDeclaringClass()->hasNativeMethod($function->getName());
164+
}
165+
130166
/**
131167
* @phpstan-assert-if-true TemplateType $type
132168
*/

‎src/Rules/FunctionCallParametersCheck.php‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use PHPStan\Reflection\MethodReflection;
2121
use PHPStan\Reflection\ParameterReflection;
2222
use PHPStan\Reflection\ParametersAcceptor;
23+
use PHPStan\Reflection\PartialFunctionApplicationParameters;
2324
use PHPStan\Reflection\ReflectionProvider;
2425
use PHPStan\Reflection\ResolvedFunctionVariant;
2526
use PHPStan\Rules\Methods\NamedArgumentParameterMethodCallsCollector;
@@ -812,6 +813,23 @@ public function checkPartialApplication(
812813
FunctionReflection|MethodReflection|null $calleeReflection,
813814
): array
814815
{
816+
if (PartialFunctionApplicationParameters::isMagicMethod($calleeReflection)) {
817+
// the __call() trampoline it calls only has a variadic parameter
818+
$errors = [];
819+
foreach ($call->getRawArgs() as $arg) {
820+
if (!$arg instanceof Node\ArgPlaceholder || $arg->name === null) {
821+
continue;
822+
}
823+
$errors[] = RuleErrorBuilder::message(sprintf('Cannot use named placeholder for unknown or variadic parameter $%s.', $arg->name->toString()))
824+
->identifier('argument.unknown')
825+
->line($arg->getStartLine())
826+
->build();
827+
}
828+
if ($errors !== []) {
829+
return $errors;
830+
}
831+
}
832+
815833
// templates are resolved from the bound arguments, as in a call;
816834
// the ones only the placeholders determine stay unresolved
817835
$argTypes = [];

‎tests/PHPStan/Rules/Functions/CallCallablesRuleTest.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,14 @@ public function testPartialFunctionApplicationClosureCalls(): void
561561
'Argument for parameter $a has already been bound by the partial function application.',
562562
60,
563563
],
564+
[
565+
'Missing parameter $arguments0 (string) in call to closure.',
566+
93,
567+
],
568+
[
569+
'Unknown parameter $x in call to closure.',
570+
93,
571+
],
564572
]);
565573
}
566574

‎tests/PHPStan/Rules/Functions/data/partial-function-application-closure-calls.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,24 @@ function forwardedArguments(): void
7272
// the variadic placeholder forwards the extra arguments to func_get_args()
7373
forwardsToFuncGetArgs(?, ...)(1, 2, 3);
7474
}
75+
76+
/**
77+
* @method int magic(string $x, int $y)
78+
*/
79+
class Magic
80+
{
81+
82+
/** @param array<mixed> $arguments */
83+
public function __call(string $name, array $arguments): int
84+
{
85+
return 1;
86+
}
87+
88+
}
89+
90+
function magicMethods(Magic $magic): void
91+
{
92+
// a method called through __call() takes its placeholders as $arguments0, $arguments1...
93+
$magic->magic(?, 1)(x: 'a');
94+
$magic->magic(?, ?)(arguments0: 'a', arguments1: 1);
95+
}

‎tests/PHPStan/Rules/Methods/MethodCallableRuleTest.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,13 @@ public function testPartialFunctionApplicationArguments(): void
140140
'Cannot combine partial application and unpacking.',
141141
43,
142142
],
143+
[
144+
'Cannot use named placeholder for unknown or variadic parameter $x.',
145+
72,
146+
],
143147
[
144148
'Variadic placeholder may only appear once.',
145-
50,
149+
78,
146150
],
147151
]);
148152
}

‎tests/PHPStan/Rules/Methods/StaticMethodCallableRuleTest.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,13 @@ public function testPartialFunctionApplicationArguments(): void
163163
'Cannot combine partial application and unpacking.',
164164
45,
165165
],
166+
[
167+
'Cannot use named placeholder for unknown or variadic parameter $y.',
168+
73,
169+
],
166170
[
167171
'Variadic placeholder may only appear once.',
168-
51,
172+
79,
169173
],
170174
]);
171175
}

‎tests/PHPStan/Rules/Methods/data/partial-function-application-method-arguments.php‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,34 @@ function placementsWithUnknownCallees(Foo $foo, array $ints): void
4545
Foo::undefinedStatic(?, ...$ints);
4646
}
4747

48+
/**
49+
* @method int magic(string $x, int $y)
50+
* @method static int staticMagic(string $x, int $y)
51+
*/
52+
class Magic
53+
{
54+
55+
/** @param array<mixed> $arguments */
56+
public function __call(string $name, array $arguments): int
57+
{
58+
return 1;
59+
}
60+
61+
/** @param array<mixed> $arguments */
62+
public static function __callStatic(string $name, array $arguments): int
63+
{
64+
return 1;
65+
}
66+
67+
}
68+
69+
function magicMethods(Magic $magic): void
70+
{
71+
// the __call() trampoline only has a variadic parameter
72+
$magic->magic(x: ?);
73+
Magic::staticMagic(?, y: ?);
74+
}
75+
4876
function variadicPlaceholderPlacements(Foo $foo): void
4977
{
5078
$foo->doFoo(?, ..., ...);

‎turbo-ext/src/InitializerExprTypeResolver.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2543,6 +2543,15 @@ class InitializerExprTypeResolver
25432543
isVariadic = zend_is_true(appliedIsVariadic);
25442544
returnType = zv::Val::copyOf(zv::Ref(appliedReturnType));
25452545
templateTypeMap = zv::Val::copyOf(zv::Ref(appliedTemplateTypeMap));
2546+
static pt_method_site magicNamesSite;
2547+
zv::Args magicNamesArgv{function, partialApplicationArgs};
2548+
IETR_VAL(magicMethodParameterNames, pt_call_static_cached(magicNamesSite, PT_CLASS_PARTIAL_FUNCTION_APPLICATION_PARAMETERS, PT_LC("getmagicmethodparameternames"), 2, magicNamesArgv));
2549+
if (Z_TYPE_P(magicMethodParameterNames.raw()) == IS_NULL) {
2550+
// creating the closure of a method called through __call() throws
2551+
continue;
2552+
}
2553+
IETR_VAL(renamedForMagicMethod, renameParameters(parameters.raw(), magicMethodParameterNames.raw()));
2554+
parameters = std::move(renamedForMagicMethod);
25462555
if (Z_TYPE_P(variantOrigin.raw()) == IS_OBJECT) {
25472556
IETR_VAL(withBoundNames, pt_type_call(Z_OBJ_P(variantOrigin.raw()), PT_LC("withboundparameternames"), 1, appliedBoundParameterNames));
25482557
variantOrigin = std::move(withBoundNames);

0 commit comments

Comments
 (0)