Skip to content

Commit

Permalink
Fix false positive about unused class elements when the class uses a …
Browse files Browse the repository at this point in the history
…trait
  • Loading branch information
ondrejmirtes committed Feb 7, 2023
1 parent 4c7d382 commit fbdf0da
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Type/ThisType.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,21 @@ public function changeBaseClass(ClassReflection $classReflection): StaticType

public function describe(VerbosityLevel $level): string
{
return sprintf('$this(%s)', $this->getStaticObjectType()->describe($level));
$callback = fn () => sprintf('$this(%s)', $this->getStaticObjectType()->describe($level));
return $level->handle(
$callback,
$callback,
$callback,
function () use ($callback): string {
$base = $callback();
$trait = $this->getTraitReflection();
if ($trait === null) {
return $base;
}

return sprintf('%s-trait-%s', $base, $trait->getDisplayName());
},
);
}

public function isSuperTypeOf(Type $type): TrinaryLogic
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/DeadCode/UnusedPrivateMethodRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,13 @@ public function testBug7389(): void
]);
}

public function testFalsePositiveWithTraitUse(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('This test needs PHP 8.1');
}

$this->analyse([__DIR__ . '/data/unused-method-false-positive-with-trait.php'], []);
}

}
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);
}

}
6 changes: 6 additions & 0 deletions tests/PHPStan/Type/ObjectTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Throwable;
use ThrowPoints\TryCatch\MyInvalidArgumentException;
use Traversable;
use UnusedMethodFalsePositiveWithTrait\LocalOnlineReservationTime;
use function count;
use function sprintf;
use const PHP_VERSION_ID;
Expand Down Expand Up @@ -432,6 +433,11 @@ public function dataIsSuperTypeOf(): array
new ObjectType(DateTime::class),
TrinaryLogic::createNo(),
],
61 => [
new ObjectType(LocalOnlineReservationTime::class),
new ThisType($reflectionProvider->getClass(LocalOnlineReservationTime::class)),
TrinaryLogic::createYes(),
],
];
}

Expand Down

0 comments on commit fbdf0da

Please sign in to comment.