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
20 changes: 15 additions & 5 deletions packages/CodeQuality/src/Rector/If_/SimplifyIfReturnBoolRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BooleanNot;
Expand Down Expand Up @@ -189,11 +190,7 @@ private function boolCastOrNullCompareIfNeeded(Expr $expr): Expr
}
}

if ($expr instanceof BooleanNot) {
return $expr;
}

if ($this->isStaticType($expr, BooleanType::class)) {
if (! $this->isBoolCastNeeded($expr)) {
return $expr;
}

Expand Down Expand Up @@ -235,4 +232,17 @@ private function removeNullTypeFromUnionType(UnionType $unionType): Type

return TypeFactoryStaticHelper::createUnionObjectType($unionedTypesWithoutNullType);
}

private function isBoolCastNeeded(Expr $expr): bool
{
if ($expr instanceof BooleanNot) {
return false;
}

if ($this->isStaticType($expr, BooleanType::class)) {
return false;
}

return ! $expr instanceof BinaryOp;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ final class AbsolutizeRequireAndIncludePathRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('include/require to absolute path. This Rector might introduce backwards incompatible code, when the include/require beeing changed depends on the current working directory.', [
new CodeSample(
<<<'PHP'
return new RectorDefinition(
'include/require to absolute path. This Rector might introduce backwards incompatible code, when the include/require beeing changed depends on the current working directory.',
[
new CodeSample(
<<<'PHP'
class SomeClass
{
public function run()
Expand All @@ -37,7 +39,7 @@ public function run()
}
PHP
,
<<<'PHP'
<<<'PHP'
class SomeClass
{
public function run()
Expand All @@ -49,8 +51,9 @@ public function run()
}
PHP

),
]);
),
]
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\CodeQuality\Tests\Rector\If_\SimplifyIfReturnBoolRector\Fixture;

use PhpParser\Node\Stmt\If_;

class IdenticalNonCast
{
public function run(If_ $if)
{
if ($if->else === null) {
return false;
}

return true;
}
}

?>
-----
<?php

namespace Rector\CodeQuality\Tests\Rector\If_\SimplifyIfReturnBoolRector\Fixture;

use PhpParser\Node\Stmt\If_;

class IdenticalNonCast
{
public function run(If_ $if)
{
return $if->else !== null;
}
}

?>
6 changes: 1 addition & 5 deletions src/PhpParser/Node/Manipulator/IfManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,7 @@ public function isEarlyElse(If_ $if): bool
}
}

if ($if->else === null) {
return false;
}

return true;
return $if->else !== null;
}

private function matchComparedAndReturnedNode(NotIdentical $notIdentical, Return_ $returnNode): ?Expr
Expand Down