Skip to content

Commit 5aaef9c

Browse files
committed
feat: add PHP_CodeSniffer 4.x support
Upgrade squizlabs/php_codesniffer from ^3.13.5 to ^4.0 and slevomat/coding-standard from ^8.22.1 to ^8.23. Changes: - File.php: typed addMessage() signature, fixableCount replaced by fixableErrorCount/fixableWarningCount - SniffDecorator.php: typed process() parameter - ForbiddenSetterSniff.php: typed process() parameter, dotted error code to comply with PHPCS 4 naming validation, getDeclarationName empty string check - Metrics: removed CodeAnalyzerSniff (dropped from Zend in PHPCS 4, redundant with SyntaxCheck), removed CallTimePassByReferenceSniff (dropped, irrelevant since PHP 5.4), moved LanguageConstructSpacingSniff from Squiz to Generic namespace - TestCase: manual sniff registration to bypass PHPCS 4 namespace convention validation for custom sniffs Closes #723
1 parent aa5bdea commit 5aaef9c

10 files changed

Lines changed: 45 additions & 54 deletions

File tree

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
"psr/container": "^2.0.2",
3131
"psr/simple-cache": "^2.0|^3.0",
3232
"sebastian/diff": "^7.0|^8.0|^9.0",
33-
"slevomat/coding-standard": "^8.22.1",
34-
"squizlabs/php_codesniffer": "^3.13.5",
33+
"slevomat/coding-standard": "^8.23",
34+
"squizlabs/php_codesniffer": "^4.0",
3535
"symfony/cache": "^7.4.5|^8.1.2",
3636
"symfony/console": "^7.4.4|^8.1.2",
3737
"symfony/finder": "^7.4.5|^8.1.1",

src/Domain/File.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,26 @@ public function disableFix(): void
100100
* {@inheritdoc}
101101
*/
102102
protected function addMessage(
103-
$isError,
104-
$message,
105-
$line,
106-
$column,
107-
$sniffClassOrCode,
108-
$data,
109-
$severity,
110-
$isFixable = false
103+
bool $isError,
104+
string $message,
105+
int $line,
106+
int $column,
107+
string $sniffClassOrCode,
108+
array $data,
109+
int $severity,
110+
bool $isFixable = false
111111
): bool {
112112
$message = $data !== [] ? vsprintf($message, $data) : $message;
113113

114114
if ($isFixable && $this->isFixable) {
115115
if ($this->fixEnabled) {
116116
$this->activeSniff->addFileFixed($this->fileInfo->getRelativePathname());
117117
} else {
118-
$this->fixableCount++;
118+
if ($isError) {
119+
$this->fixableErrorCount++;
120+
} else {
121+
$this->fixableWarningCount++;
122+
}
119123
}
120124

121125
return true;

src/Domain/Insights/SniffDecorator.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,7 @@ public function register(): array
5757
return $this->sniff->register();
5858
}
5959

60-
/**
61-
* @param int $stackPtr
62-
*
63-
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
64-
*/
65-
public function process(File $file, $stackPtr): int|null
60+
public function process(File $file, int $stackPtr): int|null
6661
{
6762
if ($file instanceof InsightFile && $this->skipFilesFromIgnoreFiles($file)) {
6863
return null;

src/Domain/Metrics/Code/Code.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@
2121
use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\DiscourageGotoSniff;
2222
use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\NoSilencedErrorsSniff;
2323
use PHP_CodeSniffer\Standards\Generic\Sniffs\Strings\UnnecessaryStringConcatSniff;
24+
use PHP_CodeSniffer\Standards\Generic\Sniffs\WhiteSpace\LanguageConstructSpacingSniff;
2425
use PHP_CodeSniffer\Standards\PSR12\Sniffs\Keywords\ShortFormTypeKeywordsSniff;
2526
use PHP_CodeSniffer\Standards\PSR2\Sniffs\ControlStructures\SwitchDeclarationSniff;
2627
use PHP_CodeSniffer\Standards\Squiz\Sniffs\PHP\EvalSniff;
27-
use PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\LanguageConstructSpacingSniff;
28-
use PHP_CodeSniffer\Standards\Zend\Sniffs\Debug\CodeAnalyzerSniff;
2928
use PhpCsFixer\Fixer\Alias\NoMixedEchoPrintFixer;
3029
use PhpCsFixer\Fixer\ArrayNotation\NoMultilineWhitespaceAroundDoubleArrowFixer;
3130
use PhpCsFixer\Fixer\ArrayNotation\NormalizeIndexBraceFixer;
@@ -80,7 +79,6 @@ public function getInsights(): array
8079
{
8180
return [
8281
UnusedVariableSniff::class,
83-
CodeAnalyzerSniff::class,
8482
SwitchDeclarationSniff::class,
8583
LanguageConstructSpacingSniff::class,
8684
UselessVariableSniff::class,

src/Domain/Metrics/Code/Functions.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use NunoMaduro\PhpInsights\Domain\Contracts\HasPercentage;
1111
use NunoMaduro\PhpInsights\Domain\Contracts\HasValue;
1212
use NunoMaduro\PhpInsights\Domain\Insights\ForbiddenDefineFunctions;
13-
use PHP_CodeSniffer\Standards\Generic\Sniffs\Functions\CallTimePassByReferenceSniff;
1413
use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\DeprecatedFunctionsSniff;
1514
use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\ForbiddenFunctionsSniff;
1615
use PHP_CodeSniffer\Standards\PSR12\Sniffs\Functions\NullableTypeDeclarationSniff;
@@ -46,7 +45,6 @@ public function getInsights(): array
4645
return [
4746
UnusedInheritedVariablePassedToClosureSniff::class,
4847
UnusedParameterSniff::class,
49-
CallTimePassByReferenceSniff::class,
5048
DeprecatedFunctionsSniff::class,
5149
NullableTypeDeclarationSniff::class,
5250
StaticClosureSniff::class,

src/Domain/Metrics/Style/Style.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use PHP_CodeSniffer\Standards\Generic\Sniffs\WhiteSpace\ArbitraryParenthesesSpacingSniff;
2727
use PHP_CodeSniffer\Standards\Generic\Sniffs\WhiteSpace\DisallowTabIndentSniff;
2828
use PHP_CodeSniffer\Standards\Generic\Sniffs\WhiteSpace\IncrementDecrementSpacingSniff;
29+
use PHP_CodeSniffer\Standards\Generic\Sniffs\WhiteSpace\LanguageConstructSpacingSniff;
2930
use PHP_CodeSniffer\Standards\PEAR\Sniffs\WhiteSpace\ObjectOperatorIndentSniff;
3031
use PHP_CodeSniffer\Standards\PEAR\Sniffs\WhiteSpace\ScopeClosingBraceSniff;
3132
use PHP_CodeSniffer\Standards\PSR1\Sniffs\Files\SideEffectsSniff;
@@ -36,7 +37,6 @@
3637
use PHP_CodeSniffer\Standards\PSR2\Sniffs\Files\ClosingTagSniff;
3738
use PHP_CodeSniffer\Standards\PSR2\Sniffs\Files\EndFileNewlineSniff;
3839
use PHP_CodeSniffer\Standards\PSR2\Sniffs\Methods\FunctionClosingBraceSniff;
39-
use PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\LanguageConstructSpacingSniff;
4040
use PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\SuperfluousWhitespaceSniff;
4141
use PhpCsFixer\Fixer\ArrayNotation\NoTrailingCommaInSinglelineArrayFixer;
4242
use PhpCsFixer\Fixer\ArrayNotation\NoWhitespaceBeforeCommaInArrayFixer;

src/Domain/Sniffs/ForbiddenSetterSniff.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,10 @@ public function register(): array
2828
return [T_FUNCTION];
2929
}
3030

31-
/**
32-
* Runs the sniff on a file.
33-
*
34-
* @param int $position
35-
*
36-
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
37-
*/
38-
public function process(File $file, $position): void
31+
public function process(File $file, int $position): void
3932
{
4033
$methodName = $file->getDeclarationName($position);
41-
if ($methodName === null) {
34+
if ($methodName === '') {
4235
return;
4336
}
4437

@@ -52,7 +45,7 @@ public function process(File $file, $position): void
5245
return;
5346
}
5447

55-
$file->addError(self::ERROR_MESSAGE, $position, self::class);
48+
$file->addError(self::ERROR_MESSAGE, $position, 'PhpInsights.Sniffs.ForbiddenSetter');
5649
}
5750

5851
/**

tests/Feature/Fix/Fixtures/ParamTypeHint.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@ final class ParamTypeHint
99
{
1010
/**
1111
* Do some calculation
12-
* @param int $a
13-
* @param int $b
14-
*
15-
* @return int
1612
*/
17-
public function sum($a, $b)
13+
public function sum(int $a, int $b): int
1814
{
1915
return $a + $b;
2016
}

tests/Feature/Fix/Fixtures/UnorderedUse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
declare(strict_types=1);
44

5-
use NunoMaduro\PhpInsights\Domain\Analyser;
65
use NunoMaduro\PhpInsights\Application\ConfigResolver;
6+
use NunoMaduro\PhpInsights\Domain\Analyser;
77

88
/**
99
* This test class is for testing if fix from CSFixer is correctly applied

tests/TestCase.php

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,32 +130,39 @@ final public static function prepareFixtureWithSniff(
130130
string $fixtureFile,
131131
array $properties = []
132132
): LocalFile {
133-
$sniffs = [self::getFilePathFromClass($sniffClassName)];
134-
135133
$config = new Config();
136134
$config->standards = [];
137135

138-
$ruleName = str_replace(
139-
'Sniff',
140-
'',
141-
class_basename($sniffClassName)
142-
);
143-
144136
/** @var Ruleset $ruleset */
145137
$ruleset = (new ReflectionClass(Ruleset::class))
146138
->newInstanceWithoutConstructor();
147139

148140
$msgCacheProperty = (new ReflectionClass(Ruleset::class))->getProperty('msgCache');
149141
$msgCacheProperty->setValue($ruleset, new MessageCollector());
150142

151-
$ruleset->ruleset = [
152-
"PhpInsights.Sniffs.{$ruleName}" => [
153-
'properties' => $properties,
154-
],
155-
];
143+
// Manually register the sniff to bypass PHPCS naming convention validation
144+
// which rejects sniffs outside the Standard\Sniffs\Category\SniffName namespace
145+
$sniffObject = new $sniffClassName();
146+
147+
foreach ($properties as $name => $value) {
148+
$sniffObject->{$name} = $value;
149+
}
150+
151+
$sniffCode = 'PhpInsights.Sniffs.' . str_replace('Sniff', '', class_basename($sniffClassName));
156152

157-
$ruleset->registerSniffs($sniffs, [], []);
158-
$ruleset->populateTokenListeners();
153+
$ruleset->sniffs[$sniffClassName] = $sniffObject;
154+
$ruleset->sniffCodes[$sniffCode] = $sniffClassName;
155+
$ruleset->tokenListeners = [];
156+
157+
foreach ($sniffObject->register() as $token) {
158+
$ruleset->tokenListeners[$token][$sniffClassName] = [
159+
'class' => $sniffClassName,
160+
'source' => $sniffCode,
161+
'tokenizers' => ['PHP'],
162+
'ignore' => [],
163+
'include' => [],
164+
];
165+
}
159166

160167
return new LocalFile($fixtureFile, $ruleset, $config);
161168
}

0 commit comments

Comments
 (0)