Skip to content

Commit

Permalink
[CodeQuality][CodingStyle] Handle crash on SimplifyIfReturnBoolRector…
Browse files Browse the repository at this point in the history
…+NewlineAfterStatementRector+StringClassNameToClassConstantRector (#3175)

* [CodeQuality][CodingStyle] Handle crash on SimplifyIfReturnBoolRector+NewlineAfterStatementRector+StringClassNameToClassConstantRector

* Fixed 🎉

* Final touch: eol
  • Loading branch information
samsonasik committed Dec 9, 2022
1 parent 863fee3 commit 4ba03ab
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
4 changes: 2 additions & 2 deletions rules/CodingStyle/Rector/Stmt/NewlineAfterStatementRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ public function refactor(Node $node): ?array
}
}

// skip same line that cause infinite loop
if ($rangeLine === 0) {
// skip same line or < 0 that cause infinite loop or crash
if ($rangeLine <= 0) {
return null;
}

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

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\IndentationCrash;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class CodeQualityCodingStyleIndentationCrashTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

/**
* @return Iterator<array<string>>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureCodeQualityCodingStyle');
}

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

namespace App;

class Foo {}

class NamespaceEqualPrefixClassString
{
public function hasMethod($classReflection, string $methodName): bool
{
if ($classReflection->getName() !== 'App\Foo') {
return false;
}

if (! str_starts_with($methodName, 'with')) {
return false;
}

return true;
}
}

?>
-----
<?php

namespace App;

class Foo {}

class NamespaceEqualPrefixClassString
{
public function hasMethod($classReflection, string $methodName): bool
{
if ($classReflection->getName() !== \App\Foo::class) {
return false;
}
return str_starts_with($methodName, 'with');
}
}

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

declare(strict_types=1);

use Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector;
use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
use Rector\Config\RectorConfig;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;

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

0 comments on commit 4ba03ab

Please sign in to comment.