Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -716,13 +716,14 @@ private function processStmtNode(
$hasYield = false;
$throwPoints = [];
$impurePoints = [];
$this->processAttributeGroups($stmt, $stmt->attrGroups, $scope, $storage, $nodeCallback);
[$templateTypeMap, $phpDocParameterTypes, $phpDocImmediatelyInvokedCallableParameters, $phpDocClosureThisTypeParameters, $phpDocReturnType, $phpDocThrowType, $deprecatedDescription, $isDeprecated, $isInternal, $isFinal, $isPure, $acceptsNamedArguments, $isReadOnly, $phpDocComment, $asserts, $selfOutType, $phpDocParameterOutTypes] = $this->getPhpDocs($scope, $stmt);

foreach ($stmt->params as $param) {
$this->processParamNode($stmt, $param, $scope, $storage, $nodeCallback);
}

$this->processAttributeGroups($stmt, $stmt->attrGroups, $scope, $storage, $nodeCallback);

if ($stmt->returnType !== null) {
$this->callNodeCallback($nodeCallback, $stmt->returnType, $scope, $storage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class ParameterClosureTypeExtensionArrowFunctionTest extends TypeInferenceTestCa
public static function dataFileAsserts(): iterable
{
yield from self::gatherAssertTypes(__DIR__ . '/data/parameter-closure-type-extension-arrow-function.php');
if (PHP_VERSION_ID >= 80500) {
yield from self::gatherAssertTypes(__DIR__ . '/data/closure-type-in-constant-expression-php85.php');
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace ClosureTypeInConstantExpressionPhp85;

use Attribute;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\Native\NativeParameterReflection;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\PassedByReference;
use PHPStan\Type\CallableType;
use PHPStan\Type\FloatType;
use PHPStan\Type\Type;
use PHPStan\Type\MixedType;
use function PHPStan\Testing\assertType;

class StaticMethodParameterClosureTypeExtension implements \PHPStan\Type\StaticMethodParameterClosureTypeExtension
{

public function isStaticMethodSupported(MethodReflection $methodReflection, ParameterReflection $parameter): bool
{
if ($methodReflection->getName() !== '__construct') {
return false;
}

if ($methodReflection->getDeclaringClass()->getName() !== Idempotency::class) {
return false;
}

return $parameter->getName() === 'key';
}

public function getTypeFromStaticMethodCall(
MethodReflection $methodReflection,
StaticCall $methodCall,
ParameterReflection $parameter,
Scope $scope
): ?Type {
// Just some garbage, that doesn't throw an error anyway.
return new CallableType(
[
new NativeParameterReflection('test', false, new FloatType(), PassedByReference::createNo(), false, null),
],
new MixedType()
);

// What we need here, is a way to find out that this is being called from `__invoke`
// using $scope->getFunctionName()
// Then we would want to find out the 1st parameter of __invoke
// It would be SomeCommand.
// Then we would want to return that the callable signature is `static function (SomeCommand): string`
// But I'm not sure if this is the correct extension point at all...
// It seems it's not.
}
}

class SomeCommand {}

#[Attribute(flags: Attribute::TARGET_METHOD)]
final readonly class Idempotency
{
/**
* @param Closure(object): string $key
*/
public function __construct(
public Closure $key,
) {
}
}

class SomeHandler
{
#[Idempotency(key: static function (object $command) : string {
assertType(SomeCommand::class, $command);

return 'hello';
})]
public function __invoke(SomeCommand $command): void
{
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ services:
tags:
- phpstan.staticMethodParameterClosureTypeExtension

-
class: \ClosureTypeInConstantExpressionPhp85\StaticMethodParameterClosureTypeExtension
tags:
- phpstan.staticMethodParameterClosureTypeExtension

Loading