Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DeadCode] Skip isset() from property fetch from docblock on RemoveAl…
…waysTrueIfConditionRector (#5754) * [DeadCode] Skip isset() from property fetch from docblock on RemoveAlwaysTrueIfConditionRector * Fix * skip on array dim fetch as well * skip on array dim fetch as well * skip on array dim fetch as well * fix * Add SafeLeftTypeBooleanAndOrAnalyzer
- Loading branch information
1 parent
dc69b1a
commit b4eb883
Showing
5 changed files
with
92 additions
and
12 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
...Code/Rector/If_/RemoveAlwaysTrueIfConditionRector/Fixture/skip_property_fetch_doc.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector\Fixture; | ||
|
||
class SkipPropertyFetchDoc | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public $bodyFormat = ''; | ||
|
||
public function run() | ||
{ | ||
if (isset($this->bodyFormat) && $this->bodyFormat !== '') { | ||
} | ||
} | ||
|
||
public function reset() | ||
{ | ||
$this->bodyFormat = null; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...ode/Rector/If_/RemoveAlwaysTrueIfConditionRector/Fixture/skip_property_fetch_doc2.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector\Fixture; | ||
|
||
class SkipPropertyFetchDoc2 | ||
{ | ||
/** | ||
* @var array<int, int> | ||
*/ | ||
public $data = []; | ||
|
||
public function run($key) | ||
{ | ||
if (is_int($this->data[$key]) && $this->data[$key] > 0) { | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
rules/DeadCode/NodeAnalyzer/SafeLeftTypeBooleanAndOrAnalyzer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\DeadCode\NodeAnalyzer; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ArrayDimFetch; | ||
use PhpParser\Node\Expr\BinaryOp\BooleanAnd; | ||
use PhpParser\Node\Expr\BinaryOp\BooleanOr; | ||
use PhpParser\Node\Expr\PropertyFetch; | ||
use PhpParser\Node\Expr\StaticPropertyFetch; | ||
use PhpParser\Node\Expr\Variable; | ||
use Rector\NodeAnalyzer\ExprAnalyzer; | ||
use Rector\PhpParser\Node\BetterNodeFinder; | ||
|
||
final readonly class SafeLeftTypeBooleanAndOrAnalyzer | ||
{ | ||
public function __construct( | ||
private readonly BetterNodeFinder $betterNodeFinder, | ||
private readonly ExprAnalyzer $exprAnalyzer | ||
) { | ||
} | ||
|
||
public function isSafe(BooleanAnd|BooleanOr $booleanAnd): bool | ||
{ | ||
$hasNonTypedFromParam = (bool) $this->betterNodeFinder->findFirst( | ||
$booleanAnd->left, | ||
fn (Node $node): bool => $node instanceof Variable && $this->exprAnalyzer->isNonTypedFromParam($node) | ||
); | ||
|
||
if ($hasNonTypedFromParam) { | ||
return false; | ||
} | ||
|
||
// get type from Property and ArrayDimFetch is unreliable | ||
return ! (bool) $this->betterNodeFinder->findFirst( | ||
$booleanAnd->left, | ||
static fn (Node $node): bool => $node instanceof PropertyFetch || $node instanceof StaticPropertyFetch || $node instanceof ArrayDimFetch | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters