From 6fa1d53b1cd452f37e1c8b86432550c4332d5cb7 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 21 Oct 2018 14:10:19 +0200 Subject: [PATCH] add isNull() to ConstFetchAnalyzer --- examples/MergeIsCandidateRector.php | 4 +- .../UnnecessaryTernaryExpressionRector.php | 14 ++----- src/NodeAnalyzer/ConstFetchAnalyzer.php | 42 +++++++++++-------- src/Rector/ConstFetchAnalyzerTrait.php | 5 +++ 4 files changed, 34 insertions(+), 31 deletions(-) diff --git a/examples/MergeIsCandidateRector.php b/examples/MergeIsCandidateRector.php index a48b606f060..5dec6a3e6aa 100644 --- a/examples/MergeIsCandidateRector.php +++ b/examples/MergeIsCandidateRector.php @@ -244,11 +244,11 @@ final class MergeIsCandidateRector extends AbstractRector private function replaceReturnFalseWithReturnNull(ClassMethod $classMethod): void { $this->callbackNodeTraverser->traverseNodesWithCallable([$classMethod], function (Node $node): ?Node { - if (!$node instanceof Return_ || !$node->expr instanceof ConstFetch) { + if (! $node instanceof Return_ || ! $node->expr instanceof ConstFetch) { return null; } - if ((string) $node->expr->name === 'false') { + if ($this->isFalse($node->expr)) { return new Return_(new ConstFetch(new Name('null'))); } diff --git a/packages/CodeQuality/src/Rector/Ternary/UnnecessaryTernaryExpressionRector.php b/packages/CodeQuality/src/Rector/Ternary/UnnecessaryTernaryExpressionRector.php index b38440bd6d7..1f1c9f0873b 100644 --- a/packages/CodeQuality/src/Rector/Ternary/UnnecessaryTernaryExpressionRector.php +++ b/packages/CodeQuality/src/Rector/Ternary/UnnecessaryTernaryExpressionRector.php @@ -15,7 +15,6 @@ use PhpParser\Node\Expr\BinaryOp\Smaller; use PhpParser\Node\Expr\BinaryOp\SmallerOrEqual; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\Ternary; -use PhpParser\Node\Identifier; use Rector\Exception\NotImplementedException; use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; @@ -76,21 +75,14 @@ final class UnnecessaryTernaryExpressionRector extends AbstractRector return null; } - /** @var Identifier $ifExpressionName */ - $ifExpressionName = $ifExpression->name; - - /** @var Identifier $elseExpressionName */ - $elseExpressionName = $elseExpression->name; - - $ifValue = $ifExpressionName->toLowerString(); - $elseValue = $elseExpressionName->toLowerString(); - if (in_array('null', [$ifValue, $elseValue], true)) { + if ($this->isNull($ifExpression) || $this->isNull($elseExpression)) { return null; } + /** @var BinaryOp $binaryOperation */ $binaryOperation = $node->cond; - if ($ifValue === 'true' && $elseValue === 'false') { + if ($this->isTrue($ifExpression) && $this->isFalse($elseExpression)) { return $binaryOperation; } diff --git a/src/NodeAnalyzer/ConstFetchAnalyzer.php b/src/NodeAnalyzer/ConstFetchAnalyzer.php index 2ad9c8e29df..d6ca6fbb692 100644 --- a/src/NodeAnalyzer/ConstFetchAnalyzer.php +++ b/src/NodeAnalyzer/ConstFetchAnalyzer.php @@ -11,26 +11,32 @@ use PhpParser\Node\Expr\ConstFetch; */ final class ConstFetchAnalyzer { - public function isFalse(Node $node): bool - { - if (! $node instanceof ConstFetch) { - return false; - } - - return $node->name->toLowerString() === 'false'; - } - - public function isTrue(Node $node): bool - { - if (! $node instanceof ConstFetch) { - return false; - } - - return $node->name->toLowerString() === 'true'; - } - public function isBool(Node $node): bool { return $this->isTrue($node) || $this->isFalse($node); } + + public function isFalse(Node $node): bool + { + return $this->isConstantWithLowercasedName($node, 'false'); + } + + public function isTrue(Node $node): bool + { + return $this->isConstantWithLowercasedName($node, 'true'); + } + + public function isNull(Node $node): bool + { + return $this->isConstantWithLowercasedName($node, 'null'); + } + + private function isConstantWithLowercasedName(Node $node, string $name): bool + { + if (! $node instanceof ConstFetch) { + return false; + } + + return $node->name->toLowerString() === $name; + } } diff --git a/src/Rector/ConstFetchAnalyzerTrait.php b/src/Rector/ConstFetchAnalyzerTrait.php index 0254365ff10..87fdf106d5b 100644 --- a/src/Rector/ConstFetchAnalyzerTrait.php +++ b/src/Rector/ConstFetchAnalyzerTrait.php @@ -38,4 +38,9 @@ trait ConstFetchAnalyzerTrait { return $this->constFetchAnalyzer->isBool($node); } + + public function isNull(Node $node): bool + { + return $this->constFetchAnalyzer->isNull($node); + } }