Skip to content

Commit 3428851

Browse files
committed
wip
1 parent e14f0a2 commit 3428851

File tree

8 files changed

+51
-68
lines changed

8 files changed

+51
-68
lines changed

MO4/Sniffs/Arrays/ArrayDoubleArrowAlignmentSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function process(File $phpcsFile, $stackPtr): void
148148
$j = ($i - 1);
149149

150150
while (($j >= 0) && ($tokens[$j]['line'] === $current['line'])) {
151-
if (!\in_array($tokens[$j]['code'], PHP_CodeSniffer_Tokens::$emptyTokens, true)) {
151+
if (!\in_array($tokens[$j]['code'], PHP_CodeSniffer_Tokens::EMPTY_TOKENS, true)) {
152152
$hasKeyInLine = true;
153153
}
154154

MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,13 @@ public function process(File $phpcsFile, $stackPtr): void
176176
private function getUseImport(File $phpcsFile, int $stackPtr)
177177
{
178178
$importTokens = [
179-
T_NS_SEPARATOR,
179+
T_NAME_FULLY_QUALIFIED,
180+
T_NAME_QUALIFIED,
180181
T_STRING,
181182
];
182183

183184
$start = $phpcsFile->findNext(
184-
PHP_CodeSniffer_Tokens::$emptyTokens,
185+
PHP_CodeSniffer_Tokens::EMPTY_TOKENS,
185186
($stackPtr + 1),
186187
null,
187188
true
@@ -235,7 +236,7 @@ private function checkIsNonImportUse(File $phpcsFile, int $stackPtr): bool
235236
$tokens = $phpcsFile->getTokens();
236237

237238
$prev = $phpcsFile->findPrevious(
238-
PHP_CodeSniffer_Tokens::$emptyTokens,
239+
PHP_CodeSniffer_Tokens::EMPTY_TOKENS,
239240
($stackPtr - 1),
240241
0,
241242
true,

MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php

Lines changed: 36 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ class UnnecessaryNamespaceUsageSniff implements Sniff
4242
/**
4343
* Tokens used in full class name.
4444
*
45-
* @var array<int, int>
4645
*/
47-
private $classNameTokens = [
48-
T_NS_SEPARATOR,
49-
T_STRING,
46+
private const CLASS_NAME_TOKENS = [
47+
T_NAME_FULLY_QUALIFIED,
48+
T_NAME_QUALIFIED,
49+
T_NAME_RELATIVE,
5050
];
5151

5252
/**
@@ -87,7 +87,9 @@ public function process(File $phpcsFile, $stackPtr): void
8787
'@var' => 2,
8888
];
8989
$scanTokens = [
90-
T_NS_SEPARATOR,
90+
T_NAME_FULLY_QUALIFIED,
91+
T_NAME_QUALIFIED,
92+
T_NAME_RELATIVE,
9193
T_DOC_COMMENT_OPEN_TAG,
9294
];
9395

@@ -99,17 +101,13 @@ public function process(File $phpcsFile, $stackPtr): void
99101

100102
while (false !== $nsSep) {
101103
$classNameEnd = (int) $phpcsFile->findNext(
102-
$this->classNameTokens,
104+
self::CLASS_NAME_TOKENS,
103105
$nsSep,
104106
null,
105107
true
106108
);
107109

108-
if (T_NS_SEPARATOR === $tokens[$nsSep]['code']) {
109-
if (T_STRING === $tokens[($nsSep - 1)]['code']) {
110-
--$nsSep;
111-
}
112-
110+
if (\in_array($tokens[$nsSep]['code'], self::CLASS_NAME_TOKENS, true)) {
113111
$className = $phpcsFile->getTokensAsString(
114112
$nsSep,
115113
($classNameEnd - $nsSep)
@@ -121,7 +119,6 @@ public function process(File $phpcsFile, $stackPtr): void
121119
$className,
122120
$namespace,
123121
$nsSep,
124-
($classNameEnd - 1)
125122
);
126123
} else {
127124
// Doc comment block.
@@ -192,8 +189,6 @@ public function process(File $phpcsFile, $stackPtr): void
192189
$typeToken,
193190
$namespace,
194191
$docCommentStringPtr,
195-
$docCommentStringPtr,
196-
true
197192
);
198193
}
199194
}
@@ -222,13 +217,13 @@ protected function getUseStatements(File $phpcsFile, int $start, int $end): arra
222217

223218
while (false !== $useTokenPtr) {
224219
$classNameStart = (int) $phpcsFile->findNext(
225-
PHP_CodeSniffer_Tokens::$emptyTokens,
220+
PHP_CodeSniffer_Tokens::EMPTY_TOKENS,
226221
($useTokenPtr + 1),
227222
$end,
228223
true
229224
);
230225
$classNameEnd = $phpcsFile->findNext(
231-
$this->classNameTokens,
226+
self::CLASS_NAME_TOKENS,
232227
($classNameStart + 1),
233228
$end,
234229
true
@@ -254,7 +249,7 @@ protected function getUseStatements(File $phpcsFile, int $start, int $end): arra
254249

255250
/** @var int $aliasNamePtr */
256251
$aliasNamePtr = $phpcsFile->findPrevious(
257-
PHP_CodeSniffer_Tokens::$emptyTokens,
252+
PHP_CodeSniffer_Tokens::EMPTY_TOKENS,
258253
($useEnd - 1),
259254
0,
260255
true
@@ -263,8 +258,15 @@ protected function getUseStatements(File $phpcsFile, int $start, int $end): arra
263258
$length = ($classNameEnd - $classNameStart);
264259
$className = $phpcsFile->getTokensAsString($classNameStart, $length);
265260

266-
$className = $this->getFullyQualifiedClassName($className);
267-
$useStatements[$className] = $tokens[$aliasNamePtr]['content'];
261+
$className = $this->getFullyQualifiedClassName($className);
262+
$tokenContent = $tokens[$aliasNamePtr]['content'];
263+
264+
if (\str_contains($tokenContent, '\\')) {
265+
$path = \explode('\\', $tokenContent);
266+
$tokenContent = $path[\array_key_last($path)];
267+
}
268+
269+
$useStatements[$className] = $tokenContent;
268270
$i = ($useEnd + 1);
269271

270272
$useTokenPtr = T_COMMA === $tokens[$useEnd]['code'] ? $i : $phpcsFile->findNext(T_USE, $i, $end);
@@ -285,7 +287,7 @@ protected function getNamespace(File $phpcsFile, int $start, int $end): string
285287
{
286288
$namespace = (int) $phpcsFile->findNext(T_NAMESPACE, $start, $end);
287289
$namespaceStart = $phpcsFile->findNext(
288-
PHP_CodeSniffer_Tokens::$emptyTokens,
290+
PHP_CodeSniffer_Tokens::EMPTY_TOKENS,
289291
($namespace + 1),
290292
$end,
291293
true
@@ -296,7 +298,7 @@ protected function getNamespace(File $phpcsFile, int $start, int $end): string
296298
}
297299

298300
$namespaceEnd = (int) $phpcsFile->findNext(
299-
$this->classNameTokens,
301+
self::CLASS_NAME_TOKENS,
300302
($namespaceStart + 1),
301303
$end,
302304
true
@@ -327,17 +329,12 @@ private function getFullyQualifiedClassName(string $className): string
327329
* @param string $className class name
328330
* @param string $namespace name space
329331
* @param int $startPtr start token pointer
330-
* @param int $endPtr end token pointer
331-
* @param bool $isDocBlock true if fixing doc block
332332
*
333333
*/
334-
private function checkShorthandPossible(File $phpcsFile, array $useStatements, string $className, string $namespace, int $startPtr, int $endPtr, bool $isDocBlock = false): void
334+
private function checkShorthandPossible(File $phpcsFile, array $useStatements, string $className, string $namespace, int $startPtr): void
335335
{
336-
$msg = 'Shorthand possible. Replace "%s" with "%s"';
337-
$code = 'UnnecessaryNamespaceUsage';
338-
$fixable = false;
339-
$replaceClassName = false;
340-
$replacement = '';
336+
$msg = 'Shorthand possible. Replace "%s" with "%s"';
337+
$code = 'UnnecessaryNamespaceUsage';
341338

342339
$fullClassName = $this->getFullyQualifiedClassName($className);
343340

@@ -349,50 +346,37 @@ private function checkShorthandPossible(File $phpcsFile, array $useStatements, s
349346
$replacement,
350347
];
351348

352-
$fixable = $phpcsFile->addFixableWarning(
349+
$phpcsFile->addFixableWarning(
353350
$msg,
354351
$startPtr,
355352
$code,
356353
$data
357354
);
358-
359-
$replaceClassName = true;
360355
} elseif ('' !== $namespace && \str_starts_with($fullClassName, $namespace)) {
361356
$replacement = \substr($fullClassName, \strlen($namespace));
362357

363-
$data = [
358+
$data = [
364359
$className,
365360
$replacement,
366361
];
367-
$fixable = $phpcsFile->addFixableWarning(
362+
363+
$phpcsFile->addFixableWarning(
368364
$msg,
369365
$startPtr,
370366
$code,
371367
$data
372368
);
373-
}
374-
375-
if (true !== $fixable) {
369+
} else {
376370
return;
377371
}
378372

379373
$phpcsFile->fixer->beginChangeset();
380374

381-
if (true === $isDocBlock) {
382-
$tokens = $phpcsFile->getTokens();
383-
$oldContent = $tokens[$startPtr]['content'];
384-
/** @var string $newContent */
385-
$newContent = \str_replace($className, $replacement, $oldContent);
386-
$phpcsFile->fixer->replaceToken($startPtr, $newContent);
387-
} else {
388-
for ($i = $startPtr; $i < $endPtr; $i++) {
389-
$phpcsFile->fixer->replaceToken($i, '');
390-
}
391-
392-
if (true === $replaceClassName) {
393-
$phpcsFile->fixer->replaceToken($endPtr, $replacement);
394-
}
395-
}
375+
$tokens = $phpcsFile->getTokens();
376+
$oldContent = $tokens[$startPtr]['content'];
377+
/** @var string $newContent */
378+
$newContent = \str_replace($className, $replacement, $oldContent);
379+
$phpcsFile->fixer->replaceToken($startPtr, $newContent);
396380

397381
$phpcsFile->fixer->endChangeset();
398382
}

MO4/Tests/AbstractMo4SniffUnitTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
namespace MO4\Tests;
1616

1717
use PHP_CodeSniffer\Exceptions\RuntimeException;
18-
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
18+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase;
1919

2020
/**
2121
* Abstract class to make the writing of tests more convenient.
@@ -34,11 +34,11 @@
3434
*
3535
* @link https://github.com/mayflower/mo4-coding-standard
3636
*/
37-
abstract class AbstractMo4SniffUnitTest extends AbstractSniffUnitTest
37+
abstract class AbstractMo4SniffUnitTest extends AbstractSniffTestCase
3838
{
3939
/**
4040
* Array or Array containing the test file as key and as value the key-value pairs with line number and number of#
41-
* errors as describe in @see AbstractSniffUnitTest::getErrorList
41+
* errors as describe in @see AbstractSniffTestCase::getErrorList
4242
*
4343
* When the array is empty, the test will pass.
4444
*
@@ -48,7 +48,7 @@ abstract class AbstractMo4SniffUnitTest extends AbstractSniffUnitTest
4848

4949
/**
5050
* Array or Array containing the test file as key and as value the key-value pairs with line number and number of#
51-
* errors as describe in @see AbstractSniffUnitTest::getWarningList
51+
* errors as describe in @see AbstractSniffTestCase::getWarningList
5252
*
5353
* When the array is empty, the test will pass.
5454
*

MO4/ruleset.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@
5151
<rule ref="Generic.Formatting.SpaceAfterCast"/>
5252
<!-- Align corresponding assignment statement tokens -->
5353
<rule ref="Generic.Formatting.MultipleStatementAlignment">
54-
<properties>
55-
<property name="error" value="true"/>
56-
</properties>
54+
<type>error</type>
5755
</rule>
5856
<!-- Forbid useless inline string concatenation -->
5957
<rule ref="Generic.Strings.UnnecessaryStringConcat">

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
"require": {
2626
"php": "^8.1",
2727
"dealerdirect/phpcodesniffer-composer-installer": "^0.7 || ^1.0",
28-
"escapestudios/symfony2-coding-standard": "^3.16.0",
29-
"slevomat/coding-standard": "^8.20",
30-
"squizlabs/php_codesniffer": "^3.8.0"
28+
"escapestudios/symfony2-coding-standard": "^3.17",
29+
"slevomat/coding-standard": "^8.23",
30+
"squizlabs/php_codesniffer": "^4.0"
3131
},
3232
"require-dev": {
3333
"ergebnis/composer-normalize": "^2.45",

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
>
99
<testsuites>
1010
<testsuite name="MO4 Ruleset Test Suite">
11-
<file>vendor/squizlabs/php_codesniffer/tests/Standards/AllSniffs.php</file>
11+
<directory>MO4/Tests</directory>
1212
</testsuite>
1313
</testsuites>
1414
<coverage processUncoveredFiles="false">

tests/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
require_once __DIR__.'/../vendor/squizlabs/php_codesniffer/tests/bootstrap.php';
2323

2424
// Add this Standard.
25-
Config::setConfigData(
25+
(new Config())->setConfigData(
2626
'installed_paths',
2727
__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.$myStandardName,
2828
true

0 commit comments

Comments
 (0)