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
6 changes: 6 additions & 0 deletions src/ProcessAnalyzer/RectifiedAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Core\ProcessAnalyzer;

use PhpParser\Node;
use PhpParser\Node\Stmt;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;

Expand Down Expand Up @@ -56,6 +57,11 @@ private function isJustReprintedOverlappedTokenStart(Node $node, ?Node $original
return false;
}

if (! $node instanceof Stmt) {
$createdByRule = $node->getAttribute(AttributeKey::CREATED_BY_RULE) ?? [];
return $createdByRule === [];
}

/**
* Start token pos must be < 0 to continue, as the node and parent node just re-printed
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Fixture extends ParentClass
{
public function run($a, $b, $c)
{
if ($a !== 'a' && $b !== 'b') {
if (!($a === 'a' || $b === 'b')) {
return;
}
if (!$c) {
Expand Down
59 changes: 59 additions & 0 deletions tests/Issues/ReplaceStmtToExpr/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

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

class Fixture
{
public function getUser(?User $user = null): ?User
{
return $user ?? (Auth::check() ? Auth::user() : null);
}

public function doFoo(
?User $user = null
): bool {
$user = $this->getUser($user);

if (!$user) {
return false;
}

return $user->isFoo() || $user->isBar();
}

}

?>
-----
<?php

declare(strict_types=1);

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

class Fixture
{
public function getUser(?User $user = null): ?User
{
return $user ?? (Auth::check() ? Auth::user() : null);
}

public function doFoo(
?User $user = null
): bool {
$user = $this->getUser($user);

if ($user === null) {
return false;
}
if ($user->isFoo()) {
return true;
}
return (bool) $user->isBar();
}

}

?>
28 changes: 28 additions & 0 deletions tests/Issues/ReplaceStmtToExpr/ReplaceStmtToExprTest.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\ReplaceStmtToExpr;

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

final class ReplaceStmtToExprTest 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';
}
}
16 changes: 16 additions & 0 deletions tests/Issues/ReplaceStmtToExpr/config/configured_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector;
use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
use Rector\EarlyReturn\Rector\Return_\ReturnBinaryOrToEarlyReturnRector;

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