Skip to content

Commit

Permalink
fix: Port squizlabs/PHP_CodeSniffer#3881 to this repo
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyvdSluijs committed Dec 4, 2023
1 parent df71300 commit 0b3c9af
Show file tree
Hide file tree
Showing 14 changed files with 179 additions and 36 deletions.
24 changes: 21 additions & 3 deletions src/Standards/Generic/Sniffs/Arrays/ArrayIndentSniff.php
Expand Up @@ -76,9 +76,15 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
// check indent levels because it's not valid. But we don't enforce exactly
// how far indented it should be.
if ($startIndent < $baseIndent) {
$error = 'Array open brace not indented correctly; expected at least %s spaces but found %s';
$pluralizeSpace = 's';
if ($baseIndent === 1) {
$pluralizeSpace = '';
}

$error = 'Array open brace not indented correctly; expected at least %s space%s but found %s';
$data = [
$baseIndent,
$pluralizeSpace,
$startIndent,
];
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'OpenBraceIncorrect', $data);
Expand Down Expand Up @@ -117,9 +123,15 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
continue;
}

$error = 'Array key not indented correctly; expected %s spaces but found %s';
$pluralizeSpace = 's';
if ($expectedIndent === 1) {
$pluralizeSpace = '';
}

$error = 'Array key not indented correctly; expected %s space%s but found %s';
$data = [
$expectedIndent,
$pluralizeSpace,
$foundIndent,
];
$fix = $phpcsFile->addFixableError($error, $first, 'KeyIncorrect', $data);
Expand Down Expand Up @@ -154,9 +166,15 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
return;
}

$error = 'Array close brace not indented correctly; expected %s spaces but found %s';
$pluralizeSpace = 's';
if ($expectedIndent === 1) {
$pluralizeSpace = '';
}

$error = 'Array close brace not indented correctly; expected %s space%s but found %s';
$data = [
$expectedIndent,
$pluralizeSpace,
$foundIndent,
];
$fix = $phpcsFile->addFixableError($error, $arrayEnd, 'CloseBraceIncorrect', $data);
Expand Down
18 changes: 13 additions & 5 deletions src/Standards/Generic/Sniffs/Formatting/SpaceAfterCastSniff.php
Expand Up @@ -54,8 +54,12 @@ public function register()
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$this->spacing = (int) $this->spacing;
$tokens = $phpcsFile->getTokens();
$this->spacing = (int) $this->spacing;
$pluralizeSpace = 's';
if ($this->spacing === 1) {
$pluralizeSpace = '';
}

if ($tokens[$stackPtr]['code'] === T_BINARY_CAST
&& $tokens[$stackPtr]['content'] === 'b'
Expand Down Expand Up @@ -83,8 +87,11 @@ public function process(File $phpcsFile, $stackPtr)

$nextNonWhitespace = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
if ($nextNonEmpty !== $nextNonWhitespace) {
$error = 'Expected %s space(s) after cast statement; comment found';
$data = [$this->spacing];
$error = 'Expected %s space%s after cast statement; comment found';
$data = [
$this->spacing,
$pluralizeSpace,
];
$phpcsFile->addError($error, $stackPtr, 'CommentFound', $data);

if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) {
Expand All @@ -109,9 +116,10 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

$error = 'Expected %s space(s) after cast statement; %s found';
$error = 'Expected %s space%s after cast statement; %s found';
$data = [
$this->spacing,
$pluralizeSpace,
$found,
];

Expand Down
18 changes: 13 additions & 5 deletions src/Standards/Generic/Sniffs/Formatting/SpaceAfterNotSniff.php
Expand Up @@ -64,8 +64,12 @@ public function register()
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$this->spacing = (int) $this->spacing;
$tokens = $phpcsFile->getTokens();
$this->spacing = (int) $this->spacing;
$pluralizeSpace = 's';
if ($this->spacing === 1) {
$pluralizeSpace = '';
}

$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
if ($nextNonEmpty === false) {
Expand All @@ -84,8 +88,11 @@ public function process(File $phpcsFile, $stackPtr)

$nextNonWhitespace = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
if ($nextNonEmpty !== $nextNonWhitespace) {
$error = 'Expected %s space(s) after NOT operator; comment found';
$data = [$this->spacing];
$error = 'Expected %s space%s after NOT operator; comment found';
$data = [
$this->spacing,
$pluralizeSpace,
];
$phpcsFile->addError($error, $stackPtr, 'CommentFound', $data);
return;
}
Expand All @@ -101,9 +108,10 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

$error = 'Expected %s space(s) after NOT operator; %s found';
$error = 'Expected %s space%s after NOT operator; %s found';
$data = [
$this->spacing,
$pluralizeSpace,
$found,
];

Expand Down
16 changes: 16 additions & 0 deletions src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.inc
Expand Up @@ -111,3 +111,19 @@ $array = [
name: $value
),
];

// phpcs:set Generic.Arrays.ArrayIndent indent 1

// Testing pluralization of indent text - array item indent.
$var = [
1 => 'one',
2 => 'two',
/* three */ 3 => 'three',
];

// Testing pluralization of indent text - close brace indent.
$var = [
1 => 'one',
];

// phpcs:set Generic.Arrays.ArrayIndent indent 4
16 changes: 16 additions & 0 deletions src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.inc.fixed
Expand Up @@ -112,3 +112,19 @@ $array = [
name: $value
),
];

// phpcs:set Generic.Arrays.ArrayIndent indent 1

// Testing pluralization of indent text - array item indent.
$var = [
1 => 'one',
2 => 'two',
/* three */ 3 => 'three',
];

// Testing pluralization of indent text - close brace indent.
$var = [
1 => 'one',
];

// phpcs:set Generic.Arrays.ArrayIndent indent 4
3 changes: 3 additions & 0 deletions src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.php
Expand Up @@ -45,6 +45,9 @@ public function getErrorList()
88 => 1,
98 => 1,
110 => 1,
120 => 1,
121 => 1,
127 => 1,
];

}//end getErrorList()
Expand Down
66 changes: 48 additions & 18 deletions src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php
Expand Up @@ -330,11 +330,17 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
}
} else if ($tokens[$arrayEnd]['column'] !== $keywordStart) {
// Check the closing bracket is lined up under the "a" in array.
$expected = ($keywordStart - 1);
$found = ($tokens[$arrayEnd]['column'] - 1);
$error = 'Closing parenthesis not aligned correctly; expected %s space(s) but found %s';
$data = [
$expected = ($keywordStart - 1);
$found = ($tokens[$arrayEnd]['column'] - 1);
$pluralizeSpace = 's';
if ($expected === 1) {
$pluralizeSpace = '';
}

$error = 'Closing parenthesis not aligned correctly; expected %s space%s but found %s';
$data = [
$expected,
$pluralizeSpace,
$found,
];

Expand Down Expand Up @@ -674,12 +680,18 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
} else if ($previousIsWhitespace === true) {
$expected = $keywordStart;

$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $valuePointer, true);
$found = ($tokens[$first]['column'] - 1);
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $valuePointer, true);
$found = ($tokens[$first]['column'] - 1);
$pluralizeSpace = 's';
if ($expected === 1) {
$pluralizeSpace = '';
}

if ($found !== $expected) {
$error = 'Array value not aligned correctly; expected %s spaces but found %s';
$error = 'Array value not aligned correctly; expected %s space%s but found %s';
$data = [
$expected,
$pluralizeSpace,
$found,
];

Expand Down Expand Up @@ -763,11 +775,17 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
}

if ($tokens[$indexPointer]['column'] !== $indicesStart && ($indexPointer - 1) !== $arrayStart) {
$expected = ($indicesStart - 1);
$found = ($tokens[$indexPointer]['column'] - 1);
$error = 'Array key not aligned correctly; expected %s spaces but found %s';
$data = [
$expected = ($indicesStart - 1);
$found = ($tokens[$indexPointer]['column'] - 1);
$pluralizeSpace = 's';
if ($expected === 1) {
$pluralizeSpace = '';
}

$error = 'Array key not aligned correctly; expected %s space%s but found %s';
$data = [
$expected,
$pluralizeSpace,
$found,
];

Expand All @@ -779,15 +797,21 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
$phpcsFile->fixer->replaceToken(($indexPointer - 1), str_repeat(' ', $expected));
}
}
}
}//end if

$arrowStart = ($tokens[$indexPointer]['column'] + $maxLength + 1);
if ($tokens[$index['arrow']]['column'] !== $arrowStart) {
$expected = ($arrowStart - ($index['index_length'] + $tokens[$indexPointer]['column']));
$found = ($tokens[$index['arrow']]['column'] - ($index['index_length'] + $tokens[$indexPointer]['column']));
$error = 'Array double arrow not aligned correctly; expected %s space(s) but found %s';
$data = [
$expected = ($arrowStart - ($index['index_length'] + $tokens[$indexPointer]['column']));
$found = ($tokens[$index['arrow']]['column'] - ($index['index_length'] + $tokens[$indexPointer]['column']));
$pluralizeSpace = 's';
if ($expected === 1) {
$pluralizeSpace = '';
}

$error = 'Array double arrow not aligned correctly; expected %s space%s but found %s';
$data = [
$expected,
$pluralizeSpace,
$found,
];

Expand All @@ -801,7 +825,7 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
}

continue;
}
}//end if

$valueStart = ($arrowStart + 3);
if ($tokens[$valuePointer]['column'] !== $valueStart) {
Expand All @@ -811,9 +835,15 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
$found = 'newline';
}

$error = 'Array value not aligned correctly; expected %s space(s) but found %s';
$pluralizeSpace = 's';
if ($expected === 1) {
$pluralizeSpace = '';
}

$error = 'Array value not aligned correctly; expected %s space%s but found %s';
$data = [
$expected,
$pluralizeSpace,
$found,
];

Expand Down
Expand Up @@ -112,9 +112,15 @@ public function process(File $phpcsFile, $stackPtr)
}

if ($tokens[$i]['column'] !== $requiredColumn) {
$error = 'Expected %s space(s) before asterisk; %s found';
$pluralizeSpace = 's';
if (($requiredColumn - 1) === 1) {
$pluralizeSpace = '';
}

$error = 'Expected %s space%s before asterisk; %s found';
$data = [
($requiredColumn - 1),
$pluralizeSpace,
($tokens[$i]['column'] - 1),
];
$fix = $phpcsFile->addFixableError($error, $i, 'SpaceBeforeStar', $data);
Expand All @@ -126,7 +132,7 @@ public function process(File $phpcsFile, $stackPtr)
$phpcsFile->fixer->replaceToken(($i - 1), $padding);
}
}
}
}//end if

if ($tokens[$i]['code'] !== T_DOC_COMMENT_STAR) {
continue;
Expand Down
Expand Up @@ -105,9 +105,15 @@ public function process(File $phpcsFile, $stackPtr)
}

if ($found !== $expected) {
$error = 'Expected %s space(s) after %s keyword; %s found';
$pluralizeSpace = 's';
if ($expected === 1) {
$pluralizeSpace = '';
}

$error = 'Expected %s space%s after %s keyword; %s found';
$data = [
$expected,
$pluralizeSpace,
strtoupper($tokens[$stackPtr]['content']),
$found,
];
Expand All @@ -120,7 +126,7 @@ public function process(File $phpcsFile, $stackPtr)
$phpcsFile->fixer->replaceToken(($stackPtr + 1), str_repeat(' ', $expected));
}
}
}
}//end if

// Single space after closing parenthesis.
if (isset($tokens[$stackPtr]['parenthesis_closer']) === true
Expand All @@ -146,9 +152,15 @@ public function process(File $phpcsFile, $stackPtr)
}

if ($found !== $expected) {
$error = 'Expected %s space(s) after closing parenthesis; found %s';
$pluralizeSpace = 's';
if ($expected === 1) {
$pluralizeSpace = '';
}

$error = 'Expected %s space%s after closing parenthesis; found %s';
$data = [
$expected,
$pluralizeSpace,
$found,
];

Expand Down
Expand Up @@ -529,3 +529,9 @@ $x = array(
'bar',
'baz' => 'bar', // KeySpecified (based on first entry).
);

$x =
array(
'a',
'b',
);
Expand Up @@ -565,3 +565,9 @@ $x = array(
'bar',
'baz' => 'bar', // KeySpecified (based on first entry).
);

$x =
array(
'a',
'b',
);

0 comments on commit 0b3c9af

Please sign in to comment.