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

$bool = !!false;

?>
-----
<?php

$bool = false;

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

$bool = !!true;

?>
-----
<?php

$bool = true;

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

$bool = !!!false;

?>
-----
<?php

$bool = true;

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

$bool = !!!true;

?>
-----
<?php

$bool = false;

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\BooleanNot\ReplaceConstantBooleanNotRector\Fixture;

class IfCondition
{
public function run()
{
if (!true) {
return true;
}
return false;
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\BooleanNot\ReplaceConstantBooleanNotRector\Fixture;

class IfCondition
{
public function run()
{
if (false) {
return true;
}
return false;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

$bool = !false;

?>
-----
<?php

$bool = true;

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

$bool = !true;

?>
-----
<?php

$bool = false;

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\CodeQuality\Rector\BooleanNot\ReplaceConstantBooleanNotRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ReplaceConstantBooleanNotRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\BooleanNot\ReplaceConstantBooleanNotRector;
use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withRules([ReplaceConstantBooleanNotRector::class]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

namespace Rector\CodeQuality\Rector\BooleanNot;

use PhpParser\Node;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Name;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* Replace negated boolean literals with their simplified equivalents
*
* @see \Rector\Tests\CodeQuality\Rector\BooleanNot\ReplaceConstantBooleanNotRector\ReplaceConstantBooleanNotRectorTest
*/
final class ReplaceConstantBooleanNotRector extends AbstractRector
{
public function __construct(
private readonly ValueResolver $valueResolver
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replace negated boolean literals (!false, !true) with their simplified equivalents (true, false)',
[
new CodeSample(
<<<'CODE_SAMPLE'
if (!false) {
return 'always true';
}

if (!true) {
return 'never reached';
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
if (true) {
return 'always true';
}

if (false) {
return 'never reached';
}
CODE_SAMPLE
),

]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [BooleanNot::class];
}

/**
* @param BooleanNot $node
*/
public function refactor(Node $node): ?Node
{
if (! $node instanceof BooleanNot) {
return null;
}

if ($this->valueResolver->isFalse($node->expr)) {
return new ConstFetch(new Name('true'));
}

if ($this->valueResolver->isTrue($node->expr)) {
return new ConstFetch(new Name('false'));
}

return null;
}
}