Skip to content

Commit

Permalink
[Printer] Apply configurable multiline on Fluent Method Call on print…
Browse files Browse the repository at this point in the history
… after method call created/re-printed (#5779)

* [Printer] Apply multiline on Fluent Method Call on print

* [ci-review] Rector Rectify

* update fixture on RectorConfigBuilderRector

* re-print args

* make new line on fluent call optional to avoid BC break

* ensure flag feature not replaced by other config

* [ci-review] Rector Rectify

* make optional when enabeld only

* Fix test

* [ci-review] Rector Rectify

* comment

* allow enable disable

* Fix phpstan

* clean up call

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Mar 28, 2024
1 parent 3122ef4 commit 08d73a1
Show file tree
Hide file tree
Showing 20 changed files with 94 additions and 31 deletions.
3 changes: 3 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@
foreach ($extensionConfigResolver->provide() as $extensionConfigFile) {
$rectorConfig->import($extensionConfigFile);
}

// use original php-parser printer to avoid BC break on fluent call
$rectorConfig->newLineOnFluentCall(false);
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ declare(strict_types=1);

use Rector\Config\RectorConfig;

return \Rector\Config\RectorConfig::configure()->withoutParallel();
return \Rector\Config\RectorConfig::configure()
->withoutParallel();

?>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ declare(strict_types=1);

use Rector\Config\RectorConfig;

return \Rector\Config\RectorConfig::configure()->withFileExtensions(['php', 'phtml']);
return \Rector\Config\RectorConfig::configure()
->withFileExtensions(['php', 'phtml']);

?>
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ declare(strict_types=1);
use Rector\Config\RectorConfig;
use Rector\Renaming\Rector\FuncCall\RenameFunctionRector;

return \Rector\Config\RectorConfig::configure()->withConfiguredRule(RenameFunctionRector::class, [
'is_real' => 'is_float',
]);
return \Rector\Config\RectorConfig::configure()
->withConfiguredRule(RenameFunctionRector::class, ['is_real' => 'is_float']);

?>
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use Rector\Transform\Rector\FileWithoutNamespace\RectorConfigBuilderRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;

return \Rector\Config\RectorConfig::configure()->withRules([ReturnUnionTypeRector::class, RectorConfigBuilderRector::class, TypedPropertyFromAssignsRector::class]);
return \Rector\Config\RectorConfig::configure()
->withRules([ReturnUnionTypeRector::class, RectorConfigBuilderRector::class, TypedPropertyFromAssignsRector::class]);

?>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ declare(strict_types=1);
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\SetList;

return \Rector\Config\RectorConfig::configure()->withSets([SetList::DEAD_CODE]);
return \Rector\Config\RectorConfig::configure()
->withSets([SetList::DEAD_CODE]);

?>
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ declare(strict_types=1);

use Rector\Config\RectorConfig;

return \Rector\Config\RectorConfig::configure()->withAutoloadPaths([
__DIR__ . '/../autoload.php',
]);
return \Rector\Config\RectorConfig::configure()
->withAutoloadPaths([__DIR__ . '/../autoload.php']);

?>
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ declare(strict_types=1);

use Rector\Config\RectorConfig;

return \Rector\Config\RectorConfig::configure()->withBootstrapFiles([
__DIR__ . '/constants.php',
]);
return \Rector\Config\RectorConfig::configure()
->withBootstrapFiles([__DIR__ . '/constants.php']);

?>
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ declare(strict_types=1);
use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector;

return \Rector\Config\RectorConfig::configure()->withPaths([
__DIR__ . '/src',
])->withRules([ReturnUnionTypeRector::class]);
return \Rector\Config\RectorConfig::configure()
->withPaths([__DIR__ . '/src'])
->withRules([ReturnUnionTypeRector::class]);

?>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ declare(strict_types=1);
use Rector\Config\RectorConfig;
use Rector\ValueObject\PhpVersion;

return \Rector\Config\RectorConfig::configure()->withPhpVersion(PhpVersion::PHP_74);
return \Rector\Config\RectorConfig::configure()
->withPhpVersion(PhpVersion::PHP_74);

?>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ declare(strict_types=1);
use Rector\Config\RectorConfig;
use Rector\Transform\Rector\FileWithoutNamespace\RectorConfigBuilderRector;

return \Rector\Config\RectorConfig::configure()->withRules([RectorConfigBuilderRector::class]);
return \Rector\Config\RectorConfig::configure()
->withRules([RectorConfigBuilderRector::class]);

?>
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use Rector\Config\RectorConfig;
use Rector\Transform\Rector\FileWithoutNamespace\RectorConfigBuilderRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;

return \Rector\Config\RectorConfig::configure()->withRules([RectorConfigBuilderRector::class, TypedPropertyFromAssignsRector::class]);
return \Rector\Config\RectorConfig::configure()
->withRules([RectorConfigBuilderRector::class, TypedPropertyFromAssignsRector::class]);

?>
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class SomeClass
];
}

return \Rector\Config\RectorConfig::configure()->withRules(SomeClass::RULES);
return \Rector\Config\RectorConfig::configure()
->withRules(SomeClass::RULES);

?>
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ declare(strict_types=1);
use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector;

return \Rector\Config\RectorConfig::configure()->withSkip([
__DIR__ . '/src/migrations',
])->withRules([ReturnUnionTypeRector::class]);
return \Rector\Config\RectorConfig::configure()
->withSkip([__DIR__ . '/src/migrations'])
->withRules([ReturnUnionTypeRector::class]);

?>
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
use Rector\Transform\Rector\FileWithoutNamespace\RectorConfigBuilderRector;

return RectorConfig::configure()
->withFluentCallNewLine()
->withRules([RectorConfigBuilderRector::class]);
5 changes: 5 additions & 0 deletions src/Config/RectorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,11 @@ public function symfonyContainerPhp(string $filePath): void
SimpleParameterProvider::setParameter(Option::SYMFONY_CONTAINER_PHP_PATH_PARAMETER, $filePath);
}

public function newLineOnFluentCall(bool $enabled = true): void
{
SimpleParameterProvider::setParameter(Option::NEW_LINE_ON_FLUENT_CALL, $enabled);
}

/**
* @param string[] $extensions
*/
Expand Down
6 changes: 6 additions & 0 deletions src/Configuration/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ final class Option
*/
public const SYMFONY_CONTAINER_PHP_PATH_PARAMETER = 'symfony_container_php_path';

/**
* @internal Use @see \Rector\Config\RectorConfig::newLineOnFluentCall()
* @var string
*/
public const NEW_LINE_ON_FLUENT_CALL = 'new_line_on_fluent_call';

/**
* @var string
*/
Expand Down
20 changes: 16 additions & 4 deletions src/Configuration/RectorConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,11 @@ final class RectorConfigBuilder
* To make sure type declarations set and level are not duplicated,
* as both contain same rules
*/
private bool $isTypeCoverageLevelUsed = false;
private ?bool $isTypeCoverageLevelUsed = null;

private bool $isDeadCodeLevelUsed = false;
private ?bool $isDeadCodeLevelUsed = null;

private ?bool $isFluentNewLine = null;

/**
* @var RegisteredService[]
Expand All @@ -135,14 +137,14 @@ public function __invoke(RectorConfig $rectorConfig): void
{
$uniqueSets = array_unique($this->sets);

if (in_array(SetList::TYPE_DECLARATION, $uniqueSets, true) && $this->isTypeCoverageLevelUsed) {
if (in_array(SetList::TYPE_DECLARATION, $uniqueSets, true) && $this->isTypeCoverageLevelUsed === true) {
throw new InvalidConfigurationException(sprintf(
'Your config already enables type declarations set.%sRemove "->withTypeCoverageLevel()" as it only duplicates it, or remove type declaration set.',
PHP_EOL
));
}

if (in_array(SetList::DEAD_CODE, $uniqueSets, true) && $this->isDeadCodeLevelUsed) {
if (in_array(SetList::DEAD_CODE, $uniqueSets, true) && $this->isDeadCodeLevelUsed === true) {
throw new InvalidConfigurationException(sprintf(
'Your config already enables dead code set.%sRemove "->withDeadCodeLevel()" as it only duplicates it, or remove dead code set.',
PHP_EOL
Expand Down Expand Up @@ -256,6 +258,10 @@ public function __invoke(RectorConfig $rectorConfig): void
if ($this->symfonyContainerPhpFile !== null) {
$rectorConfig->symfonyContainerPhp($this->symfonyContainerPhpFile);
}

if ($this->isFluentNewLine !== null) {
$rectorConfig->newLineOnFluentCall($this->isFluentNewLine);
}
}

/**
Expand Down Expand Up @@ -688,6 +694,12 @@ public function withTypeCoverageLevel(int $level): self
return $this;
}

public function withFluentCallNewLine(bool $isFluentNewLine = true): self
{
$this->isFluentNewLine = $isFluentNewLine;
return $this;
}

public function registerService(string $className, ?string $alias = null, ?string $tag = null): self
{
$this->registerServices[] = new RegisteredService($className, $alias, $tag);
Expand Down
38 changes: 34 additions & 4 deletions src/PhpParser/Printer/BetterStandardPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
use Nette\Utils\Strings;
use PhpParser\Comment;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Yield_;
use PhpParser\Node\Param;
Expand Down Expand Up @@ -166,10 +169,7 @@ protected function pExpr_ArrowFunction(ArrowFunction $arrowFunction): string
return parent::pExpr_ArrowFunction($arrowFunction);
}

$indentSize = SimpleParameterProvider::provideIntParameter(Option::INDENT_SIZE);

$indent = str_repeat($this->getIndentCharacter(), $this->indentLevel) .
str_repeat($this->getIndentCharacter(), $indentSize);
$indent = $this->resolveIndentSpaces();

$text = "\n" . $indent;
foreach ($comments as $key => $comment) {
Expand Down Expand Up @@ -459,6 +459,36 @@ protected function pScalar_LNumber(LNumber $lNumber): string|int
return parent::pScalar_LNumber($lNumber);
}

private function resolveIndentSpaces(): string
{
$indentSize = SimpleParameterProvider::provideIntParameter(Option::INDENT_SIZE);

return str_repeat($this->getIndentCharacter(), $this->indentLevel) .
str_repeat($this->getIndentCharacter(), $indentSize);
}

protected function pExpr_MethodCall(MethodCall $methodCall): string
{
if (SimpleParameterProvider::provideBoolParameter(Option::NEW_LINE_ON_FLUENT_CALL) === false) {
return parent::pExpr_MethodCall($methodCall);
}

if ($methodCall->var instanceof CallLike) {
foreach ($methodCall->args as $arg) {
if (! $arg instanceof Arg) {
continue;
}

$arg->value->setAttribute(AttributeKey::ORIGINAL_NODE, null);
}

return $this->pDereferenceLhs($methodCall->var) . "\n" . $this->resolveIndentSpaces() . "->" . $this->pObjectProperty($methodCall->name)
. '(' . $this->pMaybeMultiline($methodCall->args) . ')';
}

return parent::pExpr_MethodCall($methodCall);
}

/**
* Keep attributes on newlines
*/
Expand Down
1 change: 1 addition & 0 deletions src/Testing/PHPUnit/AbstractRectorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public static function tearDownAfterClass(): void
SimpleParameterProvider::setParameter(Option::INDENT_CHAR, ' ');
SimpleParameterProvider::setParameter(Option::INDENT_SIZE, 4);
SimpleParameterProvider::setParameter(Option::POLYFILL_PACKAGES, []);
SimpleParameterProvider::setParameter(Option::NEW_LINE_ON_FLUENT_CALL, false);
}

protected function setUp(): void
Expand Down

0 comments on commit 08d73a1

Please sign in to comment.