Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion MO4/Sniffs/Arrays/ArrayDoubleArrowAlignmentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function process(File $phpcsFile, $stackPtr): void
$j = ($i - 1);

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

Expand Down
7 changes: 4 additions & 3 deletions MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,13 @@ public function process(File $phpcsFile, $stackPtr): void
private function getUseImport(File $phpcsFile, int $stackPtr)
{
$importTokens = [
T_NS_SEPARATOR,
T_NAME_FULLY_QUALIFIED,
T_NAME_QUALIFIED,
T_STRING,
];

$start = $phpcsFile->findNext(
PHP_CodeSniffer_Tokens::$emptyTokens,
PHP_CodeSniffer_Tokens::EMPTY_TOKENS,
($stackPtr + 1),
null,
true
Expand Down Expand Up @@ -238,7 +239,7 @@ private function checkIsNonImportUse(File $phpcsFile, int $stackPtr): bool
$tokens = $phpcsFile->getTokens();

$prev = $phpcsFile->findPrevious(
PHP_CodeSniffer_Tokens::$emptyTokens,
PHP_CodeSniffer_Tokens::EMPTY_TOKENS,
($stackPtr - 1),
0,
true,
Expand Down
88 changes: 36 additions & 52 deletions MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ class UnnecessaryNamespaceUsageSniff implements Sniff
/**
* Tokens used in full class name.
*
* @var array<int, int>
*/
private $classNameTokens = [
T_NS_SEPARATOR,
T_STRING,
private const CLASS_NAME_TOKENS = [
T_NAME_FULLY_QUALIFIED,
T_NAME_QUALIFIED,
T_NAME_RELATIVE,
];

/**
Expand Down Expand Up @@ -88,7 +88,9 @@ public function process(File $phpcsFile, $stackPtr): void
'@var' => 2,
];
$scanTokens = [
T_NS_SEPARATOR,
T_NAME_FULLY_QUALIFIED,
T_NAME_QUALIFIED,
T_NAME_RELATIVE,
T_DOC_COMMENT_OPEN_TAG,
];

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

while (false !== $nsSep) {
$classNameEnd = (int) $phpcsFile->findNext(
$this->classNameTokens,
self::CLASS_NAME_TOKENS,
$nsSep,
null,
true
);

if (T_NS_SEPARATOR === $tokens[$nsSep]['code']) {
if (T_STRING === $tokens[($nsSep - 1)]['code']) {
--$nsSep;
}

if (\in_array($tokens[$nsSep]['code'], self::CLASS_NAME_TOKENS, true)) {
$className = $phpcsFile->getTokensAsString(
$nsSep,
($classNameEnd - $nsSep)
Expand All @@ -122,7 +120,6 @@ public function process(File $phpcsFile, $stackPtr): void
$className,
$namespace,
$nsSep,
($classNameEnd - 1)
);
} else {
// Doc comment block.
Expand Down Expand Up @@ -193,8 +190,6 @@ public function process(File $phpcsFile, $stackPtr): void
$typeToken,
$namespace,
$docCommentStringPtr,
$docCommentStringPtr,
true
);
}
}
Expand Down Expand Up @@ -223,13 +218,13 @@ protected function getUseStatements(File $phpcsFile, int $start, int $end): arra

while (false !== $useTokenPtr) {
$classNameStart = (int) $phpcsFile->findNext(
PHP_CodeSniffer_Tokens::$emptyTokens,
PHP_CodeSniffer_Tokens::EMPTY_TOKENS,
($useTokenPtr + 1),
$end,
true
);
$classNameEnd = $phpcsFile->findNext(
$this->classNameTokens,
self::CLASS_NAME_TOKENS,
($classNameStart + 1),
$end,
true
Expand All @@ -255,7 +250,7 @@ protected function getUseStatements(File $phpcsFile, int $start, int $end): arra

/** @var int $aliasNamePtr */
$aliasNamePtr = $phpcsFile->findPrevious(
PHP_CodeSniffer_Tokens::$emptyTokens,
PHP_CodeSniffer_Tokens::EMPTY_TOKENS,
($useEnd - 1),
0,
true
Expand All @@ -264,8 +259,15 @@ protected function getUseStatements(File $phpcsFile, int $start, int $end): arra
$length = ($classNameEnd - $classNameStart);
$className = $phpcsFile->getTokensAsString($classNameStart, $length);

$className = $this->getFullyQualifiedClassName($className);
$useStatements[$className] = $tokens[$aliasNamePtr]['content'];
$className = $this->getFullyQualifiedClassName($className);
$tokenContent = $tokens[$aliasNamePtr]['content'];

if (\str_contains($tokenContent, '\\')) {
$path = \explode('\\', $tokenContent);
$tokenContent = $path[\array_key_last($path)];
}

$useStatements[$className] = $tokenContent;
$i = ($useEnd + 1);

$useTokenPtr = T_COMMA === $tokens[$useEnd]['code'] ? $i : $phpcsFile->findNext(T_USE, $i, $end);
Expand All @@ -287,7 +289,7 @@ protected function getNamespace(File $phpcsFile, int $start, int $end): string
{
$namespace = (int) $phpcsFile->findNext(T_NAMESPACE, $start, $end);
$namespaceStart = $phpcsFile->findNext(
PHP_CodeSniffer_Tokens::$emptyTokens,
PHP_CodeSniffer_Tokens::EMPTY_TOKENS,
($namespace + 1),
$end,
true
Expand All @@ -298,7 +300,7 @@ protected function getNamespace(File $phpcsFile, int $start, int $end): string
}

$namespaceEnd = (int) $phpcsFile->findNext(
$this->classNameTokens,
self::CLASS_NAME_TOKENS,
($namespaceStart + 1),
$end,
true
Expand Down Expand Up @@ -330,18 +332,13 @@ private function getFullyQualifiedClassName(string $className): string
* @param string $className class name
* @param string $namespace name space
* @param int $startPtr start token pointer
* @param int $endPtr end token pointer
* @param bool $isDocBlock true if fixing doc block
*
* @return void
*/
private function checkShorthandPossible(File $phpcsFile, array $useStatements, string $className, string $namespace, int $startPtr, int $endPtr, bool $isDocBlock = false): void
private function checkShorthandPossible(File $phpcsFile, array $useStatements, string $className, string $namespace, int $startPtr): void
{
$msg = 'Shorthand possible. Replace "%s" with "%s"';
$code = 'UnnecessaryNamespaceUsage';
$fixable = false;
$replaceClassName = false;
$replacement = '';
$msg = 'Shorthand possible. Replace "%s" with "%s"';
$code = 'UnnecessaryNamespaceUsage';

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

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

$fixable = $phpcsFile->addFixableWarning(
$phpcsFile->addFixableWarning(
$msg,
$startPtr,
$code,
$data
);

$replaceClassName = true;
} elseif ('' !== $namespace && \str_starts_with($fullClassName, $namespace)) {
$replacement = \substr($fullClassName, \strlen($namespace));

$data = [
$data = [
$className,
$replacement,
];
$fixable = $phpcsFile->addFixableWarning(

$phpcsFile->addFixableWarning(
$msg,
$startPtr,
$code,
$data
);
}

if (true !== $fixable) {
} else {
return;
}

$phpcsFile->fixer->beginChangeset();

if (true === $isDocBlock) {
$tokens = $phpcsFile->getTokens();
$oldContent = $tokens[$startPtr]['content'];
/** @var string $newContent */
$newContent = \str_replace($className, $replacement, $oldContent);
$phpcsFile->fixer->replaceToken($startPtr, $newContent);
} else {
for ($i = $startPtr; $i < $endPtr; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}

if (true === $replaceClassName) {
$phpcsFile->fixer->replaceToken($endPtr, $replacement);
}
}
$tokens = $phpcsFile->getTokens();
$oldContent = $tokens[$startPtr]['content'];
/** @var string $newContent */
$newContent = \str_replace($className, $replacement, $oldContent);
$phpcsFile->fixer->replaceToken($startPtr, $newContent);

$phpcsFile->fixer->endChangeset();
}
Expand Down
8 changes: 4 additions & 4 deletions MO4/Tests/AbstractMo4SniffUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
namespace MO4\Tests;

use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
use PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase;

/**
* Abstract class to make the writing of tests more convenient.
Expand All @@ -34,11 +34,11 @@
*
* @link https://github.com/mayflower/mo4-coding-standard
*/
abstract class AbstractMo4SniffUnitTest extends AbstractSniffUnitTest
abstract class AbstractMo4SniffUnitTest extends AbstractSniffTestCase
{
/**
* Array or Array containing the test file as key and as value the key-value pairs with line number and number of#
* errors as describe in @see AbstractSniffUnitTest::getErrorList
* errors as describe in @see AbstractSniffTestCase::getErrorList
*
* When the array is empty, the test will pass.
*
Expand All @@ -48,7 +48,7 @@ abstract class AbstractMo4SniffUnitTest extends AbstractSniffUnitTest

/**
* Array or Array containing the test file as key and as value the key-value pairs with line number and number of#
* errors as describe in @see AbstractSniffUnitTest::getWarningList
* errors as describe in @see AbstractSniffTestCase::getWarningList
*
* When the array is empty, the test will pass.
*
Expand Down
4 changes: 1 addition & 3 deletions MO4/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@
<rule ref="Generic.Formatting.SpaceAfterCast"/>
<!-- Align corresponding assignment statement tokens -->
<rule ref="Generic.Formatting.MultipleStatementAlignment">
<properties>
<property name="error" value="true"/>
</properties>
<type>error</type>
</rule>
<!-- Forbid useless inline string concatenation -->
<rule ref="Generic.Strings.UnnecessaryStringConcat">
Expand Down
13 changes: 10 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
"require": {
"php": "^8.1",
"dealerdirect/phpcodesniffer-composer-installer": "^0.7 || ^1.0",
"escapestudios/symfony2-coding-standard": "^3.16.0",
"slevomat/coding-standard": "^8.14",
"squizlabs/php_codesniffer": "^3.8.0"
"escapestudios/symfony2-coding-standard": "dev-phpcs4",
"slevomat/coding-standard": "dev-phpcs4",
"squizlabs/php_codesniffer": "4.x-dev"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.45",
"nikic/php-parser": "< 5.0.1",
"phan/phan": "^5.4.5",
"phpcsstandards/phpcsdevtools": "^1.2",
"phpstan/phpstan": "^2.0",
"phpstan/phpstan-strict-rules": "^2.0",
"phpunit/phpunit": "^9.6.15",
Expand All @@ -42,6 +43,12 @@
"symfony/polyfill-php83": "^1.32",
"vimeo/psalm": "^6.0.0"
},
"repositories": [
{
"type": "vcs",
"url": "[email protected]:mmoll/Symfony-coding-standard.git"
}
],
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
>
<testsuites>
<testsuite name="MO4 Ruleset Test Suite">
<file>vendor/squizlabs/php_codesniffer/tests/Standards/AllSniffs.php</file>
<directory>MO4/Tests</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="false">
Expand Down
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
require_once __DIR__.'/../vendor/squizlabs/php_codesniffer/tests/bootstrap.php';

// Add this Standard.
Config::setConfigData(
(new Config())->setConfigData(
'installed_paths',
__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.$myStandardName,
true
Expand Down
Loading