Skip to content

Commit 435c3d8

Browse files
committed
Fix identifier; fix tests for old PHP
1 parent 007c52f commit 435c3d8

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
@@ -435,6 +435,10 @@ static function (string $message) use (&$actualOutput): void {
435435
*/
436436
public function testAutoRemove(string $file): void
437437
{
438+
if (PHP_VERSION_ID < 8_01_00) {
439+
self::markTestSkipped('Requires PHP 8.1 to test enum case removal');
440+
}
441+
438442
$writtenOutput = '';
439443

440444
$output = $this->createOutput();
@@ -718,11 +722,11 @@ public static function provideFiles(): Traversable
718722
yield 'const-traits-23' => [__DIR__ . '/data/constants/traits-23.php'];
719723

720724
// enums
721-
yield 'enum-basic' => [__DIR__ . '/data/enums/basic.php'];
722-
yield 'enum-mixed' => [__DIR__ . '/data/enums/mixed.php'];
725+
yield 'enum-basic' => [__DIR__ . '/data/enums/basic.php', self::requiresPhp(8_01_00)];
726+
yield 'enum-mixed' => [__DIR__ . '/data/enums/mixed.php', self::requiresPhp(8_01_00)];
723727

724728
// mixed member
725-
yield 'mixed-member-enum' => [__DIR__ . '/data/mixed-member/enum.php'];
729+
yield 'mixed-member-enum' => [__DIR__ . '/data/mixed-member/enum.php', self::requiresPhp(8_01_00)];
726730
yield 'mixed-member-full-method' => [__DIR__ . '/data/mixed-member/full-mixed-method.php'];
727731
yield 'mixed-member-full-const' => [__DIR__ . '/data/mixed-member/full-mixed-const.php'];
728732
yield 'mixed-member-indirect-2' => [__DIR__ . '/data/mixed-member/indirect-interface-2.php'];
@@ -770,7 +774,8 @@ public static function provideFiles(): Traversable
770774
yield 'mixed-member-const-traits-21' => [__DIR__ . '/data/mixed-member/traits-const-21.php'];
771775

772776
// other
773-
yield 'report-lines' => [__DIR__ . '/data/other/report-lines.php'];
777+
yield 'report-lines' => [__DIR__ . '/data/other/report-lines.php', self::requiresPhp(8_01_00)];
778+
yield 'identifiers' => [__DIR__ . '/data/other/error-identifiers.php', self::requiresPhp(8_01_00)];
774779
}
775780

776781
/**
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)