Skip to content

Commit d21c841

Browse files
committed
Only throw namespace error and warning if line cannot be shortened
1 parent e92d838 commit d21c841

3 files changed

Lines changed: 43 additions & 14 deletions

File tree

src/Standards/Generic/Sniffs/Files/LineLengthSniff.php

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ protected function checkLineLength(File $phpcsFile, array $tokens, int $stackPtr
178178
&& $lineLength > $this->absoluteLineLimit
179179
) {
180180
$code = 'MaxExceeded';
181-
if ($this->isLineNamespacedName($tokens, $tokens[$stackPtr]['line']) === true) {
181+
if ($this->isNamespacedNameExceedingLength($tokens, $stackPtr, $this->absoluteLineLimit) === true) {
182182
$code = 'NamespacedNameMaxExceeded';
183183
}
184184

@@ -191,7 +191,7 @@ protected function checkLineLength(File $phpcsFile, array $tokens, int $stackPtr
191191
$phpcsFile->addError($error, $stackPtr, $code, $data);
192192
} elseif ($lineLength > $this->lineLimit) {
193193
$code = 'TooLong';
194-
if ($this->isLineNamespacedName($tokens, $tokens[$stackPtr]['line']) === true) {
194+
if ($this->isNamespacedNameExceedingLength($tokens, $stackPtr, $this->lineLimit) === true) {
195195
$code = 'NamespacedNameTooLong';
196196
}
197197

@@ -209,21 +209,38 @@ protected function checkLineLength(File $phpcsFile, array $tokens, int $stackPtr
209209
/**
210210
* Checks if a line is a namespaced name
211211
*
212-
* @param array $tokens The token stack.
213-
* @param int $line The line to check validate
212+
* @param array $tokens The token stack.
213+
* @param int $stackPtr The last token on the line.
214+
* @param int $lineLimit The line limit.
214215
*
215216
* @return bool
216217
*/
217-
private function isLineNamespacedName(array $tokens, int $line): bool
218+
private function isNamespacedNameExceedingLength(array $tokens, int $stackPtr, int $lineLimit): bool
218219
{
219-
$filteredTokens = array_filter(
220-
$tokens,
221-
function ($token) use ($line) {
222-
return $token['line'] === $line
223-
&& in_array($token['code'], [T_NAME_QUALIFIED, T_NAME_FULLY_QUALIFIED, T_NAME_RELATIVE], true);
220+
$line = $tokens[$stackPtr]['line'];
221+
222+
for ($i = $stackPtr; $i--;) {
223+
// Only look back the tokens on this line else stop looking
224+
if ($tokens[$i]['line'] !== $line) {
225+
break;
226+
}
227+
228+
$length = ($tokens[$i]['column'] + $tokens[$i]['length'] - 1);
229+
230+
// Check the line limit of the namespaced name is equal or over the line length. This check accounts for the
231+
// fact that namespaced names are or closed by ; or opened by ( or {
232+
if (
233+
$length >= $lineLimit &&
234+
(
235+
$tokens[$i]['code'] === T_NAME_QUALIFIED ||
236+
$tokens[$i]['code'] === T_NAME_FULLY_QUALIFIED ||
237+
$tokens[$i]['code'] === T_NAME_RELATIVE
238+
)
239+
) {
240+
return true;
224241
}
225-
);
242+
}
226243

227-
return $filteredTokens !== [];
244+
return false;
228245
}
229246
}

src/Standards/Generic/Tests/Files/LineLengthUnitTest.5.inc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,13 @@ echo \ThisReallyLong\Long\Looooong\Loooooooong\UseStatement\ThatShouldThrowAWarn
3333
new \ThisReallyLong\Long\Looooong\Loooooooooooong\UseStatement\ThatShouldThrowAWarning\BecauseItsNotTooLong\ClassName();
3434

3535
// These are just a bit too long and should throw a NamespacedNameMaxExceeded error
36-
echo \ThisReallyLong\Long\Long\Looong\UseStatement\ThatShouldNotBeAllowed\AndThrowErrorsBecauseItsTooLong\functionCall();
37-
new \ThisReallyLong\Long\Long\Looooooong\UseStatement\ThatShouldNotBeAllowed\AndThrowErrorsBecauseItsTooLong\ClassName();
36+
echo \ThisReallyLong\Long\Long\Looooong\UseStatement\ThatShouldNotBeAllowed\AndThrowErrorsBecauseItsTooLong\functionCall();
37+
new \ThisReallyLong\Long\Long\Looooooooong\UseStatement\ThatShouldNotBeAllowed\AndThrowErrorsBecauseItsTooLong\ClassName();
38+
39+
// These are a bit too long but can be shortened and should throw a TooLong warning
40+
echo \This\Name\Space\IsGoingToBeJustWithinLimits\LongLongLooong\functionCall('foo', 'bar', 'baz');
41+
new \This\Name\Space\IsGoingToBeJustWithinLimits\LongLongLongLooong\ClassName('foo', 'bar', 'baz');
42+
43+
// These are just a bit too long but can be shortened and should throw a MaxExceeded error
44+
echo \ThisReallyLong\Long\Long\Looong\UseStatement\ThatShouldNotBeAllowed\AndThrowErrorsBecauseItsTooLong\functionCall('foo', 'bar', 'baz');
45+
new \ThisReallyLong\Long\Long\Looooooong\UseStatement\ThatShouldNotBeAllowed\AndThrowErrorsBecauseItsTooLong\ClassName('foo', 'bar', 'baz');

src/Standards/Generic/Tests/Files/LineLengthUnitTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public function getErrorList($testFile = '')
6868
25 => 1,
6969
36 => 1,
7070
37 => 1,
71+
44 => 1,
72+
45 => 1,
7173
];
7274

7375
default:
@@ -120,6 +122,8 @@ public function getWarningList($testFile = '')
120122
20 => 1,
121123
32 => 1,
122124
33 => 1,
125+
40 => 1,
126+
41 => 1,
123127
];
124128

125129
default:

0 commit comments

Comments
 (0)