Skip to content

Commit

Permalink
UnusedPrivatePropertyRule - recognize static properties fetched on expr
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jun 8, 2023
1 parent dcc7b7c commit 503978b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/Rules/DeadCode/UnusedPrivateMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ public function processNode(Node $node, Scope $scope): array
if ($methodCallNode instanceof Node\Expr\MethodCall) {
$calledOnType = $callScope->getType($methodCallNode->var);
} else {
if (!$methodCallNode->class instanceof Node\Name) {
continue;
if ($methodCallNode->class instanceof Node\Name) {
$calledOnType = $callScope->resolveTypeByName($methodCallNode->class);
} else {
$calledOnType = $callScope->getType($methodCallNode->class);
}
$calledOnType = $scope->resolveTypeByName($methodCallNode->class);
}

$inMethod = $callScope->getFunction();
Expand Down
8 changes: 4 additions & 4 deletions src/Rules/DeadCode/UnusedPrivatePropertyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ public function processNode(Node $node, Scope $scope): array
if ($fetch instanceof Node\Expr\PropertyFetch) {
$fetchedOnType = $usage->getScope()->getType($fetch->var);
} else {
if (!$fetch->class instanceof Node\Name) {
continue;
if ($fetch->class instanceof Node\Name) {
$fetchedOnType = $usage->getScope()->resolveTypeByName($fetch->class);
} else {
$fetchedOnType = $usage->getScope()->getType($fetch->class);
}

$fetchedOnType = $usage->getScope()->resolveTypeByName($fetch->class);
}

foreach ($propertyNames as $propertyName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,11 @@ public function testBug8850(): void
$this->analyse([__DIR__ . '/data/bug-8850.php'], []);
}

public function testBug9409(): void
{
$this->alwaysWrittenTags = [];
$this->alwaysReadTags = [];
$this->analyse([__DIR__ . '/data/bug-9409.php'], []);
}

}
22 changes: 22 additions & 0 deletions tests/PHPStan/Rules/DeadCode/data/bug-9409.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Bug9409;

class HelloWorld
{
/** @var array<string,string|null> $tempDir */
private static array $tempDir = [];

public function getTempDir(string $name): string|null
{
if (isset($this::$tempDir[$name])) {
return $this::$tempDir[$name];
}

$path = '';

$this::$tempDir[$name] = $path;

return $path;
}
}
15 changes: 15 additions & 0 deletions tests/PHPStan/Rules/DeadCode/data/unused-private-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,18 @@ private function doLorem()
}

}

class StaticMethod
{

private static function doFoo(): void
{

}

public function doTest(): void
{
$this::doFoo();
}

}

0 comments on commit 503978b

Please sign in to comment.