File tree Expand file tree Collapse file tree 5 files changed +78
-1
lines changed
Expand file tree Collapse file tree 5 files changed +78
-1
lines changed Original file line number Diff line number Diff line change 1+ /.phpunit.result.cache
12/composer.lock
23/phpunit.xml
34/vendor /
Original file line number Diff line number Diff line change 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"
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments