Skip to content

Commit 373a86a

Browse files
authored
Added "Invokable Cases" PHPStan extension (#13)
* 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. * Added "Invokable Cases" PHPStan extension testing
1 parent 7e17b84 commit 373a86a

File tree

11 files changed

+382
-0
lines changed

11 files changed

+382
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,15 @@ enum TaskStatus: int
366366

367367
And if you're using the same meta property in multiple enums, you can create a dedicated trait that includes this `@method` annotation.
368368

369+
## PHPStan
370+
371+
To assist PHPStan when using invokable cases, you can include the PHPStan extensions into your own `phpstan.neon` file:
372+
373+
```yaml
374+
includes:
375+
- ./vendor/archtechx/enums/extension.neon
376+
```
377+
369378
## Development
370379
371380
Run all checks locally:

extension.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
includes:
2+
- ./src/PHPStan/InvokableCases/extension.neon

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
includes:
22
- ./vendor/nunomaduro/larastan/extension.neon
3+
- ./extension.neon
34

45
parameters:
56
paths:
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ArchTech\Enums\PHPStan\InvokableCases;
6+
7+
use ArchTech\Enums\InvokableCases;
8+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionEnum;
9+
use PHPStan\Reflection\ClassReflection;
10+
use PHPStan\Reflection\MethodReflection;
11+
use PHPStan\Reflection\MethodsClassReflectionExtension;
12+
13+
class ReflectionExtension implements MethodsClassReflectionExtension
14+
{
15+
public function hasMethod(ClassReflection $classReflection, string $methodName): bool
16+
{
17+
if (
18+
$classReflection->getNativeReflection() instanceof ReflectionEnum
19+
&& $classReflection->hasTraitUse(InvokableCases::class)
20+
) {
21+
return $classReflection->getNativeReflection()->hasCase($methodName);
22+
}
23+
24+
return false;
25+
}
26+
27+
public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection
28+
{
29+
return new StaticInvokableCaseMethodReflection($classReflection, $methodName);
30+
}
31+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ArchTech\Enums\PHPStan\InvokableCases;
6+
7+
use PHPStan\Analyser\OutOfClassScope;
8+
use PHPStan\Reflection\ClassMemberReflection;
9+
use PHPStan\Reflection\ClassReflection;
10+
use PHPStan\Reflection\FunctionVariant;
11+
use PHPStan\Reflection\MethodReflection;
12+
use PHPStan\Reflection\ParametersAcceptor;
13+
use PHPStan\TrinaryLogic;
14+
use PHPStan\Type\Generic\TemplateTypeMap;
15+
use PHPStan\Type\StringType;
16+
use PHPStan\Type\Type;
17+
18+
class StaticInvokableCaseMethodReflection implements MethodReflection
19+
{
20+
private readonly MethodReflection $callStaticMethod;
21+
22+
public function __construct(
23+
private readonly ClassReflection $classReflection,
24+
private readonly string $name,
25+
) {
26+
$this->callStaticMethod = $this->classReflection->getMethod('__callStatic', new OutOfClassScope());
27+
}
28+
29+
public function getDeclaringClass(): ClassReflection
30+
{
31+
return $this->classReflection;
32+
}
33+
34+
public function isStatic(): bool
35+
{
36+
return true;
37+
}
38+
39+
public function isPrivate(): bool
40+
{
41+
return false;
42+
}
43+
44+
public function isPublic(): bool
45+
{
46+
return true;
47+
}
48+
49+
public function getDocComment(): ?string
50+
{
51+
return $this->callStaticMethod->getDocComment();
52+
}
53+
54+
public function getName(): string
55+
{
56+
return $this->name;
57+
}
58+
59+
public function getPrototype(): ClassMemberReflection
60+
{
61+
return $this;
62+
}
63+
64+
/**
65+
* @return ParametersAcceptor[]
66+
*/
67+
public function getVariants(): array
68+
{
69+
return [
70+
new FunctionVariant(
71+
TemplateTypeMap::createEmpty(),
72+
TemplateTypeMap::createEmpty(),
73+
[],
74+
false,
75+
$this->classReflection->getBackedEnumType() ?? new StringType()
76+
),
77+
];
78+
}
79+
80+
public function isDeprecated(): TrinaryLogic
81+
{
82+
return $this->callStaticMethod->isDeprecated();
83+
}
84+
85+
public function getDeprecatedDescription(): ?string
86+
{
87+
return $this->callStaticMethod->getDeprecatedDescription();
88+
}
89+
90+
public function isFinal(): TrinaryLogic
91+
{
92+
return TrinaryLogic::createNo();
93+
}
94+
95+
public function isInternal(): TrinaryLogic
96+
{
97+
return TrinaryLogic::createNo();
98+
}
99+
100+
public function getThrowType(): ?Type
101+
{
102+
return $this->callStaticMethod->getThrowType();
103+
}
104+
105+
public function hasSideEffects(): TrinaryLogic
106+
{
107+
return TrinaryLogic::createNo();
108+
}
109+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
-
3+
class: ArchTech\Enums\PHPStan\InvokableCases\ReflectionExtension
4+
tags:
5+
- phpstan.broker.methodsClassReflectionExtension
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace ArchTech\Enums\Tests\PHPStan\InvokableCases;
4+
5+
use PHPStan\Analyser\OutOfClassScope;
6+
use PHPStan\Reflection\ParametersAcceptorSelector;
7+
use PHPStan\Testing\PHPStanTestCase;
8+
9+
class InvokableCasesTestCase extends PHPStanTestCase
10+
{
11+
public static function getAdditionalConfigFiles(): array
12+
{
13+
return [__DIR__ . '/../../../src/PHPStan/InvokableCases/extension.neon'];
14+
}
15+
16+
public function assertStaticallyCallable(string $enum, string $case, $exists = true, $static = true): void
17+
{
18+
$reflectionProvider = $this->createReflectionProvider();
19+
$class = $reflectionProvider->getClass($enum);
20+
21+
if ($exists) {
22+
$this->assertTrue($class->hasMethod($case), sprintf('%s on class %s does not exist', $case, $enum));
23+
$method = $class->getMethod($case, new OutOfClassScope());
24+
if ($static) {
25+
$this->assertTrue($method->isStatic(), sprintf('%s on class %s is not static', $case, $enum));
26+
} else {
27+
$this->assertFalse($method->isStatic(), sprintf('%s on class %s is static', $case, $enum));
28+
}
29+
} else {
30+
$this->assertFalse($class->hasMethod($case), sprintf('%s on class %s exists', $case, $enum));
31+
}
32+
}
33+
34+
public function assertStaticallyCallableType(string $enum, string $case, string $type): void
35+
{
36+
$reflectionProvider = $this->createReflectionProvider();
37+
$class = $reflectionProvider->getClass($enum);
38+
39+
$method = $class->getMethod($case, new OutOfClassScope());
40+
$methodVariant = ParametersAcceptorSelector::selectSingle($method->getVariants());
41+
$methodReturnType = $methodVariant->getReturnType();
42+
$this->assertInstanceOf($type, $methodReturnType);
43+
}
44+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ArchTech\Enums\Tests\PHPStan\InvokableCases;
6+
7+
use ArchTech\Enums\InvokableCases;
8+
9+
enum Role
10+
{
11+
use InvokableCases;
12+
13+
case admin;
14+
case manager;
15+
case staff;
16+
17+
public static function administrator(): self
18+
{
19+
return self::admin;
20+
}
21+
22+
public function isManager(): bool
23+
{
24+
return $this === self::manager;
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ArchTech\Enums\Tests\PHPStan\InvokableCases;
6+
7+
use ArchTech\Enums\InvokableCases;
8+
9+
enum Status: int
10+
{
11+
use InvokableCases;
12+
13+
case created = 0;
14+
case running = 1;
15+
case done = 2;
16+
17+
public static function initial(): self
18+
{
19+
return self::created;
20+
}
21+
22+
public function isStarted(): bool
23+
{
24+
return $this->value > self::created->value;
25+
}
26+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ArchTech\Enums\Tests\PHPStan\InvokableCases;
6+
7+
use ArchTech\Enums\InvokableCases;
8+
9+
enum Suits: string
10+
{
11+
use InvokableCases;
12+
13+
case clubs = 'C';
14+
case diamonds = 'D';
15+
case hearts = 'H';
16+
case spades = 'S';
17+
18+
public static function valuable(): self
19+
{
20+
return self::diamonds;
21+
}
22+
23+
public function isRed(): bool
24+
{
25+
return match ($this) {
26+
self::diamonds, self::hearts => true,
27+
default => false,
28+
};
29+
}
30+
}

0 commit comments

Comments
 (0)