Skip to content

Commit

Permalink
Squiz/FunctionDeclarationArgumentSpacing: handle modifiers for constr…
Browse files Browse the repository at this point in the history
…uctor property promotion

The spacing after visibility/`readonly` modifiers for constructor property promotion were so far not checked by this sniff.

While the `Squiz.WhiteSpace.ScopeKeywordSpacing` sniff will already handle this, that sniff may not be in use in all standards using this sniff. As things were, this sniff was just no longer feature complete for the task this sniff is supposed to handle: spacing of function declaration arguments.

This commit adds handling the spacing after modifiers used for constructor property promotion to this sniff.

The spacing requirements are aligned with the spacing expectations of the `Squiz.WhiteSpace.ScopeKeywordSpacing` sniff, so the sniffs should not conflict with each other.

Additionally, the new checks in this sniff have dedicated error codes, which means that - if there would be a conflict anywhere - the modifier spacing checks within this sniff can easily be turned off.

Includes tests.
  • Loading branch information
jrfnl committed Jan 23, 2025
1 parent 68c96b5 commit 62c1100
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,71 @@ public function processBracket($phpcsFile, $openBracket)
}//end if
}//end if

if (isset($param['visibility_token']) === true && $param['visibility_token'] !== false) {
$visibilityToken = $param['visibility_token'];
$afterVisibilityToken = $phpcsFile->findNext(T_WHITESPACE, ($visibilityToken + 1), $param['token'], true);

$spacesAfter = 0;
if ($afterVisibilityToken !== false
&& $tokens[$visibilityToken]['line'] !== $tokens[$afterVisibilityToken]['line']
) {
$spacesAfter = 'newline';
} else if ($tokens[($visibilityToken + 1)]['code'] === T_WHITESPACE) {
$spacesAfter = $tokens[($visibilityToken + 1)]['length'];
}

if ($spacesAfter !== 1) {
$error = 'Expected 1 space after visibility modifier "%s"; %s found';
$data = [
$tokens[$visibilityToken]['content'],
$spacesAfter,
];

$fix = $phpcsFile->addFixableError($error, $visibilityToken, 'SpacingAfterVisbility', $data);
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->addContent($visibilityToken, ' ');

for ($i = ($visibilityToken + 1); $tokens[$i]['code'] === T_WHITESPACE; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}

$phpcsFile->fixer->endChangeset();
}
}//end if
}//end if

if (isset($param['readonly_token']) === true) {
$readonlyToken = $param['readonly_token'];
$afterReadonlyToken = $phpcsFile->findNext(T_WHITESPACE, ($readonlyToken + 1), $param['token'], true);

$spacesAfter = 0;
if ($afterReadonlyToken !== false
&& $tokens[$readonlyToken]['line'] !== $tokens[$afterReadonlyToken]['line']
) {
$spacesAfter = 'newline';
} else if ($tokens[($readonlyToken + 1)]['code'] === T_WHITESPACE) {
$spacesAfter = $tokens[($readonlyToken + 1)]['length'];
}

if ($spacesAfter !== 1) {
$error = 'Expected 1 space after readonly modifier; %s found';
$data = [$spacesAfter];

$fix = $phpcsFile->addFixableError($error, $readonlyToken, 'SpacingAfterReadonly', $data);
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->addContent($readonlyToken, ' ');

for ($i = ($readonlyToken + 1); $tokens[$i]['code'] === T_WHITESPACE; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}

$phpcsFile->fixer->endChangeset();
}
}//end if
}//end if

$commaToken = false;
if ($paramNumber > 0 && $params[($paramNumber - 1)]['comma_token'] !== false) {
$commaToken = $params[($paramNumber - 1)]['comma_token'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,14 @@ function newlineBeforeCommaFixerRespectsComments(
class PropertyPromotionSpacingAfterComma {
public function __construct(private string|int $propA, protected bool $correctSpace, public MyClass $tooMuchSpace,readonly string $noSpace) {}
}

class PropertyPromotionSpacingAfterModifier {
public function __construct(
private$noSpace,
public MyClass $tooMuchSpace,
protected readonly string $tooMuchSpaceX2,
readonly
public
string $tooMuchSpaceNewLines,
) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,12 @@ function newlineBeforeCommaFixerRespectsComments(
class PropertyPromotionSpacingAfterComma {
public function __construct(private string|int $propA, protected bool $correctSpace, public MyClass $tooMuchSpace, readonly string $noSpace) {}
}

class PropertyPromotionSpacingAfterModifier {
public function __construct(
private $noSpace,
public MyClass $tooMuchSpace,
protected readonly string $tooMuchSpaceX2,
readonly public string $tooMuchSpaceNewLines,
) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ public function getErrorList($testFile='')
195 => 1,
196 => 1,
200 => 2,
205 => 1,
206 => 1,
207 => 2,
208 => 1,
209 => 1,
];

default:
Expand Down

0 comments on commit 62c1100

Please sign in to comment.