add isNull() to ConstFetchAnalyzer

This commit is contained in:
Tomas Votruba 2018-10-21 14:10:19 +02:00
parent 2723f5ab8a
commit 6fa1d53b1c
4 changed files with 34 additions and 31 deletions

View File

@ -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')));
}

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -38,4 +38,9 @@ trait ConstFetchAnalyzerTrait
{
return $this->constFetchAnalyzer->isBool($node);
}
public function isNull(Node $node): bool
{
return $this->constFetchAnalyzer->isNull($node);
}
}