Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Rules/DeadCode/UnusedPrivateMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,16 @@ public function processNode(Node $node, Scope $scope): array
$methodNameType = $callScope->getType($methodCallNode->name);
$strings = $methodNameType->getConstantStrings();
if (count($strings) === 0) {
return [];
// handle subtractions of a dynamic method call
foreach ($methods as $lowerMethodName => $method) {
if ((new ConstantStringType($method->getNode()->name->toString()))->isSuperTypeOf($methodNameType)->no()) {
continue;
}

unset($methods[$lowerMethodName]);
}

continue;
}

$methodNames = array_map(static fn (ConstantStringType $type): string => $type->getValue(), $strings);
Expand Down
11 changes: 10 additions & 1 deletion src/Rules/DeadCode/UnusedPrivatePropertyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,16 @@ public function processNode(Node $node, Scope $scope): array
$propertyNameType = $usage->getScope()->getType($fetch->name);
$strings = $propertyNameType->getConstantStrings();
if (count($strings) === 0) {
return [];
// handle subtractions of a dynamic property fetch
foreach ($properties as $propertyName => $data) {
if ((new ConstantStringType($propertyName))->isSuperTypeOf($propertyNameType)->no()) {
continue;
}

unset($properties[$propertyName]);
}

continue;
}

$propertyNames = array_map(static fn (ConstantStringType $type): string => $type->getValue(), $strings);
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/DeadCode/UnusedPrivateMethodRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,14 @@ public function testBug9765(): void
$this->analyse([__DIR__ . '/data/bug-9765.php'], []);
}

public function testBug11802(): void
{
$this->analyse([__DIR__ . '/data/bug-11802b.php'], [
[
'Method Bug11802b\HelloWorld::doBar() is unused.',
10,
],
]);
}

}
15 changes: 15 additions & 0 deletions tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,4 +336,19 @@ public function testBug7251(): void
$this->analyse([__DIR__ . '/data/bug-7251.php'], []);
}

public function testBug11802(): void
{
$tip = 'See: https://phpstan.org/developing-extensions/always-read-written-properties';

$this->alwaysWrittenTags = [];
$this->alwaysReadTags = [];
$this->analyse([__DIR__ . '/data/bug-11802.php'], [
[
'Property Bug11802\HelloWorld::$isFinal is never read, only written.',
8,
$tip,
],
]);
}

}
20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/DeadCode/data/bug-11802.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php // lint >= 8.0

namespace Bug11802;

class HelloWorld
{
public function __construct(
private bool $isFinal,
private bool $used
)
{
}

public function doFoo(HelloWorld $x, $y): void
{
if ($y !== 'isFinal') {
$s = $x->{$y};
}
}
}
19 changes: 19 additions & 0 deletions tests/PHPStan/Rules/DeadCode/data/bug-11802b.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php // lint >= 8.0

namespace Bug11802b;

class HelloWorld
{
public function __construct(
) {}

private function doBar():void {}

private function doFooBar():void {}

public function doFoo(HelloWorld $x, $y): void {
if ($y !== 'doBar') {
$s = $x->$y();
}
}
}
Loading