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
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Expression\RemoveDeadStmtRector\Fixture;

final class SkipVoidCast
{
#[\NoDiscard('this value matters')]
public static function importantValue(): int
{
return 42;
}

public static function intentionallyDiscard(): void
{
// PHP 8.5: (void) explicitly discards a #[\NoDiscard] result, suppressing the warning.
(void) self::importantValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ final class RemoveDeadStmtRectorTest extends AbstractRectorTestCase
#[DataProvider('provideData')]
public function test(string $filePath): void
{
if (str_ends_with($filePath, 'skip_pipe_operator.php.inc') && PHP_VERSION_ID < 80500) {
if (
(
str_ends_with($filePath, 'skip_pipe_operator.php.inc')
||
str_ends_with($filePath, 'skip_void_cast.php.inc')
)
&& PHP_VERSION_ID < 80500) {
$this->markTestSkipped('test contains php 8.5 syntax early before transformation');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ private function isBuiltinReflection(FunctionReflection|MethodReflection $reflec
return $reflection->isBuiltin();
}

return $reflection->getDeclaringClass()->isBuiltin();
return $reflection->getDeclaringClass()
->isBuiltin();
}

/**
Expand Down
96 changes: 96 additions & 0 deletions rules/DeadCode/NodeAnalyzer/NoDiscardCallAnalyzer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

declare(strict_types=1);

namespace Rector\DeadCode\NodeAnalyzer;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PHPStan\Reflection\AttributeReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PHPStan\ScopeFetcher;

final readonly class NoDiscardCallAnalyzer
{
public function __construct(
private NodeNameResolver $nodeNameResolver,
private NodeTypeResolver $nodeTypeResolver,
private ReflectionProvider $reflectionProvider
) {
}

public function isNoDiscardCall(Expr $expr): bool
{
if ($expr instanceof FuncCall) {
$name = $this->nodeNameResolver->getName($expr);
if ($name === null) {
return false;
}

$scope = ScopeFetcher::fetch($expr);
$functionName = new Name($name);

if (! $this->reflectionProvider->hasFunction($functionName, $scope)) {
return false;
}

return $this->hasNoDiscardAttribute(
$this->reflectionProvider->getFunction($functionName, $scope)
->getAttributes()
);
}

if ($expr instanceof StaticCall) {
$classNames = $this->nodeTypeResolver->getType($expr->class)
->getObjectClassNames();
$methodName = $this->nodeNameResolver->getName($expr->name);
} elseif ($expr instanceof MethodCall || $expr instanceof NullsafeMethodCall) {
$classNames = $this->nodeTypeResolver->getType($expr->var)
->getObjectClassNames();
$methodName = $this->nodeNameResolver->getName($expr->name);
} else {
return false;
}

if ($classNames === [] || $methodName === null) {
return false;
}

foreach ($classNames as $className) {
if (! $this->reflectionProvider->hasClass($className)) {
continue;
}

$classReflection = $this->reflectionProvider->getClass($className);
if (! $classReflection->hasNativeMethod($methodName)) {
continue;
}

if ($this->hasNoDiscardAttribute($classReflection->getNativeMethod($methodName)->getAttributes())) {
return true;
}
}

return false;
}

/**
* @param AttributeReflection[] $attributes
*/
private function hasNoDiscardAttribute(array $attributes): bool
{
foreach ($attributes as $attribute) {
if ($attribute->getName() === 'NoDiscard') {
return true;
}
}

return false;
}
}
81 changes: 3 additions & 78 deletions rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,22 @@
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Include_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Function_;
use PhpParser\NodeVisitor;
use PHPStan\Reflection\AttributeReflection;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
use Rector\DeadCode\NodeAnalyzer\NoDiscardCallAnalyzer;
use Rector\DeadCode\SideEffect\SideEffectNodeDetector;
use Rector\NodeAnalyzer\VariableAnalyzer;
use Rector\NodeManipulator\StmtsManipulator;
use Rector\Php\ReservedKeywordAnalyzer;
use Rector\PhpParser\Enum\NodeGroup;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\MethodName;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -50,7 +44,7 @@ public function __construct(
private readonly VariableAnalyzer $variableAnalyzer,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly StmtsManipulator $stmtsManipulator,
private readonly ReflectionProvider $reflectionProvider,
private readonly NoDiscardCallAnalyzer $noDiscardCallAnalyzer,
) {
}

Expand Down Expand Up @@ -273,7 +267,7 @@ function (Node $subNode) use (&$refVariableNames) {
continue;
}

if ($this->isNoDiscardCall($assign->expr)) {
if ($this->noDiscardCallAnalyzer->isNoDiscardCall($assign->expr)) {
continue;
}

Expand All @@ -298,73 +292,4 @@ private function shouldSkipVariable(Variable $variable, string $variableName, ar

return in_array($variableName, $refVariableNames, true);
}

private function isNoDiscardCall(Expr $expr): bool
{
if ($expr instanceof FuncCall) {
$name = $this->getName($expr);

if ($name === null) {
return false;
}

$scope = ScopeFetcher::fetch($expr);

if (! $this->reflectionProvider->hasFunction(new Name($name), $scope)) {
return false;
}

return $this->hasNoDiscardAttribute(
$this->reflectionProvider->getFunction(new Name($name), $scope)
->getAttributes()
);
}

if ($expr instanceof StaticCall) {
$classNames = $this->getType($expr->class)
->getObjectClassNames();
$methodName = $this->getName($expr->name);
} elseif ($expr instanceof MethodCall || $expr instanceof NullsafeMethodCall) {
$classNames = $this->getType($expr->var)
->getObjectClassNames();
$methodName = $this->getName($expr->name);
} else {
return false;
}

if ($classNames === [] || $methodName === null) {
return false;
}

foreach ($classNames as $className) {
if (! $this->reflectionProvider->hasClass($className)) {
continue;
}

$classReflection = $this->reflectionProvider->getClass($className);
if (! $classReflection->hasNativeMethod($methodName)) {
continue;
}

if ($this->hasNoDiscardAttribute($classReflection->getNativeMethod($methodName)->getAttributes())) {
return true;
}
}

return false;
}

/**
* @param AttributeReflection[] $attributes
*/
private function hasNoDiscardAttribute(array $attributes): bool
{
foreach ($attributes as $attribute) {
if ($attribute->getName() === 'NoDiscard') {
return true;
}
}

return false;
}
}
16 changes: 16 additions & 0 deletions rules/DeadCode/Rector/Expression/RemoveDeadStmtRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Expr\Cast\Void_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Nop;
use PhpParser\NodeVisitor;
use PHPStan\Reflection\Php\PhpPropertyReflection;
use Rector\DeadCode\NodeAnalyzer\NoDiscardCallAnalyzer;
use Rector\DeadCode\NodeManipulator\LivingCodeManipulator;
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Rector\AbstractRector;
Expand All @@ -24,6 +26,7 @@ final class RemoveDeadStmtRector extends AbstractRector
{
public function __construct(
private readonly LivingCodeManipulator $livingCodeManipulator,
private readonly NoDiscardCallAnalyzer $noDiscardCallAnalyzer,
private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer,
private readonly ReflectionResolver $reflectionResolver,
) {
Expand Down Expand Up @@ -63,6 +66,10 @@ public function refactor(Node $node): array|Node|null|int
return null;
}

if ($this->isVoidCastNoDiscardCall($node)) {
return null;
}

$livingCode = $this->livingCodeManipulator->keepLivingCodeFromExpr($node->expr);
if ($livingCode === []) {
return $this->removeNodeAndKeepComments($node);
Expand Down Expand Up @@ -101,6 +108,15 @@ private function hasGetMagic(Expression $expression): bool
return ! $phpPropertyReflection instanceof PhpPropertyReflection;
}

private function isVoidCastNoDiscardCall(Expression $expression): bool
{
if (! $expression->expr instanceof Void_) {
return false;
}

return $this->noDiscardCallAnalyzer->isNoDiscardCall($expression->expr->expr);
}

/**
* @return NodeVisitor::REMOVE_NODE|Node
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ public function refactor(Node $node): ?Node

private function removeDuplicatedCases(Switch_ $switch): void
{
/** @var Case_[] */
/** @var Case_[] $result */
$result = [];

/** @var int[] */
/** @var int[] $processedCasesKeys */
$processedCasesKeys = [];

foreach ($switch->cases as $outerCaseKey => $outerCase) {
Expand All @@ -123,7 +123,7 @@ private function removeDuplicatedCases(Switch_ $switch): void
/** @var array<int, Case_> */
$casesWithoutStmts = [];

/** @var Case_[] */
/** @var Case_[] $equalCases */
$equalCases = [];

foreach ($switch->cases as $innerCaseKey => $innerCase) {
Expand Down
Loading