Skip to content
Closed
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
106 changes: 106 additions & 0 deletions src/Reflection/Php/ClosureInvokeMethodReflection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php declare(strict_types = 1);

namespace PHPStan\Reflection\Php;

use PHPStan\Reflection\ClassMemberReflection;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\FunctionVariant;
use PHPStan\Reflection\MethodReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\ClosureType;
use PHPStan\Type\Type;

final class ClosureInvokeMethodReflection implements MethodReflection
{

/** @var MethodReflection */
private $nativeMethodReflection;

/** @var ClosureType */
private $closureType;

public function __construct(MethodReflection $nativeMethodReflection, ClosureType $closureType)
{
$this->nativeMethodReflection = $nativeMethodReflection;
$this->closureType = $closureType;
}

public function getDeclaringClass(): ClassReflection
{
return $this->nativeMethodReflection->getDeclaringClass();
}

public function isStatic(): bool
{
return $this->nativeMethodReflection->isStatic();
}

public function isPrivate(): bool
{
return $this->nativeMethodReflection->isPrivate();
}

public function isPublic(): bool
{
return $this->nativeMethodReflection->isPublic();
}

public function getDocComment(): ?string
{
return $this->nativeMethodReflection->getDocComment();
}

public function getName(): string
{
return $this->nativeMethodReflection->getName();
}

public function getPrototype(): ClassMemberReflection
{
return $this->nativeMethodReflection->getPrototype();
}

public function getVariants(): array
{
return [
new FunctionVariant(
$this->closureType->getTemplateTypeMap(),
$this->closureType->getResolvedTemplateTypeMap(),
$this->closureType->getParameters(),
$this->closureType->isVariadic(),
$this->closureType->getReturnType()
),
];
}

public function isDeprecated(): TrinaryLogic
{
return $this->nativeMethodReflection->isDeprecated();
}

public function getDeprecatedDescription(): ?string
{
return $this->nativeMethodReflection->getDeprecatedDescription();
}

public function isFinal(): TrinaryLogic
{
return $this->nativeMethodReflection->isFinal();
}

public function isInternal(): TrinaryLogic
{
return $this->nativeMethodReflection->isInternal();
}

public function getThrowType(): ?Type
{
return $this->nativeMethodReflection->getThrowType();
}

public function hasSideEffects(): TrinaryLogic
{
return $this->nativeMethodReflection->hasSideEffects();
}

}
8 changes: 8 additions & 0 deletions src/Type/ClosureType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\Reflection\Php\ClosureCallMethodReflection;
use PHPStan\Reflection\Php\ClosureInvokeMethodReflection;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Constant\ConstantArrayType;
Expand Down Expand Up @@ -167,6 +168,13 @@ public function getMethod(string $methodName, ClassMemberAccessAnswerer $scope):
);
}

if ($methodName === '__invoke') {
return new ClosureInvokeMethodReflection(
$this->objectType->getMethod($methodName, $scope),
$this
);
}

return $this->objectType->getMethod($methodName, $scope);
}

Expand Down