Skip to content

Commit

Permalink
Fix JoinStringConcatRector for too long lines
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Oct 30, 2019
1 parent e57526e commit 6f66783
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
11 changes: 11 additions & 0 deletions packages/CodeQuality/src/Exception/TooLongException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Rector\CodeQuality\Exception;

use Exception;

final class TooLongException extends Exception
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Rector\CodeQuality\Rector\Concat;

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Scalar\String_;
use Rector\CodeQuality\Exception\TooLongException;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand All @@ -16,6 +18,11 @@
*/
final class JoinStringConcatRector extends AbstractRector
{
/**
* @var int
*/
private const LINE_BREAK_POINT = 80;

public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Joins concat of 2 strings', [
Expand Down Expand Up @@ -56,7 +63,11 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
return $this->joinConcatIfStrings($node);
try {
return $this->joinConcatIfStrings($node);
} catch (TooLongException $tooLongException) {
return null;
}
}

/**
Expand All @@ -80,6 +91,11 @@ private function joinConcatIfStrings(Concat $concat): Node
return $concat;
}

$value = $concat->left->value . $concat->right->value;
if (Strings::length($value) >= self::LINE_BREAK_POINT) {
throw new TooLongException();
}

return new String_($concat->left->value . $concat->right->value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\CodeQuality\Tests\Rector\Concat\JoinStringConcatRector\Fixture;

class SkipLongerThan120
{
public function run()
{
$name = 'Adds $uuid property to entities, that already have $id with integer type.' .
'Require for step-by-step migration from int to uuid. ' .
'In following step it should be renamed to $id and replace it';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function test(string $file): void
public function provideDataForTest(): Iterator
{
yield [__DIR__ . '/Fixture/fixture.php.inc'];
yield [__DIR__ . '/Fixture/skip_longer_than_120.php.inc'];
}

protected function getRectorClass(): string
Expand Down

0 comments on commit 6f66783

Please sign in to comment.