Force newlines for method arguments #7024
-
I'm working on a Rector that adds new methods to a class. Some methods have a lot of arguments but are printed on a single line like this: public function getFeed($locator, $value, $args, $context, int $first, string $after) : object I want them all on a new line. If only I could insert 1 newline before the first argument, PHP CS Fixer could fix the rest. public function getFeed(
$locator, $value, $args, $context, int $first, string $after) : object Any idea how to get this done? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
If only I could modify the
but it's not possible as the class is |
Beta Was this translation helpful? Give feedback.
-
@ruudk you can create custom rector rule like this: <?php
declare(strict_types=1);
namespace Utils\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
class NewLineFirstParamRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('new line first param', []);
}
public function getNodeTypes(): array
{
return [ClassMethod::class];
}
public function refactor(Node $node): ?Node
{
$params = $node->params;
// no params, keep as is
if ($params === []) {
return null;
}
// not too long? keep as is
$printParams = $this->betterStandardPrinter->print($params);
if (strlen($printParams) <= 50) {
return null;
}
$firstParam = $params[0];
$oldTokens = $this->file->getOldTokens();
$startPosition = $firstParam->getStartTokenPos();
if (! isset($oldTokens[$startPosition][1])) {
return null;
}
// already new lined? keep as is
if (isset($oldTokens[$startPosition - 1][1]) && $oldTokens[$startPosition - 1][1] == PHP_EOL) {
return null;
}
$start = $oldTokens[$startPosition][1];
if (is_string($start)) {
$oldTokens[$startPosition][1] = PHP_EOL. $start;
(function ($file) {
$file->oldStmts = [];
})->bindTo($this->file, $this->file)($this->file);
$this->file->hydrateStmtsAndTokens(
$this->file->getNewStmts(),
$this->file->getOldStmts(),
$oldTokens
);
return $node;
}
return null;
}
} Then, register to rector.php <?php
declare(strict_types=1);
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Utils\Rector\ClassMethod\NewLineFirstParamRector;
return static function (ContainerConfigurator $containerConfigurator): void {
// other config here ...
$services = $containerConfigurator->services();
$services->set(NewLineFirstParamRector::class);
}; and run it: ➜ proj vendor/bin/rector process test4.php --clear-cache
1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
1 file with changes
===================
1) test4.php:1
---------- begin diff ----------
@@ @@
class Test4
{
- public function getFeed($locator, $value, $args, $context, int $first, string $after): object
+ public function getFeed(
+$locator, $value, $args, $context, int $first, string $after): object
{
return new stdClass;
}
----------- end diff -----------
Applied rules:
* NewLineFirstParamRector
[OK] 1 file has been changed by Rector |
Beta Was this translation helpful? Give feedback.
@ruudk you can create custom rector rule like this: