Skip to content

Commit 1b28a6e

Browse files
committed
wip
1 parent 93dad05 commit 1b28a6e

File tree

8 files changed

+57
-67
lines changed

8 files changed

+57
-67
lines changed

MO4/Sniffs/Arrays/ArrayDoubleArrowAlignmentSniff.php

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

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

MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ public function process(File $phpcsFile, $stackPtr): void
177177
private function getUseImport(File $phpcsFile, int $stackPtr)
178178
{
179179
$importTokens = [
180-
T_NS_SEPARATOR,
180+
T_NAME_FULLY_QUALIFIED,
181+
T_NAME_QUALIFIED,
181182
T_STRING,
182183
];
183184

@@ -238,7 +239,7 @@ private function checkIsNonImportUse(File $phpcsFile, int $stackPtr): bool
238239
$tokens = $phpcsFile->getTokens();
239240

240241
$prev = $phpcsFile->findPrevious(
241-
PHP_CodeSniffer_Tokens::$emptyTokens,
242+
PHP_CodeSniffer_Tokens::EMPTY_TOKENS,
242243
($stackPtr - 1),
243244
0,
244245
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
/**
@@ -88,7 +88,9 @@ public function process(File $phpcsFile, $stackPtr): void
8888
'@var' => 2,
8989
];
9090
$scanTokens = [
91-
T_NS_SEPARATOR,
91+
T_NAME_FULLY_QUALIFIED,
92+
T_NAME_QUALIFIED,
93+
T_NAME_RELATIVE,
9294
T_DOC_COMMENT_OPEN_TAG,
9395
];
9496

@@ -100,17 +102,13 @@ public function process(File $phpcsFile, $stackPtr): void
100102

101103
while (false !== $nsSep) {
102104
$classNameEnd = (int) $phpcsFile->findNext(
103-
$this->classNameTokens,
105+
self::CLASS_NAME_TOKENS,
104106
$nsSep,
105107
null,
106108
true
107109
);
108110

109-
if (T_NS_SEPARATOR === $tokens[$nsSep]['code']) {
110-
if (T_STRING === $tokens[($nsSep - 1)]['code']) {
111-
--$nsSep;
112-
}
113-
111+
if (\in_array($tokens[$nsSep]['code'], self::CLASS_NAME_TOKENS, true)) {
114112
$className = $phpcsFile->getTokensAsString(
115113
$nsSep,
116114
($classNameEnd - $nsSep)
@@ -122,7 +120,6 @@ public function process(File $phpcsFile, $stackPtr): void
122120
$className,
123121
$namespace,
124122
$nsSep,
125-
($classNameEnd - 1)
126123
);
127124
} else {
128125
// Doc comment block.
@@ -193,8 +190,6 @@ public function process(File $phpcsFile, $stackPtr): void
193190
$typeToken,
194191
$namespace,
195192
$docCommentStringPtr,
196-
$docCommentStringPtr,
197-
true
198193
);
199194
}
200195
}
@@ -223,13 +218,13 @@ protected function getUseStatements(File $phpcsFile, int $start, int $end): arra
223218

224219
while (false !== $useTokenPtr) {
225220
$classNameStart = (int) $phpcsFile->findNext(
226-
PHP_CodeSniffer_Tokens::$emptyTokens,
221+
PHP_CodeSniffer_Tokens::EMPTY_TOKENS,
227222
($useTokenPtr + 1),
228223
$end,
229224
true
230225
);
231226
$classNameEnd = $phpcsFile->findNext(
232-
$this->classNameTokens,
227+
self::CLASS_NAME_TOKENS,
233228
($classNameStart + 1),
234229
$end,
235230
true
@@ -255,7 +250,7 @@ protected function getUseStatements(File $phpcsFile, int $start, int $end): arra
255250

256251
/** @var int $aliasNamePtr */
257252
$aliasNamePtr = $phpcsFile->findPrevious(
258-
PHP_CodeSniffer_Tokens::$emptyTokens,
253+
PHP_CodeSniffer_Tokens::EMPTY_TOKENS,
259254
($useEnd - 1),
260255
0,
261256
true
@@ -264,8 +259,15 @@ protected function getUseStatements(File $phpcsFile, int $start, int $end): arra
264259
$length = ($classNameEnd - $classNameStart);
265260
$className = $phpcsFile->getTokensAsString($classNameStart, $length);
266261

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

271273
$useTokenPtr = T_COMMA === $tokens[$useEnd]['code'] ? $i : $phpcsFile->findNext(T_USE, $i, $end);
@@ -287,7 +289,7 @@ protected function getNamespace(File $phpcsFile, int $start, int $end): string
287289
{
288290
$namespace = (int) $phpcsFile->findNext(T_NAMESPACE, $start, $end);
289291
$namespaceStart = $phpcsFile->findNext(
290-
PHP_CodeSniffer_Tokens::$emptyTokens,
292+
PHP_CodeSniffer_Tokens::EMPTY_TOKENS,
291293
($namespace + 1),
292294
$end,
293295
true
@@ -298,7 +300,7 @@ protected function getNamespace(File $phpcsFile, int $start, int $end): string
298300
}
299301

300302
$namespaceEnd = (int) $phpcsFile->findNext(
301-
$this->classNameTokens,
303+
self::CLASS_NAME_TOKENS,
302304
($namespaceStart + 1),
303305
$end,
304306
true
@@ -330,18 +332,13 @@ private function getFullyQualifiedClassName(string $className): string
330332
* @param string $className class name
331333
* @param string $namespace name space
332334
* @param int $startPtr start token pointer
333-
* @param int $endPtr end token pointer
334-
* @param bool $isDocBlock true if fixing doc block
335335
*
336336
* @return void
337337
*/
338-
private function checkShorthandPossible(File $phpcsFile, array $useStatements, string $className, string $namespace, int $startPtr, int $endPtr, bool $isDocBlock = false): void
338+
private function checkShorthandPossible(File $phpcsFile, array $useStatements, string $className, string $namespace, int $startPtr): void
339339
{
340-
$msg = 'Shorthand possible. Replace "%s" with "%s"';
341-
$code = 'UnnecessaryNamespaceUsage';
342-
$fixable = false;
343-
$replaceClassName = false;
344-
$replacement = '';
340+
$msg = 'Shorthand possible. Replace "%s" with "%s"';
341+
$code = 'UnnecessaryNamespaceUsage';
345342

346343
$fullClassName = $this->getFullyQualifiedClassName($className);
347344

@@ -353,50 +350,37 @@ private function checkShorthandPossible(File $phpcsFile, array $useStatements, s
353350
$replacement,
354351
];
355352

356-
$fixable = $phpcsFile->addFixableWarning(
353+
$phpcsFile->addFixableWarning(
357354
$msg,
358355
$startPtr,
359356
$code,
360357
$data
361358
);
362-
363-
$replaceClassName = true;
364359
} elseif ('' !== $namespace && \str_starts_with($fullClassName, $namespace)) {
365360
$replacement = \substr($fullClassName, \strlen($namespace));
366361

367-
$data = [
362+
$data = [
368363
$className,
369364
$replacement,
370365
];
371-
$fixable = $phpcsFile->addFixableWarning(
366+
367+
$phpcsFile->addFixableWarning(
372368
$msg,
373369
$startPtr,
374370
$code,
375371
$data
376372
);
377-
}
378-
379-
if (true !== $fixable) {
373+
} else {
380374
return;
381375
}
382376

383377
$phpcsFile->fixer->beginChangeset();
384378

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

401385
$phpcsFile->fixer->endChangeset();
402386
}

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
@@ -47,9 +47,7 @@
4747
<rule ref="Generic.Formatting.SpaceAfterCast"/>
4848
<!-- Align corresponding assignment statement tokens -->
4949
<rule ref="Generic.Formatting.MultipleStatementAlignment">
50-
<properties>
51-
<property name="error" value="true"/>
52-
</properties>
50+
<type>error</type>
5351
</rule>
5452
<!-- Forbid useless inline string concatenation -->
5553
<rule ref="Generic.Strings.UnnecessaryStringConcat">

composer.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@
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.14",
30-
"squizlabs/php_codesniffer": "^3.8.0"
28+
"escapestudios/symfony2-coding-standard": "dev-phpcs4",
29+
"slevomat/coding-standard": "dev-phpcs4",
30+
"squizlabs/php_codesniffer": "4.x-dev"
3131
},
3232
"require-dev": {
3333
"ergebnis/composer-normalize": "^2.45",
3434
"nikic/php-parser": "< 5.0.1",
3535
"phan/phan": "^5.4.5",
36+
"phpcsstandards/phpcsdevtools": "^1.2",
3637
"phpstan/phpstan": "^2.0",
3738
"phpstan/phpstan-strict-rules": "^2.0",
3839
"phpunit/phpunit": "^9.6.15",
@@ -42,6 +43,12 @@
4243
"symfony/polyfill-php83": "^1.32",
4344
"vimeo/psalm": "^6.0.0"
4445
},
46+
"repositories": [
47+
{
48+
"type": "vcs",
49+
"url": "[email protected]:mmoll/Symfony-coding-standard.git"
50+
}
51+
],
4552
"config": {
4653
"allow-plugins": {
4754
"dealerdirect/phpcodesniffer-composer-installer": true,

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)