-
Notifications
You must be signed in to change notification settings - Fork 486
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix false positive about unused class elements when the class uses a …
…trait
- Loading branch information
1 parent
4c7d382
commit fbdf0da
Showing
4 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
tests/PHPStan/Rules/DeadCode/data/unused-method-false-positive-with-trait.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php // lint >= 8.1 | ||
|
||
namespace UnusedMethodFalsePositiveWithTrait; | ||
|
||
use ReflectionEnum; | ||
|
||
enum LocalOnlineReservationTime: string | ||
{ | ||
|
||
use LabeledEnumTrait; | ||
|
||
case MORNING = 'morning'; | ||
case AFTERNOON = 'afternoon'; | ||
case EVENING = 'evening'; | ||
|
||
public static function getPeriodForHour(string $hour): self | ||
{ | ||
$hour = self::hourToNumber($hour); | ||
|
||
throw new \Exception('Internal error'); | ||
} | ||
|
||
private static function hourToNumber(string $hour): int | ||
{ | ||
return (int) str_replace(':', '', $hour); | ||
} | ||
|
||
} | ||
|
||
trait LabeledEnumTrait | ||
{ | ||
|
||
use EnumTrait; | ||
|
||
} | ||
|
||
trait EnumTrait | ||
{ | ||
|
||
/** | ||
* @return list<static> | ||
*/ | ||
public static function getDeprecatedEnums(): array | ||
{ | ||
static $cache = []; | ||
if ($cache === []) { | ||
$reflection = new ReflectionEnum(self::class); | ||
$cases = $reflection->getCases(); | ||
|
||
foreach ($cases as $case) { | ||
$docComment = $case->getDocComment(); | ||
if ($docComment === false || !str_contains($docComment, '@deprecated')) { | ||
continue; | ||
} | ||
$cache[] = self::from($case->getBackingValue()); | ||
} | ||
} | ||
|
||
return $cache; | ||
} | ||
|
||
public function isDeprecated(): bool | ||
{ | ||
return $this->equalsAny(self::getDeprecatedEnums()); | ||
} | ||
|
||
public function equalsAny(...$that): bool | ||
{ | ||
return in_array($this, $that, true); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters