Skip to content

ConstructorPropertyPromotionSpacingSniff #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2021
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
@@ -0,0 +1,108 @@
<?php

declare(strict_types = 1);

namespace InfinityloopCodingStandard\Sniffs\Classes;

class ConstructorPropertyPromotionSpacingSniff implements \PHP_CodeSniffer\Sniffs\Sniff
{
public const CONSTRUCTOR_PARAMETER_SAME_LINE = 'ConstructorParametersOnSameLine';

public function register() : array
{
return [\T_FUNCTION];
}

public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $functionPointer) : void
{
if (!\SlevomatCodingStandard\Helpers\SniffSettingsHelper::isEnabledByPhpVersion(null, 80000)) {
return;
}

$tokens = $phpcsFile->getTokens();

$namePointer = \SlevomatCodingStandard\Helpers\TokenHelper::findNextEffective($phpcsFile, $functionPointer + 1);

if (\strtolower($tokens[$namePointer]['content']) !== '__construct') {
return;
}

if (\SlevomatCodingStandard\Helpers\FunctionHelper::isAbstract($phpcsFile, $functionPointer)) {
return;
}

$parameterPointers = $this->getParameterPointers($phpcsFile, $functionPointer);

if (\count($parameterPointers) === 0) {
return;
}

$containsPropertyPromotion = false;

foreach ($parameterPointers as $parameterPointer) {
$pointerBefore = \SlevomatCodingStandard\Helpers\TokenHelper::findPrevious(
$phpcsFile,
[\T_COMMA, \T_OPEN_PARENTHESIS],
$parameterPointer - 1,
);

$visibilityPointer = \SlevomatCodingStandard\Helpers\TokenHelper::findNextEffective($phpcsFile, $pointerBefore + 1);

if (\in_array($tokens[$visibilityPointer]['code'], \PHP_CodeSniffer\Util\Tokens::$scopeModifiers, true)) {
$containsPropertyPromotion = true;
}
}

if (!$containsPropertyPromotion) {
return;
}

$previousPointer = null;

foreach ($parameterPointers as $parameterPointer) {
if ($previousPointer === null) {
$previousPointer = $parameterPointer;

continue;
}

if ($tokens[$previousPointer]['line'] !== $tokens[$parameterPointer]['line']) {
continue;
}

$fix = $phpcsFile->addFixableError(
'Constructor parameter should be reformatted to next line.',
$parameterPointer,
self::CONSTRUCTOR_PARAMETER_SAME_LINE,
);

if (!$fix) {
continue;
}

$phpcsFile->fixer->beginChangeset();

$pointerBefore = \SlevomatCodingStandard\Helpers\TokenHelper::findPrevious(
$phpcsFile,
[\T_COMMA, \T_OPEN_PARENTHESIS],
$parameterPointer - 1,
);

$phpcsFile->fixer->addContent($pointerBefore, $phpcsFile->eolChar);

$phpcsFile->fixer->endChangeset();
}
}

private function getParameterPointers(\PHP_CodeSniffer\Files\File $phpcsFile, int $functionPointer) : array
{
$tokens = $phpcsFile->getTokens();

return \SlevomatCodingStandard\Helpers\TokenHelper::findNextAll(
$phpcsFile,
\T_VARIABLE,
$tokens[$functionPointer]['parenthesis_opener'] + 1,
$tokens[$functionPointer]['parenthesis_closer'],
);
}
}
1 change: 1 addition & 0 deletions InfinityloopCodingStandard/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -502,4 +502,5 @@
<rule ref="InfinityloopCodingStandard.ControlStructures.RequireMultiLineNullCoalesce"/>
<rule ref="InfinityloopCodingStandard.ControlStructures.SwitchCommentSpacing"/>
<rule ref="InfinityloopCodingStandard.TypeHints.UnionTypeHintFormat"/>
<rule ref="InfinityloopCodingStandard.Classes.ConstructorPropertyPromotionSpacing"/>
</ruleset>
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ Checks that there is a certain number of blank lines between code and comment

Improved version of Slevomat UnionTypeHintFormat with added formatting of multiline unions

#### InfinityloopCodingStandard.Classes.ConstructorPropertyPromotionSpacing :wrench:

Space constructor arguments one per line when Constructor Property Promotion is used

### Slevomat sniffs

Detailed list of Slevomat sniffs with configured settings. Some sniffs are not included, either because we dont find them helpful, the are too strict, or collide with their counter-sniff (require/disallow pairs).
Expand Down