Skip to content

Commit

Permalink
ConstructorPropertyPromotionSpacingSniff
Browse files Browse the repository at this point in the history
  • Loading branch information
vossik committed Jun 2, 2021
1 parent 6958894 commit 0a25c52
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
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

0 comments on commit 0a25c52

Please sign in to comment.