Skip to content

Commit 17903c7

Browse files
committed
Fix identifier; fix tests for old PHP
1 parent 5b3e005 commit 17903c7

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

src/Error/BlackMember.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
use ShipMonk\PHPStan\DeadCode\Graph\ClassConstantRef;
77
use ShipMonk\PHPStan\DeadCode\Graph\ClassMemberRef;
88
use ShipMonk\PHPStan\DeadCode\Graph\ClassMemberUsage;
9+
use ShipMonk\PHPStan\DeadCode\Graph\ClassMethodRef;
910
use ShipMonk\PHPStan\DeadCode\Graph\CollectedUsage;
11+
use ShipMonk\PHPStan\DeadCode\Graph\EnumCaseRef;
1012
use ShipMonk\PHPStan\DeadCode\Rule\DeadCodeRule;
1113
use function array_keys;
1214
use function count;
@@ -77,9 +79,15 @@ public function addExcludedUsage(CollectedUsage $excludedUsage): void
7779

7880
public function getErrorIdentifier(): string
7981
{
80-
return $this->member instanceof ClassConstantRef
81-
? DeadCodeRule::IDENTIFIER_CONSTANT
82-
: DeadCodeRule::IDENTIFIER_METHOD;
82+
if ($this->member instanceof ClassConstantRef) {
83+
return DeadCodeRule::IDENTIFIER_CONSTANT;
84+
} elseif ($this->member instanceof ClassMethodRef) {
85+
return DeadCodeRule::IDENTIFIER_METHOD;
86+
} elseif ($this->member instanceof EnumCaseRef) {
87+
return DeadCodeRule::IDENTIFIER_ENUM_CASE;
88+
} else {
89+
throw new LogicException('Unknown member type');
90+
}
8391
}
8492

8593
public function getExclusionMessage(): string

tests/Rule/DeadCodeRuleTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,10 @@ static function (string $message) use (&$actualOutput): void {
503503
*/
504504
public function testAutoRemove(string $file): void
505505
{
506+
if (PHP_VERSION_ID < 8_01_00) {
507+
self::markTestSkipped('Requires PHP 8.1 to test enum case removal');
508+
}
509+
506510
$writtenOutput = '';
507511

508512
$output = $this->createOutput();
@@ -792,11 +796,11 @@ public static function provideFiles(): Traversable
792796
yield 'const-traits-23' => [__DIR__ . '/data/constants/traits-23.php'];
793797

794798
// enums
795-
yield 'enum-basic' => [__DIR__ . '/data/enums/basic.php'];
796-
yield 'enum-mixed' => [__DIR__ . '/data/enums/mixed.php'];
799+
yield 'enum-basic' => [__DIR__ . '/data/enums/basic.php', self::requiresPhp(8_01_00)];
800+
yield 'enum-mixed' => [__DIR__ . '/data/enums/mixed.php', self::requiresPhp(8_01_00)];
797801

798802
// mixed member
799-
yield 'mixed-member-enum' => [__DIR__ . '/data/mixed-member/enum.php'];
803+
yield 'mixed-member-enum' => [__DIR__ . '/data/mixed-member/enum.php', self::requiresPhp(8_01_00)];
800804
yield 'mixed-member-full-method' => [__DIR__ . '/data/mixed-member/full-mixed-method.php'];
801805
yield 'mixed-member-full-const' => [__DIR__ . '/data/mixed-member/full-mixed-const.php'];
802806
yield 'mixed-member-indirect-2' => [__DIR__ . '/data/mixed-member/indirect-interface-2.php'];
@@ -845,7 +849,8 @@ public static function provideFiles(): Traversable
845849
yield 'mixed-member-const-traits-21' => [__DIR__ . '/data/mixed-member/traits-const-21.php'];
846850

847851
// other
848-
yield 'report-lines' => [__DIR__ . '/data/other/report-lines.php'];
852+
yield 'report-lines' => [__DIR__ . '/data/other/report-lines.php', self::requiresPhp(8_01_00)];
853+
yield 'identifiers' => [__DIR__ . '/data/other/error-identifiers.php', self::requiresPhp(8_01_00)];
849854
}
850855

851856
/**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ErrorIdentifiers;
4+
5+
class Foo
6+
{
7+
8+
public const BAR = 1; // @phpstan-ignore shipmonk.deadConstant
9+
10+
public function method(): void {} // @phpstan-ignore shipmonk.deadMethod
11+
12+
}
13+
14+
enum MyEnum: string
15+
{
16+
case MyCase = 'A'; // @phpstan-ignore shipmonk.deadEnumCase
17+
}

0 commit comments

Comments
 (0)