mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-22 10:43:35 +01:00
make SimplifyIfElseToTernaryRector limit to 120 chars to prevent complicated code
This commit is contained in:
parent
3fafc49ea0
commit
8574f286b6
@ -2,6 +2,7 @@
|
||||
|
||||
namespace Rector\CodeQuality\Rector\If_;
|
||||
|
||||
use Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
@ -15,6 +16,11 @@ use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
final class SimplifyIfElseToTernaryRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private const LINE_LENGHT_LIMIT = 120;
|
||||
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Changes if/else for same value as assign to ternary', [
|
||||
@ -84,9 +90,15 @@ CODE_SAMPLE
|
||||
return null;
|
||||
}
|
||||
|
||||
$ternaryNode = new Ternary($node->cond, $ternaryIf, $ternaryElse);
|
||||
$ternary = new Ternary($node->cond, $ternaryIf, $ternaryElse);
|
||||
$assign = new Assign($ifAssignVar, $ternary);
|
||||
|
||||
return new Assign($ifAssignVar, $ternaryNode);
|
||||
// do not create super long lines
|
||||
if (Strings::length($this->print($assign)) > self::LINE_LENGHT_LIMIT) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $assign;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\If_\SimplifyIfElseToTernaryRector\Fixture;
|
||||
|
||||
class TooLong
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
if ($this->nodeTypeResolver->isStringyType($staticCall->args[1]->value)) {
|
||||
$name = $this->nameResolver->isName(
|
||||
$staticCall,
|
||||
'contains'
|
||||
) ? 'assertStringContainsString' : 'assertStringNotContainsString';
|
||||
} else {
|
||||
$name = $this->nameResolver->isName($staticCall, 'contains') ? 'assertContains' : 'assertNotContains';
|
||||
}
|
||||
}
|
||||
}
|
@ -9,7 +9,11 @@ final class SimplifyIfElseToTernaryRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc', __DIR__ . '/Fixture/keep.php.inc']);
|
||||
$this->doTestFiles([
|
||||
__DIR__ . '/Fixture/fixture.php.inc',
|
||||
__DIR__ . '/Fixture/keep.php.inc',
|
||||
__DIR__ . '/Fixture/too_long.php.inc',
|
||||
]);
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
|
Loading…
x
Reference in New Issue
Block a user