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
2 changes: 2 additions & 0 deletions .github/typos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ nd = "nd"

# plurals
Identicals = "Identicals"
Ands = "Ands"
Ors = "Ors"

# native function names
writeable = "writeable"
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

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

class EmptyInArray
{
public static function validateAssetDownload($assetId, array $limitToAssets): bool
{
return !(!empty($limitToAssets) && !in_array($assetId, $limitToAssets));
}
}

?>
-----
<?php

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

class EmptyInArray
{
public static function validateAssetDownload($assetId, array $limitToAssets): bool
{
return empty($limitToAssets) || in_array($assetId, $limitToAssets);
}
}

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

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

class Fixture
{
public function run($a, $b, $c)
{
$result = !($a > 20 && $b <= 50);
$d = !($a === null && $b < $c);
}
}

?>
-----
<?php

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

class Fixture
{
public function run($a, $b, $c)
{
$result = $a <= 20 || $b > 50;
$d = $a !== null || $b >= $c;
}
}

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

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

class SkipNestedAnd
{
public function run($a, $b, $c)
{
$result = !($a > 20 && $b < $c && $a !== null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

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

class SkipOr
{
public function run($a, $b)
{
$result = !($a > 20 || $b <= 50);
}
}
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\NegatedAndsToPositiveOrsRector;

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

final class NegatedAndsToPositiveOrsRectorTest 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\NegatedAndsToPositiveOrsRector;
use Rector\Config\RectorConfig;

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

declare(strict_types=1);

namespace Rector\CodeQuality\Rector\BooleanNot;

use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BooleanNot;
use Rector\NodeManipulator\BinaryOpManipulator;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\CodeQuality\Rector\BooleanNot\NegatedAndsToPositiveOrsRector\NegatedAndsToPositiveOrsRectorTest
*/
final class NegatedAndsToPositiveOrsRector extends AbstractRector
{
public function __construct(
private readonly BinaryOpManipulator $binaryOpManipulator
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Simplify negated "and" conditions to "or" with de Morgan theorem',
[
new CodeSample(
<<<'CODE_SAMPLE'
$a = 5;
$b = 10;
$result = !($a > 20 && $b <= 50);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$a = 5;
$b = 10;
$result = $a <= 20 || $b > 50;
CODE_SAMPLE
),
]
);
}

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

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

return $this->binaryOpManipulator->inverseBooleanAnd($node->expr);
}
}
2 changes: 1 addition & 1 deletion rules/DeadCode/Rector/TryCatch/RemoveDeadCatchRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private function isJustThrownSameVariable(Catch_ $catch): bool
}

$catchItemStmt = $catch->stmts[0];
if (! ($catchItemStmt instanceof Expression && $catchItemStmt->expr instanceof Throw_)) {
if (!$catchItemStmt instanceof Expression || !$catchItemStmt->expr instanceof Throw_) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function refactor(Node $node): array|null|int
}

$onlyCatchStmt = $onlyCatch->stmts[0];
if (! ($onlyCatchStmt instanceof Expression && $onlyCatchStmt->expr instanceof Throw_)) {
if (!$onlyCatchStmt instanceof Expression || !$onlyCatchStmt->expr instanceof Throw_) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion rules/Php70/EregToPcreTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ private function processSquareBracket(string $s, int $i, int $l, string $cls, bo
} else {
$a = $s[$i];
++$i;
if ($a === '-' && ! $start && ! ($i < $l && $s[$i] === ']')) {
if ($a === '-' && ! $start && ($i >= $l || $s[$i] !== ']')) {
throw new InvalidEregException('"-" is invalid for the start character in the brackets');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private function isFollowedByCurlyBracket(File $file, ArrayDimFetch $arrayDimFet

if (isset($oldTokens[$endTokenPost]) && (string) $oldTokens[$endTokenPost] === '}') {
$startTokenPos = $arrayDimFetch->getStartTokenPos();
return ! (isset($oldTokens[$startTokenPos]) && (string) $oldTokens[$startTokenPos] === '${');
return !isset($oldTokens[$startTokenPos]) || (string) $oldTokens[$startTokenPos] !== '${';
}

return false;
Expand Down
2 changes: 1 addition & 1 deletion rules/Php80/NodeAnalyzer/MatchSwitchAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function shouldSkipSwitch(Switch_ $switch, array $condAndExprs, ?Stmt $ne
return false;
}

return ! ($nextStmt instanceof Expression && $nextStmt->expr instanceof Throw_);
return !$nextStmt instanceof Expression || !$nextStmt->expr instanceof Throw_;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function refactor(Node $node): ?Node
$hasChanged = false;

foreach ($node->parts as $part) {
if (! $part instanceof Variable && ! ($part instanceof ArrayDimFetch && $part->var instanceof Variable)) {
if (! $part instanceof Variable && (!$part instanceof ArrayDimFetch || !$part->var instanceof Variable)) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private function removeByName(ClassMethod | StaticCall | MethodCall $node, int $
return;
}

if (! (isset($node->params[$position]) && $this->isName($node->params[$position], $name))) {
if (!isset($node->params[$position]) || !$this->isName($node->params[$position], $name)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private function hasAlwaysStringScalarReturn(array $returns): bool
{
return array_all(
$returns,
fn (Return_ $return): bool => ! (! $return->expr instanceof String_ && ! $return->expr instanceof InterpolatedString)
fn (Return_ $return): bool => $return->expr instanceof String_ || $return->expr instanceof InterpolatedString
);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Config/Level/CodeQualityLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Rector\CodeQuality\Rector\BooleanAnd\RemoveUselessIsObjectCheckRector;
use Rector\CodeQuality\Rector\BooleanAnd\RepeatedAndNotEqualToNotInArrayRector;
use Rector\CodeQuality\Rector\BooleanAnd\SimplifyEmptyArrayCheckRector;
use Rector\CodeQuality\Rector\BooleanNot\NegatedAndsToPositiveOrsRector;
use Rector\CodeQuality\Rector\BooleanNot\ReplaceConstantBooleanNotRector;
use Rector\CodeQuality\Rector\BooleanNot\ReplaceMultipleBooleanNotRector;
use Rector\CodeQuality\Rector\BooleanNot\SimplifyDeMorganBinaryRector;
Expand Down Expand Up @@ -132,6 +133,7 @@ final class CodeQualityLevel
UnnecessaryTernaryExpressionRector::class,
RemoveExtraParametersRector::class,
SimplifyDeMorganBinaryRector::class,
NegatedAndsToPositiveOrsRector::class,
SimplifyTautologyTernaryRector::class,
SingleInArrayToCompareRector::class,
SimplifyIfElseToTernaryRector::class,
Expand Down
17 changes: 17 additions & 0 deletions src/NodeManipulator/BinaryOpManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ public function inverseBooleanOr(BooleanOr $booleanOr): ?BinaryOp
return new $inversedNodeClass($firstInversedExpr, $secondInversedExpr);
}

public function inverseBooleanAnd(BooleanAnd $booleanAnd): ?BinaryOp
{
// no nesting
if ($booleanAnd->left instanceof BooleanAnd) {
return null;
}

if ($booleanAnd->right instanceof BooleanAnd) {
return null;
}

$firstInversedExpr = $this->inverseNode($booleanAnd->left);
$secondInversedExpr = $this->inverseNode($booleanAnd->right);

return new BooleanOr($firstInversedExpr, $secondInversedExpr);
}

public function invertCondition(BinaryOp $binaryOp): ?BinaryOp
{
// no nesting
Expand Down
Loading