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
8 changes: 4 additions & 4 deletions rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ public function refactor(Node $node): ?Node
continue;
}

if (! $stmt->expr->var instanceof Variable && ! $stmt->expr->var instanceof PropertyFetch && ! $stmt->expr->var instanceof StaticPropertyFetch) {
continue;
}

// early check self referencing, ensure that variable not re-used
if ($this->isSelfReferencing($nextAssign)) {
continue;
Expand Down Expand Up @@ -116,10 +120,6 @@ public function refactor(Node $node): ?Node
continue;
}

if (! $stmt->expr->var instanceof Variable && ! $stmt->expr->var instanceof PropertyFetch && ! $stmt->expr->var instanceof StaticPropertyFetch) {
continue;
}

// side effect may throw exception, and may be handled by catch block
if ($node instanceof TryCatch && $this->sideEffectNodeDetector->detectCallExpr($nextAssign->expr)) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ public function refactor(Node $node): ?Node

$constantFilterName = $secondArgValue->name->toString();

if (! in_array(
$constantFilterName,
['FILTER_VALIDATE_INT', 'FILTER_VALIDATE_FLOAT', 'FILTER_VALIDATE_BOOLEAN', 'FILTER_VALIDATE_BOOL'],
true
)) {
return null;
}

$valueType = $this->nodeTypeResolver->getNativeType($firstArgValue);

if ($constantFilterName === 'FILTER_VALIDATE_INT' && $valueType->isInteger()->yes()) {
Expand Down
8 changes: 4 additions & 4 deletions rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,13 @@ private function isInstanceofTheSameType(Instanceof_ $instanceof): ?bool
return null;
}

$classReflection = $this->reflectionResolver->resolveClassReflection($instanceof);
if ($classReflection instanceof ClassReflection && $classReflection->isTrait()) {
$exprType = $this->nodeTypeResolver->getNativeType($instanceof->expr);
if (! $exprType instanceof ObjectType) {
return null;
}

$exprType = $this->nodeTypeResolver->getNativeType($instanceof->expr);
if (! $exprType instanceof ObjectType) {
$classReflection = $this->reflectionResolver->resolveClassReflection($instanceof);
if ($classReflection instanceof ClassReflection && $classReflection->isTrait()) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,6 @@ public function refactor(Node $node): ?Node
$hasChanged = false;

foreach ($node->stmts as $key => $stmt) {
$variableName = $this->matchOverridableAssignVariableName($stmt);
if ($variableName === null) {
continue;
}

$nextStmt = $node->stmts[$key + 1] ?? null;
if (! $nextStmt instanceof If_) {
continue;
Expand All @@ -112,6 +107,11 @@ public function refactor(Node $node): ?Node
continue;
}

$variableName = $this->matchOverridableAssignVariableName($stmt);
if ($variableName === null) {
continue;
}

// the initial value is still read when the condition uses it
if ($this->isVariableUsedInNode($nextStmt->cond, $variableName)) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ private function areNumberType(BinaryOp $binaryOp): bool

private function processBinaryPlusAndMinus(Plus|Minus $binaryOp): ?Expr
{
if (! $this->isLiteralZero($binaryOp->left) && ! $this->isLiteralZero($binaryOp->right)) {
return null;
}

if (! $this->areNumberType($binaryOp)) {
return null;
}
Expand Down Expand Up @@ -229,6 +233,10 @@ private function processBinaryMulAndDiv(Mul|Div $binaryOp): ?Expr
return null;
}

if (! $this->isLiteralOne($binaryOp->left) && ! $this->isLiteralOne($binaryOp->right)) {
return null;
}

if (! $this->areNumberType($binaryOp)) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ private function isParentStaticCall(Expr $expr): bool

private function shouldSkipClass(Class_ $class): bool
{
// skip cases when parent class reflection is not found
if ($class->extends instanceof FullyQualified && ! $this->reflectionProvider->hasClass(
$class->extends->toString()
)) {
// currently the classMethodManipulator isn't able to find usages of anonymous classes
if ($this->classAnalyzer->isAnonymousClass($class)) {
return true;
}

// currently the classMethodManipulator isn't able to find usages of anonymous classes
return $this->classAnalyzer->isAnonymousClass($class);
// skip cases when parent class reflection is not found
return $class->extends instanceof FullyQualified && ! $this->reflectionProvider->hasClass(
$class->extends->toString()
);
}

private function doesCalledMethodExistInParent(StaticCall $staticCall, Class_ $class): bool
Expand Down
16 changes: 8 additions & 8 deletions rules/DeadCode/Rector/Stmt/RemoveConditionExactReturnRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,26 +95,26 @@ public function refactor(Node $node): ?Node

$identicalOrEqual = $stmt->cond;

if ($this->sideEffectNodeDetector->detect($identicalOrEqual->right)) {
// next if must be return of the same var
$nextStmt = $node->stmts[$key + 1] ?? null;
if (! $nextStmt instanceof Return_) {
continue;
}

if (! $this->nodeComparator->areNodesEqual($identicalOrEqual->right, $soleIfReturn->expr)) {
if (! $nextStmt->expr instanceof Expr) {
continue;
}

$comparedVariable = $identicalOrEqual->left;

// next if must be return of the same var
$nextStmt = $node->stmts[$key + 1] ?? null;
if (! $nextStmt instanceof Return_) {
if ($this->sideEffectNodeDetector->detect($identicalOrEqual->right)) {
continue;
}

if (! $nextStmt->expr instanceof Expr) {
if (! $this->nodeComparator->areNodesEqual($identicalOrEqual->right, $soleIfReturn->expr)) {
continue;
}

$comparedVariable = $identicalOrEqual->left;

if (! $this->nodeComparator->areNodesEqual($nextStmt->expr, $comparedVariable)) {
continue;
}
Expand Down
Loading