Skip to content

Commit 0804ea6

Browse files
committed
wip
1 parent 93dad05 commit 0804ea6

File tree

7 files changed

+35
-23
lines changed

7 files changed

+35
-23
lines changed

MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php

Lines changed: 2 additions & 1 deletion
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

MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ class UnnecessaryNamespaceUsageSniff implements Sniff
4545
* @var array<int, int>
4646
*/
4747
private $classNameTokens = [
48-
T_NS_SEPARATOR,
49-
T_STRING,
48+
T_NAME_FULLY_QUALIFIED,
49+
T_NAME_QUALIFIED,
50+
T_NAME_RELATIVE,
5051
];
5152

5253
/**
@@ -88,7 +89,9 @@ public function process(File $phpcsFile, $stackPtr): void
8889
'@var' => 2,
8990
];
9091
$scanTokens = [
91-
T_NS_SEPARATOR,
92+
T_NAME_FULLY_QUALIFIED,
93+
T_NAME_QUALIFIED,
94+
T_NAME_RELATIVE,
9295
T_DOC_COMMENT_OPEN_TAG,
9396
];
9497

@@ -106,11 +109,7 @@ public function process(File $phpcsFile, $stackPtr): void
106109
true
107110
);
108111

109-
if (T_NS_SEPARATOR === $tokens[$nsSep]['code']) {
110-
if (T_STRING === $tokens[($nsSep - 1)]['code']) {
111-
--$nsSep;
112-
}
113-
112+
if (\in_array($tokens[$nsSep]['code'], [T_NAME_FULLY_QUALIFIED, T_NAME_QUALIFIED, T_NAME_RELATIVE], true)) {
114113
$className = $phpcsFile->getTokensAsString(
115114
$nsSep,
116115
($classNameEnd - $nsSep)
@@ -264,8 +263,15 @@ protected function getUseStatements(File $phpcsFile, int $start, int $end): arra
264263
$length = ($classNameEnd - $classNameStart);
265264
$className = $phpcsFile->getTokensAsString($classNameStart, $length);
266265

267-
$className = $this->getFullyQualifiedClassName($className);
268-
$useStatements[$className] = $tokens[$aliasNamePtr]['content'];
266+
$className = $this->getFullyQualifiedClassName($className);
267+
$tokenContent = $tokens[$aliasNamePtr]['content'];
268+
269+
if (\str_contains($tokenContent, '\\')) {
270+
$path = \explode('\\', $tokenContent);
271+
$tokenContent = $path[\array_key_last($path)];
272+
}
273+
274+
$useStatements[$className] = $tokenContent;
269275
$i = ($useEnd + 1);
270276

271277
$useTokenPtr = T_COMMA === $tokens[$useEnd]['code'] ? $i : $phpcsFile->findNext(T_USE, $i, $end);

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": "git@github.com: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)