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,21 @@
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector\Fixture;

trait SkipBooleanAndWithFallbackStringOnTrait
{
public function check($id): string
{
$usage = $this->options['usage'] ?? 'create';

if ($usage === 'update' && ! $id) {
return 'missing';
}

if ($usage === 'create' && $id) {
return 'exists';
}

return 'ok';
}
}
13 changes: 7 additions & 6 deletions rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ public function refactor(Node $node): int|null|array|If_
return null;
}

// types inside a trait are resolved without the class using it, so they are unreliable
$scope = ScopeFetcher::fetch($node);
$classReflection = $scope->getClassReflection();
if ($classReflection instanceof ClassReflection && $classReflection->isTrait()) {
return null;
}

if ($node->cond instanceof BooleanAnd) {
return $this->refactorIfWithBooleanAnd($node);
}
Expand All @@ -135,7 +142,6 @@ public function refactor(Node $node): int|null|array|If_
return null;
}

$scope = ScopeFetcher::fetch($node);
if ($this->shouldSkipFromVariable($node->cond, $scope)) {
return null;
}
Expand All @@ -150,11 +156,6 @@ public function refactor(Node $node): int|null|array|If_
return null;
}

$classReflection = $scope->getClassReflection();
if ($classReflection instanceof ClassReflection && $classReflection->isTrait()) {
return null;
}

if ($node->stmts === []) {
return NodeVisitor::REMOVE_NODE;
}
Expand Down
Loading