Skip to content
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

Correct pluralization of the word space/spaces depending on the spacing value #128

Merged
merged 4 commits into from
Dec 8, 2023
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
24 changes: 21 additions & 3 deletions src/Standards/Generic/Sniffs/Arrays/ArrayIndentSniff.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
38 changes: 38 additions & 0 deletions src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,41 @@ $array = [
name: $value
),
];

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

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

// 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 0

// No test for open brace indent as that is _minimum_ and any actual value will be 0 or more, so with indent 0, this will never yield an error.

// 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
38 changes: 38 additions & 0 deletions src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,41 @@ $array = [
name: $value
),
];

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

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

// 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 0

// No test for open brace indent as that is _minimum_ and any actual value will be 0 or more, so with indent 0, this will never yield an error.

// 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
8 changes: 8 additions & 0 deletions src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ public function getErrorList()
88 => 1,
98 => 1,
110 => 1,
119 => 1,
126 => 1,
127 => 1,
133 => 1,
141 => 1,
142 => 1,
143 => 1,
149 => 1,
];

}//end getErrorList()
Expand Down
66 changes: 48 additions & 18 deletions src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading