Skip to content

Commit e92d838

Browse files
committed
feat: Use different warning and errors for namespaced names
1 parent 00cef30 commit e92d838

3 files changed

Lines changed: 89 additions & 2 deletions

File tree

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,21 +177,53 @@ protected function checkLineLength(File $phpcsFile, array $tokens, int $stackPtr
177177
if ($this->absoluteLineLimit > 0
178178
&& $lineLength > $this->absoluteLineLimit
179179
) {
180+
$code = 'MaxExceeded';
181+
if ($this->isLineNamespacedName($tokens, $tokens[$stackPtr]['line']) === true) {
182+
$code = 'NamespacedNameMaxExceeded';
183+
}
184+
180185
$data = [
181186
$this->absoluteLineLimit,
182187
$lineLength,
183188
];
184189

185190
$error = 'Line exceeds maximum limit of %s characters; contains %s characters';
186-
$phpcsFile->addError($error, $stackPtr, 'MaxExceeded', $data);
191+
$phpcsFile->addError($error, $stackPtr, $code, $data);
187192
} elseif ($lineLength > $this->lineLimit) {
193+
$code = 'TooLong';
194+
if ($this->isLineNamespacedName($tokens, $tokens[$stackPtr]['line']) === true) {
195+
$code = 'NamespacedNameTooLong';
196+
}
197+
188198
$data = [
189199
$this->lineLimit,
190200
$lineLength,
191201
];
192202

193203
$warning = 'Line exceeds %s characters; contains %s characters';
194-
$phpcsFile->addWarning($warning, $stackPtr, 'TooLong', $data);
204+
$phpcsFile->addWarning($warning, $stackPtr, $code, $data);
195205
}
196206
}
207+
208+
209+
/**
210+
* Checks if a line is a namespaced name
211+
*
212+
* @param array $tokens The token stack.
213+
* @param int $line The line to check validate
214+
*
215+
* @return bool
216+
*/
217+
private function isLineNamespacedName(array $tokens, int $line): bool
218+
{
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);
224+
}
225+
);
226+
227+
return $filteredTokens !== [];
228+
}
197229
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
// This is just within the limit
4+
namespace This\Name\Space\IsGoingToBeJustWithinLimits\LongLongLongLongLongLoong;
5+
6+
// This is just within the absolute limit and should throw a NamespacedNameTooLong warning
7+
namespace This\Name\Space\IsGoingToBeJustWithinLimits\LongLongLongLongLongLongLongLongLongLongLongLongLongLongLoooooong;
8+
9+
// This is too long and should throw a NamespacedNameMaxExceeded error
10+
namespace This\Name\Space\IsGoingToBeTooLong\LongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLoooong;
11+
12+
// These are just within the limits
13+
use ThisReallyLong\Looooooooooooooong\UseStatement\ShouldBeNotTooLong\ClassName;
14+
use function ThisReallyLong\Looong\UseStatement\ShouldBeNotTooLong\functionCall;
15+
use const ThisReallyLong\Loooooooooong\UseStatement\ShouldBeNotTooLong\CONSTANT;
16+
17+
// These are just a bit too long and should throw a NamespacedNameTooLong warning
18+
use ThisReallyLong\Long\Looooong\Looooooooooooooong\UseStatement\ThatShouldThrowAWarning\BecauseItsNotTooLong\ClassName;
19+
use function ThisReallyLong\Long\Looooooong\Long\UseStatement\ThatShouldThrowAWarning\BecauseItsNotTooLong\functionCall;
20+
use const ThisReallyLong\Long\Looooong\Loooooooooong\UseStatement\ThatShouldThrowAWarning\BecauseItsNotTooLong\CONSTANT;
21+
22+
// These are just a bit too long and should throw a NamespacedNameMaxExceeded error
23+
use ThisReallyLong\Long\Loooooong\Looooong\UseStatement\ThatShouldNotBeAllowed\AndThrowErrorsBecauseItsTooLong\ClassName;
24+
use function ThisReallyLong\Long\Looong\UseStatement\ThatShouldNotBeAllowed\AndThrowErrorsBecauseItsTooLong\functionCall;
25+
use const ThisReallyLong\Long\Loooooooooong\UseStatement\ThatShouldNotBeAllowed\AndThrowErrorsBecauseItsTooLong\CONSTANT;
26+
27+
// These are just within the limits
28+
echo \This\Name\Space\IsGoingToBeJustWithinLimits\LongLongLooong\functionCall();
29+
new \This\Name\Space\IsGoingToBeJustWithinLimits\LongLongLongLooong\ClassName();
30+
31+
// These are just a bit too long and should throw a NamespacedNameTooLong warning
32+
echo \ThisReallyLong\Long\Looooong\Loooooooong\UseStatement\ThatShouldThrowAWarning\BecauseItsNotTooLong\functionCall();
33+
new \ThisReallyLong\Long\Looooong\Loooooooooooong\UseStatement\ThatShouldThrowAWarning\BecauseItsNotTooLong\ClassName();
34+
35+
// 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();

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ public function getErrorList($testFile = '')
6060
case 'LineLengthUnitTest.2.inc':
6161
case 'LineLengthUnitTest.3.inc':
6262
return [7 => 1];
63+
case 'LineLengthUnitTest.5.inc':
64+
return [
65+
10 => 1,
66+
23 => 1,
67+
24 => 1,
68+
25 => 1,
69+
36 => 1,
70+
37 => 1,
71+
];
6372

6473
default:
6574
return [];
@@ -103,6 +112,15 @@ public function getWarningList($testFile = '')
103112
10 => 1,
104113
14 => 1,
105114
];
115+
case 'LineLengthUnitTest.5.inc':
116+
return [
117+
7 => 1,
118+
18 => 1,
119+
19 => 1,
120+
20 => 1,
121+
32 => 1,
122+
33 => 1,
123+
];
106124

107125
default:
108126
return [];

0 commit comments

Comments
 (0)