Skip to content

Commit 31c7c05

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

3 files changed

Lines changed: 35 additions & 14 deletions

File tree

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

Lines changed: 21 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,30 @@ 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; $tokens[$i]['line'] === $line; $i--) {
223+
$length = ($tokens[$i]['column'] + $tokens[$i]['length'] - 1);
224+
225+
// Check the line limit of the namespaced name is equal or over the line length. This check accounts for the
226+
// fact that namespaced names are or closed by ; or opened by ( or {.
227+
if ($length >= $lineLimit
228+
&& ($tokens[$i]['code'] === T_NAME_QUALIFIED
229+
|| $tokens[$i]['code'] === T_NAME_FULLY_QUALIFIED
230+
|| $tokens[$i]['code'] === T_NAME_RELATIVE)
231+
) {
232+
return true;
224233
}
225-
);
234+
}
226235

227-
return $filteredTokens !== [];
236+
return false;
228237
}
229238
}

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)