Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
866af5a
Infer the closure signature of partial function applications
ondrejmirtes Nov 10, 2026
5242dbb
Report partial function application arguments that do not fit the callee
ondrejmirtes Nov 10, 2026
eb3ea73
Say that a first-class callable of compact() throws when called
ondrejmirtes Nov 10, 2026
b5d152f
Resolve the templates a partial function application binds
ondrejmirtes Nov 10, 2026
1194ccd
Test partial function applications passed as callbacks
ondrejmirtes Nov 10, 2026
0e1ee9c
Replay calls of closures created from callables on their callee
ondrejmirtes Nov 10, 2026
831dec5
Report a named argument for a parameter a partial application bound
ondrejmirtes Nov 10, 2026
32098af
Take the throw points of a closure call from its replayed callee
ondrejmirtes Nov 10, 2026
bd8f6f7
Test first-class callables where the suite did not cover them
ondrejmirtes Nov 10, 2026
9c22850
Accept a first-class callable of a multi-variant callee when any vari…
ondrejmirtes Nov 11, 2026
747548e
Mark closures of static methods and partial applications of functions…
ondrejmirtes Nov 11, 2026
67a98ae
Report placeholder placements PHP refuses to compile whatever the cal…
ondrejmirtes Nov 11, 2026
8cd32a1
Give Closure::__invoke() the signature of the closure it is called on
ondrejmirtes Nov 11, 2026
fb7e9db
Infer null from unbinding the closure of an instance method
ondrejmirtes Nov 11, 2026
c25af9b
Keep a partial application with ... variadic when its callee is
ondrejmirtes Nov 11, 2026
4d328c9
Name the parameters of a magic method's partial application like PHP …
ondrejmirtes Nov 11, 2026
07f6786
Check a partial application no variant fits against the closest variant
ondrejmirtes Nov 13, 2026
b53bb1b
Test placeholder misuse in instantiations, nullsafe calls and by-ref …
ondrejmirtes Nov 13, 2026
3295957
Report leaving out a parameter whose default PHP does not know
ondrejmirtes Nov 13, 2026
a16fa0b
Resolve named arguments of multi-variant built-ins against their php-…
ondrejmirtes Nov 13, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ lint:
--exclude tests/PHPStan/Rules/Properties/data/intersection-types.php \
--exclude tests/PHPStan/Rules/Classes/data/first-class-instantiation-callable.php \
--exclude tests/PHPStan/Rules/Classes/data/instantiation-callable.php \
--exclude tests/PHPStan/Rules/Classes/data/instantiation-partial-function-application.php \
--exclude tests/PHPStan/Rules/Classes/data/bug-9402.php \
--exclude tests/PHPStan/Rules/Constants/data/class-as-class-constant.php \
--exclude tests/PHPStan/Rules/Constants/data/value-assigned-to-class-constant-native-type.php \
Expand Down
9 changes: 8 additions & 1 deletion build/collision-detector.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
"../tests/PHPStan/Rules/DeadCode/data/partial-function-application-bound-args.php",
"../tests/PHPStan/Rules/Exceptions/data/dead-catch-partial-function-application.php",
"../tests/PHPStan/Rules/Functions/data/partial-function-application-bound-args.php",
"../tests/PHPStan/Rules/Pure/data/partial-function-application-pure.php"
"../tests/PHPStan/Rules/Pure/data/partial-function-application-pure.php",
"../tests/PHPStan/Analyser/nsrt/partial-function-application-signature.php",
"../tests/PHPStan/Rules/Functions/data/partial-function-application-closure-calls.php",
"../tests/PHPStan/Rules/Functions/data/partial-function-application-arguments.php",
"../tests/PHPStan/Rules/Methods/data/partial-function-application-method-arguments.php",
"../tests/PHPStan/Rules/Functions/data/partial-function-application-callbacks.php",
"../tests/PHPStan/Analyser/nsrt/partial-function-application-replay.php",
"../tests/PHPStan/Rules/Classes/data/instantiation-partial-function-application.php"
]
}
29 changes: 15 additions & 14 deletions build/stubs/PhpParserExpr.stub
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace PhpParser\Node\Expr;
use PhpParser\Node;
use PhpParser\NodeAbstract;
use PhpParser\Node\Arg;
use PhpParser\Node\ArgPlaceholder;
use PhpParser\Node\Expr;
use PhpParser\Node\Identifier;
use PhpParser\Node\VariadicPlaceholder;
Expand All @@ -13,90 +14,90 @@ use PhpParser\Node\FunctionLike;
abstract class CallLike extends Expr {
/**
* Return raw arguments, which may be actual Args, or VariadicPlaceholders for first-class
* callables.
* callables, or ArgPlaceholders for partial function application.
*
* @return list<Arg|VariadicPlaceholder>
* @return list<Arg|VariadicPlaceholder|ArgPlaceholder>
*/
abstract public function getRawArgs(): array;
/**
* Assert that this is not a first-class callable and return only ordinary Args.
* Assert that this is not a partial function application and return only ordinary Args.
*
* @return list<Arg>
*/
public function getArgs(): array {
assert(!$this->isFirstClassCallable());
assert(!$this->isPartialFunctionApplication());
return $this->getRawArgs();
}
}

class FuncCall extends CallLike {
/** @var list<Node\Arg|Node\VariadicPlaceholder> Arguments */
/** @var list<Node\Arg|Node\VariadicPlaceholder|Node\ArgPlaceholder> Arguments */
public array $args;

/**
* Constructs a function call node.
*
* @param Node\Name|Expr $name Function name
* @param list<Node\Arg|Node\VariadicPlaceholder> $args Arguments
* @param list<Node\Arg|Node\VariadicPlaceholder|Node\ArgPlaceholder> $args Arguments
* @param array<string, mixed> $attributes Additional attributes
*/
public function __construct(Node $name, array $args = [], array $attributes = []) {}
}

class MethodCall extends CallLike {
/** @var list<Node\Arg|Node\VariadicPlaceholder> Arguments */
/** @var list<Node\Arg|Node\VariadicPlaceholder|Node\ArgPlaceholder> Arguments */
public array $args;

/**
* Constructs a function call node.
*
* @param Expr $var Variable holding object
* @param string|Identifier|Expr $name Method name
* @param list<Arg|VariadicPlaceholder> $args Arguments
* @param list<Arg|VariadicPlaceholder|ArgPlaceholder> $args Arguments
* @param array<string, mixed> $attributes Additional attributes
*/
public function __construct(Expr $var, $name, array $args = [], array $attributes = []) {}
}

class New_ extends CallLike {
/** @var list<Arg|VariadicPlaceholder> Arguments */
/** @var list<Arg|VariadicPlaceholder|ArgPlaceholder> Arguments */
public array $args;

/**
* Constructs a function call node.
*
* @param Node\Name|Expr|Node\Stmt\Class_ $class Class name (or class node for anonymous classes)
* @param list<Arg|VariadicPlaceholder> $args Arguments
* @param list<Arg|VariadicPlaceholder|ArgPlaceholder> $args Arguments
* @param array<string, mixed> $attributes Additional attributes
*/
public function __construct(Node $class, array $args = [], array $attributes = []) {}
}

class NullsafeMethodCall extends CallLike {
/** @var list<Arg|VariadicPlaceholder> Arguments */
/** @var list<Arg|VariadicPlaceholder|ArgPlaceholder> Arguments */
public array $args;

/**
* Constructs a nullsafe method call node.
*
* @param Expr $var Variable holding object
* @param string|Identifier|Expr $name Method name
* @param list<Arg|VariadicPlaceholder> $args Arguments
* @param list<Arg|VariadicPlaceholder|ArgPlaceholder> $args Arguments
* @param array<string, mixed> $attributes Additional attributes
*/
public function __construct(Expr $var, $name, array $args = [], array $attributes = []) {}
}

class StaticCall extends CallLike {
/** @var list<Arg|VariadicPlaceholder> Arguments */
/** @var list<Arg|VariadicPlaceholder|ArgPlaceholder> Arguments */
public array $args;

/**
* Constructs a static method call node.
*
* @param Node\Name|Expr $class Class name
* @param string|Identifier|Expr $name Method name
* @param list<Arg|VariadicPlaceholder> $args Arguments
* @param list<Arg|VariadicPlaceholder|ArgPlaceholder> $args Arguments
* @param array<string, mixed> $attributes Additional attributes
*/
public function __construct(Node $class, $name, array $args = [], array $attributes = []) {}
Expand Down
28 changes: 28 additions & 0 deletions e2e/result-cache-first-class-callable/dep.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--- src/Dep.php
+++ src/Dep.php
@@ -5,21 +5,21 @@
class Dep
{

- public function m(): string
+ public function m(): int
{
- return 'a';
+ return 1;
}

- public static function s(): string
+ public static function s(): int
{
- return 'a';
+ return 1;
}

}

-function f(): string
+function f(): int
{
- return 'a';
+ return 1;
}
6 changes: 6 additions & 0 deletions e2e/result-cache-first-class-callable/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
level: 8
phpVersion: 80100
tmpDir: tmp
paths:
- src
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash
# @tags result-cache

function set_up() {
e2e::set_up
}

function tear_down() {
e2e::tear_down
}

function test_result_cache_first_class_callable() {
OUTPUT=$(../../bin/phpstan analyse -vv --error-format raw 2>&1) && CODE=0 || CODE=$?
echo "$OUTPUT"
assert_same "0" "$CODE"
# The signature of a first-class callable is the signature of its target, so a
# file creating one from a function, a method or a static method of Dep.php
# depends on Dep.php. Unrelated.php does not.
patch -b src/Dep.php < dep.patch
INFO=$(../../bin/phpstan result-cache-info --json)
echo "$INFO"
assert_equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')"
assert_equals '4' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) && CODE=0 || CODE=$?
echo "$OUTPUT"
assert_same "1" "$CODE"
assert_contains 'FunctionFcc.php:9:Function ResultCacheE2EFirstClassCallable\functionFcc() should return string but returns int.' "$OUTPUT"
assert_contains 'MethodFcc.php:9:Function ResultCacheE2EFirstClassCallable\methodFcc() should return string but returns int.' "$OUTPUT"
assert_contains 'StaticMethodFcc.php:9:Function ResultCacheE2EFirstClassCallable\staticMethodFcc() should return string but returns int.' "$OUTPUT"
}
23 changes: 23 additions & 0 deletions e2e/result-cache-first-class-callable/src/Dep.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace ResultCacheE2EFirstClassCallable;

class Dep
{

public function m(): string
{
return 'a';
}

public static function s(): string
{
return 'a';
}

}

function f(): string
{
return 'a';
}
10 changes: 10 additions & 0 deletions e2e/result-cache-first-class-callable/src/FunctionFcc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace ResultCacheE2EFirstClassCallable;

function functionFcc(): string
{
$f = f(...);

return $f();
}
10 changes: 10 additions & 0 deletions e2e/result-cache-first-class-callable/src/MethodFcc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace ResultCacheE2EFirstClassCallable;

function methodFcc(Dep $dep): string
{
$f = $dep->m(...);

return $f();
}
10 changes: 10 additions & 0 deletions e2e/result-cache-first-class-callable/src/StaticMethodFcc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace ResultCacheE2EFirstClassCallable;

function staticMethodFcc(): string
{
$f = Dep::s(...);

return $f();
}
8 changes: 8 additions & 0 deletions e2e/result-cache-first-class-callable/src/Unrelated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace ResultCacheE2EFirstClassCallable;

function unrelated(): \Closure
{
return strlen(...);
}
2 changes: 2 additions & 0 deletions e2e/result-cache-first-class-callable/tmp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.*
5 changes: 2 additions & 3 deletions src/Analyser/ArgumentsNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Turbo\ShadowedByTurboExtension;
use PHPStan\Type\Constant\ConstantArrayType;
Expand All @@ -29,7 +28,6 @@
use function key;
use function ksort;
use function max;
use function sprintf;

/**
* @api
Expand Down Expand Up @@ -443,7 +441,8 @@ public static function reorderArgs(ParametersAcceptor $parametersAcceptor, array
$defaultValue = $parameter->getDefaultValue();
if ($defaultValue === null) {
if (!$parameter->isVariadic()) {
throw new ShouldNotHappenException(sprintf('An optional parameter $%s must have a default value', $parameter->getName()));
// like an optional parameter combined from variants that do not all have it
return null;
}
$defaultValue = new ConstantArrayType([], []);
}
Expand Down
14 changes: 7 additions & 7 deletions src/Analyser/ExprHandler/FuncCallHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use PHPStan\Analyser\ExpressionResultFactory;
use PHPStan\Analyser\ExpressionResultStorage;
use PHPStan\Analyser\ExprHandler;
use PHPStan\Analyser\ExprHandler\Helper\ClosureOriginCallReplay;
use PHPStan\Analyser\ExprHandler\Helper\ClosureTypeResolver;
use PHPStan\Analyser\ExprHandler\Helper\DefaultNarrowingHelper;
use PHPStan\Analyser\ExprHandler\Helper\DynamicReturnTypeStoragePrimer;
Expand Down Expand Up @@ -600,7 +601,12 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
$argsResult->getDependencies(),
$this->getDependencies($beforeScope, $functionReflection, $nameResult, $preliminaryResult),
);
return $preliminaryResult->finalize($scope, $hasYield, $isAlwaysTerminating, $throwPoints, $impurePoints, $variableFlow, $dependencies);
$result = $preliminaryResult->finalize($scope, $hasYield, $isAlwaysTerminating, $throwPoints, $impurePoints, $variableFlow, $dependencies);
if ($nameResult === null) {
return $result;
}

return ClosureOriginCallReplay::replay($nodeScopeResolver, $this->expressionResultFactory, $expr, $nameResult, $nameType, $scopeBeforeArgs, $storage, $result);
}

/**
Expand Down Expand Up @@ -863,12 +869,6 @@ private function resolveReturnType(NodeScopeResolver $nodeScopeResolver, Mutatin
/** @var non-empty-string $name */
$name = $expr->name->value;
$functionName = new Name($name);
} elseif (
$expr->name instanceof FuncCall
&& $expr->name->name instanceof Name
&& $expr->name->isFirstClassCallable()
) {
$functionName = $expr->name->name;
}

$normalizedNode = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $expr);
Expand Down
Loading
Loading