Skip to content

Commit 53e6372

Browse files
committed
Update ruleset for modern (PHP 8+) code
1 parent 93dad05 commit 53e6372

12 files changed

+53
-47
lines changed

MO4/Sniffs/Arrays/ArrayDoubleArrowAlignmentSniff.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ public function register(): array
6868
* @param int $stackPtr The position of the current token in
6969
* the stack passed in $tokens.
7070
*
71-
* @return void
7271
*/
7372
#[\Override]
7473
public function process(File $phpcsFile, $stackPtr): void

MO4/Sniffs/Arrays/MultiLineArraySniff.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public function register(): array
6565
* @param int $stackPtr The position of the current token in
6666
* the stack passed in $tokens.
6767
*
68-
* @return void
6968
*/
7069
#[\Override]
7170
public function process(File $phpcsFile, $stackPtr): void

MO4/Sniffs/Commenting/PropertyCommentSniff.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public function __construct()
7373
* opened the scope that this test is
7474
* listening for.
7575
*
76-
* @return void
7776
*
7877
* @throws RuntimeException
7978
*/
@@ -244,7 +243,6 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
244243
* @param int $stackPtr The position in the stack where this
245244
* token was found.
246245
*
247-
* @return void
248246
*/
249247
#[\Override]
250248
protected function processTokenOutsideScope(File $phpcsFile, $stackPtr): void

MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ class AlphabeticalUseStatementsSniff extends UseDeclarationSniff
8787
* @param int $stackPtr The position of the current token in
8888
* the stack passed in $tokens.
8989
*
90-
* @return void
9190
*/
9291
#[\Override]
9392
public function process(File $phpcsFile, $stackPtr): void
@@ -172,7 +171,7 @@ public function process(File $phpcsFile, $stackPtr): void
172171
* @param File $phpcsFile PHP CS File
173172
* @param int $stackPtr pointer
174173
*
175-
* @return array|false
174+
* @return array{startPtr: int, content: string}|false
176175
*/
177176
private function getUseImport(File $phpcsFile, int $stackPtr)
178177
{
@@ -208,7 +207,6 @@ private function getUseImport(File $phpcsFile, int $stackPtr)
208207
* @param File $phpcsFile PHP CS File
209208
* @param int $stackPtr pointer
210209
*
211-
* @return string
212210
*/
213211
private function getUseStatementAsString(File $phpcsFile, int $stackPtr): string
214212
{
@@ -231,7 +229,6 @@ private function getUseStatementAsString(File $phpcsFile, int $stackPtr): string
231229
* @param File $phpcsFile PHP CS File
232230
* @param int $stackPtr pointer
233231
*
234-
* @return bool
235232
*/
236233
private function checkIsNonImportUse(File $phpcsFile, int $stackPtr): bool
237234
{
@@ -265,7 +262,6 @@ private function checkIsNonImportUse(File $phpcsFile, int $stackPtr): bool
265262
* @param File $phpcsFile PHP CS file
266263
* @param int $stackPtr pointer
267264
*
268-
* @return void
269265
*/
270266
private function fixerClearLine(File $phpcsFile, int $stackPtr): void
271267
{
@@ -289,7 +285,6 @@ private function fixerClearLine(File $phpcsFile, int $stackPtr): void
289285
* @param int $stackPtr pointer
290286
* @param string $import import string requiring new position
291287
*
292-
* @return int
293288
*/
294289
private function findNewDestination(File $phpcsFile, int $stackPtr, string $import): int
295290
{
@@ -332,7 +327,6 @@ private function findNewDestination(File $phpcsFile, int $stackPtr, string $impo
332327
* @param string $a first namespace string
333328
* @param string $b second namespace string
334329
*
335-
* @return int
336330
*/
337331
private function compareString(string $a, string $b): int
338332
{
@@ -357,7 +351,6 @@ private function compareString(string $a, string $b): int
357351
* @param string $a first namespace string
358352
* @param string $b second namespace string
359353
*
360-
* @return int
361354
*/
362355
private function dictionaryCompare(string $a, string $b): int
363356
{

MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ public function register(): array
7474
* file's token stack where the token
7575
* was found.
7676
*
77-
* @return void
7877
*
7978
* @throws RuntimeException
8079
*/
@@ -212,7 +211,7 @@ public function process(File $phpcsFile, $stackPtr): void
212211
* @param int $start start pointer
213212
* @param int $end end pointer
214213
*
215-
* @return array
214+
* @return array<string, string>
216215
*/
217216
protected function getUseStatements(File $phpcsFile, int $start, int $end): array
218217
{
@@ -281,7 +280,6 @@ protected function getUseStatements(File $phpcsFile, int $start, int $end): arra
281280
* @param int $start start pointer
282281
* @param int $end end pointer
283282
*
284-
* @return string
285283
*/
286284
protected function getNamespace(File $phpcsFile, int $start, int $end): string
287285
{
@@ -315,7 +313,6 @@ protected function getNamespace(File $phpcsFile, int $start, int $end): string
315313
*
316314
* @param string $className class name
317315
*
318-
* @return string
319316
*/
320317
private function getFullyQualifiedClassName(string $className): string
321318
{
@@ -325,15 +322,14 @@ private function getFullyQualifiedClassName(string $className): string
325322
/**
326323
* Check if short hand is possible.
327324
*
328-
* @param File $phpcsFile PHP CS File
329-
* @param array $useStatements array with class use statements
330-
* @param string $className class name
331-
* @param string $namespace name space
332-
* @param int $startPtr start token pointer
333-
* @param int $endPtr end token pointer
334-
* @param bool $isDocBlock true if fixing doc block
325+
* @param File $phpcsFile PHP CS File
326+
* @param array<string, string> $useStatements array with class use statements
327+
* @param string $className class name
328+
* @param string $namespace name space
329+
* @param int $startPtr start token pointer
330+
* @param int $endPtr end token pointer
331+
* @param bool $isDocBlock true if fixing doc block
335332
*
336-
* @return void
337333
*/
338334
private function checkShorthandPossible(File $phpcsFile, array $useStatements, string $className, string $namespace, int $startPtr, int $endPtr, bool $isDocBlock = false): void
339335
{

MO4/Sniffs/Strings/VariableInDoubleQuotedStringSniff.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ public function register(): array
5959
* file's token stack where the token
6060
* was found.
6161
*
62-
* @return void
6362
*/
6463
#[\Override]
6564
public function process(File $phpcsFile, $stackPtr): void
@@ -130,7 +129,6 @@ public function process(File $phpcsFile, $stackPtr): void
130129
* @param int $pos position
131130
* @param string $var variable
132131
*
133-
* @return string
134132
*/
135133
private function surroundVariableWithBraces(string $content, int $pos, string $var): string
136134
{
@@ -147,7 +145,6 @@ private function surroundVariableWithBraces(string $content, int $pos, string $v
147145
* @param string $correctVariable correct variable
148146
* @param File $phpCsFile PHP_CodeSniffer File object
149147
*
150-
* @return void
151148
*/
152149
private function fixPhpCsFile(int $stackPtr, string $correctVariable, File $phpCsFile): void
153150
{

MO4/Sniffs/WhiteSpace/ConstantSpacingSniff.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ public function register(): array
6363
* @param int $stackPtr The position of the current token in
6464
* the stack passed in $tokens.
6565
*
66-
* @return void
6766
*/
6867
#[\Override]
6968
public function process(File $phpcsFile, $stackPtr): void

MO4/Sniffs/WhiteSpace/MultipleEmptyLinesSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function process(File $phpcsFile, $stackPtr)
7070
$next = $stackPtr + 1;
7171

7272
while (isset($tokens[$next + 1]) &&
73-
$tokens[$next]['code'] === T_WHITESPACE &&
73+
T_WHITESPACE === $tokens[$next]['code'] &&
7474
$tokens[$next]['line'] !== $tokens[$next + 1]['line']
7575
) {
7676
$next++;

MO4/Tests/AbstractMo4SniffUnitTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ protected function getWarningList(string $testFile = ''): array
9898
* The key of the array should represent the line number and the value
9999
* should represent the number of warnings that should occur on that line.
100100
*
101-
* @param string $testFile test file
102-
* @param array $list record for test file
101+
* @param string $testFile test file
102+
* @param array<array<int>> $list record for test file
103103
*
104104
* @return array<int, int>
105105
*

MO4/ruleset.xml

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
<!-- exclude sniffs that are disabled on purpose by this standard -->
1515
<exclude name="Symfony.Commenting.ClassComment.Missing"/>
1616
<exclude name="Symfony.Commenting.License"/>
17+
<!-- SlevomatCodingStandard.ControlStructures.RequireYodaComparison supports autofixing -->
18+
<exclude name="Symfony.ControlStructure.YodaConditions.Invalid"/>
19+
<!-- disable forcing of function type hints as we're requiring native types -->
20+
<exclude name="Symfony.Commenting.FunctionComment"/>
1721
</rule>
1822

1923
<!-- Also include PSR12 -->
@@ -131,8 +135,16 @@
131135
</rule>
132136
<!-- Require usage of ::class instead of __CLASS__, get_class(), get_class($this), get_called_class() and get_parent_class() -->
133137
<rule ref="SlevomatCodingStandard.Classes.ModernClassNameReference"/>
138+
<!-- Require use of constructor property promotion -->
139+
<rule ref="SlevomatCodingStandard.Classes.RequireConstructorPropertyPromotion"/>
140+
<!-- Forbid static:: in final class -->
141+
<rule ref="SlevomatCodingStandard.Classes.UselessLateStaticBinding"/>
142+
<!-- Forbid empty comments-->
143+
<rule ref="SlevomatCodingStandard.Commenting.EmptyComment"/>
134144
<!-- report invalid format of inline phpDocs with @var -->
135145
<rule ref="SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration"/>
146+
<!-- Forbid useless function doc comments -->
147+
<rule ref="SlevomatCodingStandard.Commenting.UselessFunctionDocComment"/>
136148
<!-- Forbid useless @inheritDoc comments -->
137149
<rule ref="SlevomatCodingStandard.Commenting.UselessInheritDocComment"/>
138150
<!-- Forbid assignments in conditions -->
@@ -159,13 +171,21 @@
159171
<rule ref="SlevomatCodingStandard.ControlStructures.NewWithParentheses"/>
160172
<!-- Require usage of null coalesce operator when possible -->
161173
<rule ref="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator"/>
174+
<!-- Require usage of null coalesce equal operator when possible -->
175+
<rule ref="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceEqualOperator"/>
176+
<!-- Require usage of null safe operator when possible -->
177+
<rule ref="SlevomatCodingStandard.ControlStructures.RequireNullSafeObjectOperator"/>
162178
<!-- Require usage of ternary operator when possible -->
163179
<rule ref="SlevomatCodingStandard.ControlStructures.RequireTernaryOperator">
164180
<!-- But multiline is useful for readability -->
165181
<properties>
166182
<property name="ignoreMultiLine" value="true"/>
167183
</properties>
168184
</rule>
185+
<!-- Require Yoda comparisons -->
186+
<rule ref="SlevomatCodingStandard.ControlStructures.RequireYodaComparison"/>
187+
<!-- Formatting of arrow functions -->
188+
<rule ref="SlevomatCodingStandard.Functions.ArrowFunctionDeclaration"/>
169189
<!-- Enforce consistent spacing of named arguments -->
170190
<rule ref="SlevomatCodingStandard.Functions.NamedArgumentSpacing"/>
171191
<!-- Require static closures for microoptimization -->
@@ -229,19 +249,15 @@
229249
<!-- Require one space after and no space before colon in return types -->
230250
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing"/>
231251
<!-- Require parameter type hints to be declared -->
232-
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint">
233-
<properties>
234-
<property name="enableMixedTypeHint" value="false"/>
235-
<property name="enableUnionTypeHint" value="false"/>
236-
<property name="enableIntersectionTypeHint" value="false"/>
237-
</properties>
238-
</rule>
252+
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint"/>
239253
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint.UselessAnnotation">
240-
<severity>0</severity>
254+
<severity>9</severity>
241255
</rule>
242256
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification">
243-
<severity>0</severity>
257+
<severity>9</severity>
244258
</rule>
259+
<!-- check spacing of parameter type hints -->
260+
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHintSpacing"/>
245261
<!-- Require return type hints to be declared -->
246262
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint">
247263
<properties>
@@ -251,10 +267,21 @@
251267
</properties>
252268
</rule>
253269
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint.UselessAnnotation">
254-
<severity>0</severity>
270+
<severity>9</severity>
255271
</rule>
256272
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification">
257-
<severity>0</severity>
273+
<severity>9</severity>
274+
</rule>
275+
<!-- check spacing of return type hints -->
276+
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing"/>
277+
<!-- DNF (union / intersection) type hint formatting -->
278+
<rule ref="SlevomatCodingStandard.TypeHints.DNFTypeHintFormat">
279+
<properties>
280+
<property name="withSpacesAroundOperators" value="no"/>
281+
<property name="withSpacesInsideParentheses" value="no"/>
282+
<property name="shortNullable" value="yes"/>
283+
<property name="nullPosition" value="last"/>
284+
</properties>
258285
</rule>
259286
<!-- Forbid useless @var for constants -->
260287
<rule ref="SlevomatCodingStandard.TypeHints.UselessConstantTypeHint"/>

0 commit comments

Comments
 (0)