Skip to content

Commit 5fe2eab

Browse files
authored
Add AlwaysUsedClassConstantsExtension rule (#18)
* Add EnumAlwaysUsedConstantsExtension * Add .phpunit.result.cache in .gitignore
1 parent 8c28346 commit 5fe2eab

File tree

5 files changed

+78
-1
lines changed

5 files changed

+78
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/.phpunit.result.cache
12
/composer.lock
23
/phpunit.xml
34
/vendor/

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"require": {
88
"php": "~7.1|^8.0",
99
"myclabs/php-enum": "^1.2",
10-
"phpstan/phpstan": "^0.10|^0.11|^0.12.34"
10+
"phpstan/phpstan": "^0.12.84"
1111
},
1212
"require-dev": {
1313
"phpunit/phpunit": "^7.0|^9.0"

extension.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ services:
22
- class: Timeweb\PHPStan\Reflection\EnumMethodsClassReflectionExtension
33
tags:
44
- phpstan.broker.methodsClassReflectionExtension
5+
- class: Timeweb\PHPStan\Rule\EnumAlwaysUsedConstants
6+
tags:
7+
- phpstan.constants.alwaysUsedClassConstantsExtension
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Timeweb\PHPStan\Rule;
6+
7+
use MyCLabs\Enum\Enum;
8+
use PHPStan\Reflection\ConstantReflection;
9+
use PHPStan\Rules\Constants\AlwaysUsedClassConstantsExtension;
10+
11+
class EnumAlwaysUsedConstantsExtension implements AlwaysUsedClassConstantsExtension
12+
{
13+
public function isAlwaysUsed(ConstantReflection $constant): bool
14+
{
15+
return $constant->getDeclaringClass()->isSubclassOf(Enum::class);
16+
}
17+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Timeweb\Tests\PHPStan\Rule;
6+
7+
use PHPStan\Testing\TestCase;
8+
use Timeweb\PHPStan\Rule\EnumAlwaysUsedConstantsExtension;
9+
use Timeweb\Tests\PHPStan\Fixture\EnumFixture;
10+
11+
/**
12+
* @coversDefaultClass \Timeweb\PHPStan\Rule\EnumAlwaysUsedConstantsExtension
13+
*/
14+
class EnumAlwaysUsedConstantsExtensionTest extends TestCase
15+
{
16+
/**
17+
* @var \PHPStan\Broker\Broker
18+
*/
19+
protected $broker;
20+
21+
/**
22+
* @var EnumAlwaysUsedConstantsExtension
23+
*/
24+
protected $constantsExtension;
25+
26+
public function setUp(): void
27+
{
28+
$this->broker = $this->createBroker();
29+
$this->constantsExtension = new EnumAlwaysUsedConstantsExtension();
30+
}
31+
32+
/**
33+
* @covers ::isAlwaysUsed
34+
* @dataProvider enumFixtureProperties
35+
*/
36+
public function testEnumConstantsAreConsideredAsAlwaysUsed(string $constantName): void
37+
{
38+
$classReflection = $this->broker->getClass(EnumFixture::class);
39+
$constantReflection = $classReflection->getConstant($constantName);
40+
41+
$this->assertTrue($this->constantsExtension->isAlwaysUsed($constantReflection));
42+
43+
}
44+
45+
/**
46+
* @return string[][]
47+
*/
48+
public function enumFixtureProperties(): array
49+
{
50+
return [
51+
['MEMBER'],
52+
['PUBLIC_CONST'],
53+
['PRIVATE_CONST'],
54+
];
55+
}
56+
}

0 commit comments

Comments
 (0)