1
0
Fork 0
mirror of https://github.com/archtechx/enums.git synced 2025-12-12 09:24:03 +00:00

Added "Invokable Cases" PHPStan extension

This includes a PHPStan extension to add support for `InvokableCases`
so that static analysis tools can understand the callable methods and
their return types.

The extension has been added in a way that will allow multiple extensions
in the future if required, with a single include file that will import
all extensions, or the option to include only specific extensions.
This commit is contained in:
Samuel Levy 2022-07-14 11:55:36 +10:00
parent 7e17b84451
commit 7e6914a52f
5 changed files with 156 additions and 0 deletions

View file

@ -366,6 +366,15 @@ enum TaskStatus: int
And if you're using the same meta property in multiple enums, you can create a dedicated trait that includes this `@method` annotation. And if you're using the same meta property in multiple enums, you can create a dedicated trait that includes this `@method` annotation.
## PHPStan
To assist PHPStan when using invokable cases, you can include the PHPStan extensions into your own `phpstan.neon` file:
```yaml
includes:
- ./vendor/archtechx/enums/extension.neon
```
## Development ## Development
Run all checks locally: Run all checks locally:

2
extension.neon Normal file
View file

@ -0,0 +1,2 @@
includes:
- ./src/PHPStan/InvokableCases/extension.neon

View file

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace ArchTech\Enums\PHPStan\InvokableCases;
use ArchTech\Enums\InvokableCases;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionEnum;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\MethodsClassReflectionExtension;
class ReflectionExtension implements MethodsClassReflectionExtension
{
public function hasMethod(ClassReflection $classReflection, string $methodName): bool
{
if (
$classReflection->getNativeReflection() instanceof ReflectionEnum
&& $classReflection->hasTraitUse(InvokableCases::class)
) {
return $classReflection->getNativeReflection()->hasCase($methodName);
}
return false;
}
public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection
{
return new StaticInvokableCaseMethodReflection($classReflection, $methodName);
}
}

View file

@ -0,0 +1,109 @@
<?php
declare(strict_types=1);
namespace ArchTech\Enums\PHPStan\InvokableCases;
use PHPStan\Analyser\OutOfClassScope;
use PHPStan\Reflection\ClassMemberReflection;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\FunctionVariant;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
class StaticInvokableCaseMethodReflection implements MethodReflection
{
private readonly MethodReflection $callStaticMethod;
public function __construct(
private readonly ClassReflection $classReflection,
private readonly string $name,
) {
$this->callStaticMethod = $this->classReflection->getMethod('__callStatic', new OutOfClassScope());
}
public function getDeclaringClass(): ClassReflection
{
return $this->classReflection;
}
public function isStatic(): bool
{
return true;
}
public function isPrivate(): bool
{
return false;
}
public function isPublic(): bool
{
return true;
}
public function getDocComment(): ?string
{
return $this->callStaticMethod->getDocComment();
}
public function getName(): string
{
return $this->name;
}
public function getPrototype(): ClassMemberReflection
{
return $this;
}
/**
* @return ParametersAcceptor[]
*/
public function getVariants(): array
{
return [
new FunctionVariant(
TemplateTypeMap::createEmpty(),
TemplateTypeMap::createEmpty(),
[],
false,
$this->classReflection->getBackedEnumType() ?? new StringType()
),
];
}
public function isDeprecated(): TrinaryLogic
{
return $this->callStaticMethod->isDeprecated();
}
public function getDeprecatedDescription(): ?string
{
return $this->callStaticMethod->getDeprecatedDescription();
}
public function isFinal(): TrinaryLogic
{
return TrinaryLogic::createNo();
}
public function isInternal(): TrinaryLogic
{
return TrinaryLogic::createNo();
}
public function getThrowType(): ?Type
{
return $this->callStaticMethod->getThrowType();
}
public function hasSideEffects(): TrinaryLogic
{
return TrinaryLogic::createNo();
}
}

View file

@ -0,0 +1,5 @@
services:
-
class: ArchTech\Enums\PHPStan\InvokableCases\ReflectionExtension
tags:
- phpstan.broker.methodsClassReflectionExtension