Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
use Symplify\CodingStandard\TokenAnalyzer\ParamNewliner;

/**
* Every parameter of a constructor without promoted properties must be on a standalone line.
* Every parameter of a constructor without promoted properties must be on a standalone line, unless the constructor
* has 3 or less parameters.
*
* Constructors with promoted properties are handled by @see StandaloneLinePromotedPropertyFixer, so both rules can
* be used side by side without processing the same constructor twice.
Expand All @@ -28,6 +29,11 @@ final class StandaloneLinePlainConstructorParamFixer extends AbstractSymplifyFix
{
private const string ERROR_MESSAGE = 'Constructor param should be on a standalone line to ease git diffs on new dependency';

/**
* Short parameter lists are readable on a single line.
*/
private const int MIN_PARAM_COUNT = 4;

/**
* @var int[]
*/
Expand Down Expand Up @@ -99,7 +105,7 @@ public function fix(SplFileInfo $fileInfo, Tokens $tokens): void
continue;
}

if (! $this->hasParams($tokens, $paramBracketPosition)) {
if ($this->countParams($tokens, $paramBracketPosition) < self::MIN_PARAM_COUNT) {
continue;
}

Expand All @@ -125,21 +131,35 @@ private function resolveOpenBracketPosition(Tokens $tokens, int $position): ?int
}

/**
* An empty parameter list would be broken into an empty line between the brackets.
*
* @param Tokens<Token> $tokens
*/
private function hasParams(Tokens $tokens, int $openBracketPosition): bool
private function countParams(Tokens $tokens, int $openBracketPosition): int
{
$firstParamPosition = $tokens->getNextMeaningfulToken($openBracketPosition);
if ($firstParamPosition === null) {
return false;
}
$closeBracketPosition = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openBracketPosition);

$paramCount = 0;
$nestingLevel = 0;

/** @var Token $firstParamToken */
$firstParamToken = $tokens[$firstParamPosition];
for ($index = $openBracketPosition + 1; $index < $closeBracketPosition; ++$index) {
/** @var Token $token */
$token = $tokens[$index];

if ($token->equalsAny(['(', '[', '{'])) {
++$nestingLevel;
continue;
}

if ($token->equalsAny([')', ']', '}'])) {
--$nestingLevel;
continue;
}

if ($nestingLevel === 0 && $token->isGivenKind(T_VARIABLE)) {
++$paramCount;
}
}

return $firstParamToken->getContent() !== ')';
return $paramCount;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Symplify\CodingStandard\Tests\Fixer\Spacing\StandaloneLinePlainConstru

final class PlainParams
{
public function __construct(CorePermissions $security, Translator $translator, RouterInterface $router)
public function __construct(CorePermissions $security, Translator $translator, RouterInterface $router, Logger $logger)
{
}
}
Expand All @@ -20,7 +20,8 @@ final class PlainParams
public function __construct(
CorePermissions $security,
Translator $translator,
RouterInterface $router
RouterInterface $router,
Logger $logger
)
{
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Symplify\CodingStandard\Tests\Fixer\Spacing\StandaloneLinePlainConstructorParamFixer\Fixture;

final class SkipThreeParams
{
public function __construct(CorePermissions $security, Translator $translator, RouterInterface $router)
{
}
}
Loading