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
8 changes: 4 additions & 4 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use Rector\CodingStyle\Rector\String_\UseClassKeywordForClassNameResolutionRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector;
use Rector\Naming\Rector\Assign\RenameVariableToMatchMethodCallReturnTypeRector;
use Rector\Naming\Rector\Class_\RenamePropertyToMatchTypeRector;
use Rector\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector;
use Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector;

return static function (RectorConfig $rectorConfig): void {
Expand Down Expand Up @@ -80,13 +82,11 @@
],

// race condition with stmts aware patch and PHPStan type
\Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector::class => [
AddMethodCallBasedStrictParamTypeRector::class => [
__DIR__ . '/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php',
],

\Rector\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector::class => [
__DIR__ . '/src/Util/FileHasher.php',
],
RemovePhpVersionIdCheckRector::class => [__DIR__ . '/src/Util/FileHasher.php'],
]);

$rectorConfig->phpstanConfig(__DIR__ . '/phpstan-for-rector.neon');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public function refactor(Node $node): ?Node
return null;
}

$hasChanged = false;

foreach ($node->stmts as $key => $stmt) {
if (! $stmt instanceof TryCatch) {
continue;
Expand All @@ -99,10 +101,15 @@ public function refactor(Node $node): ?Node
}

$catch->var = null;
$hasChanged = true;
}
}

return $node;
if ($hasChanged) {
return $node;
}

return null;
}

public function provideMinPhpVersion(): int
Expand Down
20 changes: 20 additions & 0 deletions rules/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Empty_;
use PhpParser\Node\Expr\Isset_;
use PHPStan\Analyser\Scope;
use Rector\Strict\NodeFactory\ExactCompareFactory;
use Rector\Strict\Rector\AbstractFalsyScalarRuleFixerRector;
Expand Down Expand Up @@ -91,6 +93,10 @@ private function refactorBooleanNot(BooleanNot $booleanNot, Scope $scope): Expr|
}

$empty = $booleanNot->expr;
if ($empty->expr instanceof ArrayDimFetch) {
return $this->createDimFetchBooleanAnd($empty);
}

$emptyExprType = $scope->getType($empty->expr);

return $this->exactCompareFactory->createNotIdenticalFalsyCompare(
Expand All @@ -105,4 +111,18 @@ private function refactorEmpty(Empty_ $empty, Scope $scope, bool $treatAsNonEmpt
$exprType = $scope->getType($empty->expr);
return $this->exactCompareFactory->createIdenticalFalsyCompare($exprType, $empty->expr, $treatAsNonEmpty);
}

private function createDimFetchBooleanAnd(Empty_ $empty): ?BooleanAnd
{
$exprType = $this->getType($empty->expr);

$isset = new Isset_([$empty->expr]);
$compareExpr = $this->exactCompareFactory->createNotIdenticalFalsyCompare($exprType, $empty->expr, false);

if (! $compareExpr instanceof Expr) {
return null;
}

return new BooleanAnd($isset, $compareExpr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,5 @@
use Rector\Php80\Rector\Ternary\GetDebugTypeRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
ClassOnObjectRector::class,
GetDebugTypeRector::class,
]);
$rectorConfig->rules([ClassOnObjectRector::class, GetDebugTypeRector::class]);
};
28 changes: 28 additions & 0 deletions tests/Issues/EmptyBooleanCompare/EmptyBooleanCompareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\EmptyBooleanCompare;

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

final class EmptyBooleanCompareTest 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';
}
}
39 changes: 39 additions & 0 deletions tests/Issues/EmptyBooleanCompare/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Rector\Core\Tests\Issues\EmptyBooleanCompare\Fixture;

final class SomeFixture
{
public function checkUrl(string $url)
{
$parts = parse_url($url);

if (!empty($parts['host'])) {
return $parts['host'];
}

return null;
}
}

?>
-----
<?php

namespace Rector\Core\Tests\Issues\EmptyBooleanCompare\Fixture;

final class SomeFixture
{
public function checkUrl(string $url)
{
$parts = parse_url($url);

if (isset($parts['host']) && $parts['host'] !== '') {
return $parts['host'];
}

return null;
}
}

?>
11 changes: 11 additions & 0 deletions tests/Issues/EmptyBooleanCompare/config/configured_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
use Rector\Strict\Rector\If_\BooleanInIfConditionRuleFixerRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([BooleanInIfConditionRuleFixerRector::class, DisallowedEmptyRuleFixerRector::class]);
};
5 changes: 1 addition & 4 deletions tests/Issues/InfiniteSwapParams/config/configured_rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,5 @@
use Rector\Php80\Rector\Catch_\RemoveUnusedVariableInCatchRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
RemoveUnusedVariableInCatchRector::class,
OptionalParametersAfterRequiredRector::class
]);
$rectorConfig->rules([RemoveUnusedVariableInCatchRector::class, OptionalParametersAfterRequiredRector::class]);
};