Skip to content

Commit

Permalink
Rename DeadCodeRule & ClassMemberUsage
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal committed Nov 14, 2024
1 parent 8358d25 commit 782ee7e
Show file tree
Hide file tree
Showing 104 changed files with 148 additions and 148 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ Found 2 methods called over unknown type:
- Abstract trait methods are never reported as dead
- Most magic methods (e.g. `__get`, `__set` etc) are never reported as dead
- Only supported are: `__construct`, `__clone`

### Other problematic cases:

#### Constructors:
Expand All @@ -194,7 +194,7 @@ parameters:
#### Private constructors:
- Those are never reported as dead as those are often used to deny class instantiation


## Future scope:
- Dead class constant detection
- Dead class property detection
Expand Down
2 changes: 1 addition & 1 deletion collision-detector.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"excludePaths": ["tests/Rule/data/DeadMethodRule/removing"]
"excludePaths": ["tests/Rule/data/removing"]
}
2 changes: 1 addition & 1 deletion rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ services:
entrypointProviders: tagged(shipmonk.deadCode.entrypointProvider)

-
class: ShipMonk\PHPStan\DeadCode\Rule\DeadMethodRule
class: ShipMonk\PHPStan\DeadCode\Rule\DeadCodeRule
tags:
- phpstan.rules.rule
- phpstan.diagnoseExtension
Expand Down
4 changes: 2 additions & 2 deletions src/Crate/ClassConstantFetch.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* @immutable
*/
class ClassConstantFetch extends ClassMemberUse
class ClassConstantFetch extends ClassMemberUsage
{

private ClassConstantRef $fetch;
Expand All @@ -28,7 +28,7 @@ public function getMemberType(): int
return ClassMemberRef::TYPE_CONSTANT;
}

public function getMemberUse(): ClassConstantRef
public function getMemberUsage(): ClassConstantRef
{
return $this->fetch;
}
Expand Down
28 changes: 14 additions & 14 deletions src/Crate/ClassMemberUse.php → src/Crate/ClassMemberUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,44 @@
/**
* @immutable
*/
abstract class ClassMemberUse // TODO rename to ClassMemberUsage ?
abstract class ClassMemberUsage
{

/**
* Origin method of the use, "where it was called from"
* Origin method of the usage, "where it was called from"
*/
private ?ClassMethodRef $caller;
private ?ClassMethodRef $origin;

/**
* If true, class of getMemberUse() may be descendant
* If true, class of getMemberUsage() may be descendant
*/
private bool $possibleDescendantUse;
private bool $possibleDescendantUsage;

public function __construct(
?ClassMethodRef $caller,
bool $possibleDescendantUse
?ClassMethodRef $origin,
bool $possibleDescendantUsage
)
{
$this->caller = $caller;
$this->possibleDescendantUse = $possibleDescendantUse;
$this->origin = $origin;
$this->possibleDescendantUsage = $possibleDescendantUsage;
}

public function isPossibleDescendantUse(): bool
public function isPossibleDescendantUsage(): bool
{
return $this->possibleDescendantUse;
return $this->possibleDescendantUsage;
}

public function getCaller(): ?ClassMethodRef
public function getOrigin(): ?ClassMethodRef
{
return $this->caller;
return $this->origin;
}

/**
* @return ClassMemberRef::TYPE_*
*/
abstract public function getMemberType(): int;

abstract public function getMemberUse(): ClassMemberRef;
abstract public function getMemberUsage(): ClassMemberRef;

public function serialize(): string
{
Expand Down
8 changes: 4 additions & 4 deletions src/Crate/ClassMethodCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
/**
* @immutable
*/
class ClassMethodCall extends ClassMemberUse
class ClassMethodCall extends ClassMemberUsage
{

private ClassMethodRef $callee;

public function __construct(
?ClassMethodRef $caller,
?ClassMethodRef $origin,
ClassMethodRef $callee,
bool $possibleDescendantCall
)
{
parent::__construct($caller, $possibleDescendantCall);
parent::__construct($origin, $possibleDescendantCall);

$this->callee = $callee;
}
Expand All @@ -29,7 +29,7 @@ public function getMemberType(): int
return ClassMemberRef::TYPE_METHOD;
}

public function getMemberUse(): ClassMethodRef
public function getMemberUsage(): ClassMethodRef
{
return $this->callee;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Formatter/RemoveDeadCodeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use PHPStan\Command\AnalysisResult;
use PHPStan\Command\ErrorFormatter\ErrorFormatter;
use PHPStan\Command\Output;
use ShipMonk\PHPStan\DeadCode\Rule\DeadMethodRule;
use ShipMonk\PHPStan\DeadCode\Rule\DeadCodeRule;
use ShipMonk\PHPStan\DeadCode\Transformer\FileSystem;
use ShipMonk\PHPStan\DeadCode\Transformer\RemoveDeadCodeTransformer;
use function count;
Expand Down Expand Up @@ -41,8 +41,8 @@ public function formatErrors(

foreach ($analysisResult->getFileSpecificErrors() as $fileSpecificError) {
if (
$fileSpecificError->getIdentifier() !== DeadMethodRule::IDENTIFIER_METHOD
&& $fileSpecificError->getIdentifier() !== DeadMethodRule::IDENTIFIER_CONSTANT
$fileSpecificError->getIdentifier() !== DeadCodeRule::IDENTIFIER_METHOD
&& $fileSpecificError->getIdentifier() !== DeadCodeRule::IDENTIFIER_CONSTANT
) {
continue;
}
Expand All @@ -59,8 +59,8 @@ public function formatErrors(
$count = 0;

foreach ($deadMembersByFiles as $file => $deadMembersByType) {
$deadConstants = $deadMembersByType[DeadMethodRule::IDENTIFIER_CONSTANT] ?? [];
$deadMethods = $deadMembersByType[DeadMethodRule::IDENTIFIER_METHOD] ?? [];
$deadConstants = $deadMembersByType[DeadCodeRule::IDENTIFIER_CONSTANT] ?? [];
$deadMethods = $deadMembersByType[DeadCodeRule::IDENTIFIER_METHOD] ?? [];

$count += count($deadConstants) + count($deadMethods);

Expand Down
40 changes: 20 additions & 20 deletions src/Rule/DeadMethodRule.php → src/Rule/DeadCodeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use ShipMonk\PHPStan\DeadCode\Crate\ClassConstantFetch;
use ShipMonk\PHPStan\DeadCode\Crate\ClassConstantRef;
use ShipMonk\PHPStan\DeadCode\Crate\ClassMemberRef;
use ShipMonk\PHPStan\DeadCode\Crate\ClassMemberUse;
use ShipMonk\PHPStan\DeadCode\Crate\ClassMemberUsage;
use ShipMonk\PHPStan\DeadCode\Crate\ClassMethodCall;
use ShipMonk\PHPStan\DeadCode\Crate\ClassMethodRef;
use ShipMonk\PHPStan\DeadCode\Crate\Kind;
Expand All @@ -38,7 +38,7 @@
/**
* @implements Rule<CollectedDataNode>
*/
class DeadMethodRule implements Rule, DiagnoseExtension // TODO rename to DeadCodeRule
class DeadCodeRule implements Rule, DiagnoseExtension
{

public const IDENTIFIER_METHOD = 'shipmonk.deadMethod';
Expand Down Expand Up @@ -99,7 +99,7 @@ class DeadMethodRule implements Rule, DiagnoseExtension // TODO rename to DeadCo
/**
* memberType => [memberName => ClassMemberUse[]]
*
* @var array<ClassMemberRef::TYPE_*, array<string, list<ClassMemberUse>>>
* @var array<ClassMemberRef::TYPE_*, array<string, list<ClassMemberUsage>>>
*/
private array $mixedMemberUses = [];

Expand Down Expand Up @@ -137,7 +137,7 @@ public function processNode(
return [];
}

/** @var list<ClassMemberUse> $memberUses */
/** @var list<ClassMemberUsage> $memberUses */
$memberUses = [];

$methodDeclarationData = $node->get(ClassDefinitionCollector::class);
Expand All @@ -152,10 +152,10 @@ public function processNode(
foreach ($memberUseData as $usesPerFile) {
foreach ($usesPerFile as $useStrings) {
foreach ($useStrings as $useString) {
$memberUse = ClassMemberUse::deserialize($useString);
$memberUse = ClassMemberUsage::deserialize($useString);

if ($memberUse->getMemberUse()->className === null) {
$this->mixedMemberUses[$memberUse->getMemberType()][$memberUse->getMemberUse()->memberName][] = $memberUse;
if ($memberUse->getMemberUsage()->className === null) {
$this->mixedMemberUses[$memberUse->getMemberType()][$memberUse->getMemberUsage()->memberName][] = $memberUse;
continue;
}

Expand Down Expand Up @@ -199,9 +199,9 @@ public function processNode(

foreach ($this->mixedMemberUses[ClassMemberRef::TYPE_METHOD][$methodName] ?? [] as $originalCall) {
$memberUses[] = new ClassMethodCall(
$originalCall->getCaller(),
$originalCall->getOrigin(),
new ClassMethodRef($typeName, $methodName),
$originalCall->isPossibleDescendantUse(),
$originalCall->isPossibleDescendantUsage(),
);
}
}
Expand All @@ -212,9 +212,9 @@ public function processNode(

foreach ($this->mixedMemberUses[ClassMemberRef::TYPE_CONSTANT][$constantName] ?? [] as $originalFetch) {
$memberUses[] = new ClassConstantFetch(
$originalFetch->getCaller(),
$originalFetch->getOrigin(),
new ClassConstantRef($typeName, $constantName),
$originalFetch->isPossibleDescendantUse(),
$originalFetch->isPossibleDescendantUsage(),
);
}
}
Expand All @@ -225,8 +225,8 @@ public function processNode(
foreach ($memberUses as $memberUse) {
$isWhite = $this->isConsideredWhite($memberUse);

$alternativeMemberKeys = $this->getAlternativeMemberKeys($memberUse->getMemberUse(), $memberUse->isPossibleDescendantUse());
$alternativeCallerKeys = $memberUse->getCaller() !== null ? $this->getAlternativeMemberKeys($memberUse->getCaller(), false) : [];
$alternativeMemberKeys = $this->getAlternativeMemberKeys($memberUse->getMemberUsage(), $memberUse->isPossibleDescendantUsage());
$alternativeCallerKeys = $memberUse->getOrigin() !== null ? $this->getAlternativeMemberKeys($memberUse->getOrigin(), false) : [];

foreach ($alternativeMemberKeys as $alternativeMemberKey) {
foreach ($alternativeCallerKeys as $alternativeCallerKey) {
Expand Down Expand Up @@ -590,11 +590,11 @@ private function getTraitUsages(string $typeName): array
return $this->typeDefinitions[$typeName]['traits'] ?? [];
}

private function isConsideredWhite(ClassMemberUse $memberUse): bool
private function isConsideredWhite(ClassMemberUsage $memberUse): bool
{
return $memberUse->getCaller() === null
|| $this->isAnonymousClass($memberUse->getCaller()->className)
|| (array_key_exists($memberUse->getCaller()->memberName, self::UNSUPPORTED_MAGIC_METHODS) && $memberUse instanceof ClassMethodCall);
return $memberUse->getOrigin() === null
|| $this->isAnonymousClass($memberUse->getOrigin()->className)
|| (array_key_exists($memberUse->getOrigin()->memberName, self::UNSUPPORTED_MAGIC_METHODS) && $memberUse instanceof ClassMethodCall);
}

/**
Expand Down Expand Up @@ -669,13 +669,13 @@ public function print(Output $output): void
}

/**
* @param list<ClassMemberUse> $uses
* @param list<ClassMemberUsage> $uses
*/
private function getExampleCaller(array $uses): ?string
{
foreach ($uses as $call) {
if ($call->getCaller() !== null) {
return $call->getCaller()->toHumanString();
if ($call->getOrigin() !== null) {
return $call->getOrigin()->toHumanString();
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/AllServicesInConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use ShipMonk\PHPStan\DeadCode\Crate\ClassConstantFetch;
use ShipMonk\PHPStan\DeadCode\Crate\ClassConstantRef;
use ShipMonk\PHPStan\DeadCode\Crate\ClassMemberRef;
use ShipMonk\PHPStan\DeadCode\Crate\ClassMemberUse;
use ShipMonk\PHPStan\DeadCode\Crate\ClassMemberUsage;
use ShipMonk\PHPStan\DeadCode\Crate\ClassMethodCall;
use ShipMonk\PHPStan\DeadCode\Crate\ClassMethodRef;
use ShipMonk\PHPStan\DeadCode\Crate\Kind;
Expand Down Expand Up @@ -54,7 +54,7 @@ public function test(): void
ClassConstantRef::class,
ClassConstantFetch::class,
ClassMemberRef::class,
ClassMemberUse::class,
ClassMemberUsage::class,
Kind::class,
Visibility::class,
MethodEntrypointProvider::class,
Expand Down
Loading

0 comments on commit 782ee7e

Please sign in to comment.