Skip to content

Commit 06f27c1

Browse files
authored
Merge pull request #63 from PHPCSStandards/feature/add-phpcsextra
Add the PHPCSExtra package and start using a pletora of sniffs from it
2 parents 3f0254b + 1618418 commit 06f27c1

File tree

2 files changed

+146
-1
lines changed

2 files changed

+146
-1
lines changed

PHPCSDev/ruleset.xml

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@
3939
</rule>
4040

4141

42+
<!--
43+
####################################################################
44+
PHP: Modernize a codebase when possible.
45+
####################################################################
46+
-->
47+
48+
<rule ref="Modernize.FunctionCalls.Dirname.FileConstant"/>
49+
50+
4251
<!--
4352
####################################################################
4453
Code style: Check style for compliance with PSR12.
@@ -81,20 +90,43 @@
8190
</properties>
8291
</rule>
8392

93+
<!-- Prevent PHP 8 named parameters confusion. -->
94+
<rule ref="Universal.NamingConventions.NoReservedKeywordParameterNames"/>
95+
8496

8597
<!--
8698
####################################################################
8799
Code style: Various other additions.
100+
Some of these rules will probably be enforced via PERCS once it is available, but we already want to use them.
88101
####################################################################
89102
-->
90103

104+
<!-- Enforce lowercase PHP tags. -->
105+
<rule ref="Universal.PHP.LowercasePHPTag"/>
106+
107+
<!-- Enforce consistency in import use statements. -->
108+
<rule ref="Universal.UseStatements.NoLeadingBackslash"/>
109+
<rule ref="Universal.UseStatements.KeywordSpacing"/>
110+
<rule ref="Universal.UseStatements.LowercaseFunctionConst"/>
111+
112+
<!-- Consistent modifier keyword order. PSR12 currently only enforces this for property declarations. -->
113+
<rule ref="Universal.Classes.ModifierKeywordOrder"/>
114+
<rule ref="Universal.Constants.ModifierKeywordOrder"/>
115+
91116
<!-- PSR12 doesn't enforce consistency in where boolean operators are placed. We do. -->
92117
<rule ref="PSR12.ControlStructures.BooleanOperatorPlacement">
93118
<properties>
94119
<property name="allowOnly" value="first"/>
95120
</properties>
96121
</rule>
97122

123+
<!-- Same thing, but then for multi-line concatenation in statements. -->
124+
<rule ref="Universal.Operators.ConcatPosition">
125+
<properties>
126+
<property name="allowOnly" value="start"/>
127+
</properties>
128+
</rule>
129+
98130
<!-- PSR12 appears to ignore blank lines for superfluous whitespace and in several other places. Let's fix that. -->
99131
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">
100132
<properties>
@@ -129,6 +161,23 @@
129161
<!-- No spacing inside heredoc/nowdocs identifiers. -->
130162
<rule ref="Generic.WhiteSpace.HereNowdocIdentifierSpacing"/>
131163

164+
<!-- No spaces around type operators. -->
165+
<rule ref="Universal.Operators.TypeSeparatorSpacing"/>
166+
167+
<!-- Always use parentheses when instantiating anonymous classes. -->
168+
<rule ref="Universal.Classes.RequireAnonClassParentheses"/>
169+
170+
<!-- No space between the class keyword and the open parentheses for anonymous classes. -->
171+
<rule ref="Universal.WhiteSpace.AnonClassKeywordSpacing"/>
172+
173+
<!-- Enforce consistent spacing around commas (no space before, one space or new line after). -->
174+
<rule ref="Universal.WhiteSpace.CommaSpacing">
175+
<exclude name="Universal.WhiteSpace.CommaSpacing.TooMuchSpaceAfterCommaBeforeTrailingComment"/>
176+
</rule>
177+
178+
<!-- Disallow precision alignment (indentation). -->
179+
<rule ref="Universal.WhiteSpace.PrecisionAlignment"/>
180+
132181
<!-- Align the equal operator in assignment blocks. -->
133182
<rule ref="Generic.Formatting.MultipleStatementAlignment">
134183
<properties>
@@ -148,24 +197,57 @@
148197
<!-- Disallow Yoda conditions. -->
149198
<rule ref="Generic.ControlStructures.DisallowYodaConditions"/>
150199

200+
<!-- Disallow alternative syntax for control structures. -->
201+
<rule ref="Universal.ControlStructures.DisallowAlternativeSyntax"/>
202+
151203
<!-- Use self or static when referring to the class in use. -->
152204
<rule ref="Squiz.Classes.SelfMemberReference"/>
205+
<rule ref="Universal.CodeAnalysis.StaticInFinalClass"/>
206+
207+
<!-- Only one namespace per file. -->
208+
<rule ref="Universal.Namespaces.OneDeclarationPerFile"/>
209+
210+
<!-- Don't allow curly brace syntax for namespace declarations. -->
211+
<rule ref="Universal.Namespaces.DisallowCurlyBraceSyntax"/>
153212

154213
<!-- Only one object structure per file. -->
155214
<rule ref="Generic.Files.OneObjectStructurePerFile"/>
156215

216+
<!-- And don't mix OO declarations and function declarations in one file. -->
217+
<rule ref="Universal.Files.SeparateFunctionsFromOO"/>
218+
157219
<!-- Disallow non-strict comparisons and the use of the not operator. -->
158220
<rule ref="Squiz.Operators.ComparisonOperatorUsage">
159221
<exclude name="Squiz.Operators.ComparisonOperatorUsage.ImplicitTrue"/>
160222
</rule>
161223

224+
<!-- Enforce lowercase ::class. -->
225+
<rule ref="Universal.Constants.LowercaseClassResolutionKeyword"/>
226+
227+
<!-- PHP native magic constants should be uppercase. -->
228+
<rule ref="Universal.Constants.UppercaseMagicConstants"/>
229+
230+
<!-- Alphabetize lists of extended and implemented interfaces. -->
231+
<rule ref="Universal.OOStructures.AlphabeticExtendsImplements"/>
232+
233+
<!-- Disallow the use of fully qualified true/false/null. -->
234+
<rule ref="Universal.PHP.NoFQNTrueFalseNull"/>
235+
236+
<!-- Prevent really long lists with all (global) functions/constants used in a file. -->
237+
<rule ref="Universal.UseStatements.DisallowUseConst"/>
238+
<rule ref="Universal.UseStatements.DisallowUseFunction"/>
239+
162240

163241
<!--
164242
####################################################################
165243
Code style: Array declarations.
166244
####################################################################
167245
-->
168246

247+
<!-- This (incomplete) ruleset may cause some duplication for now, but that will be fixed once
248+
the ruleset is completed and we can remove the `Squiz.Arrays.ArrayDeclaration` sniff. -->
249+
<rule ref="NormalizedArrays"/>
250+
169251
<!-- Enforce short array syntax. -->
170252
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
171253

@@ -226,12 +308,33 @@
226308
</rule>
227309

228310

311+
<!--
312+
####################################################################
313+
PHPCS compatibility additions.
314+
####################################################################
315+
-->
316+
317+
<!-- The PHPCS autoloader does not handle sniffs which extends other sniffs well, so prevent problems by making all sniffs final. -->
318+
<rule ref="Universal.Classes.RequireFinalClass"/>
319+
320+
<!-- Enforce methods in traits to be final to prevent (accidental) overloading. -->
321+
<rule ref="Universal.FunctionDeclarations.RequireFinalMethodsInTraits"/>
322+
323+
229324
<!--
230325
####################################################################
231326
Code Analysis additions.
232327
####################################################################
233328
-->
234329

330+
<!-- Testability: long closures make for code which is more difficult to test. -->
331+
<rule ref="Universal.FunctionDeclarations.NoLongClosures">
332+
<!-- Disable the warning after 5 lines, only have the error after 8 lines. -->
333+
<properties>
334+
<property name="recommendedLines " value="1000"/>
335+
</properties>
336+
</rule>
337+
235338
<!-- Efficiency: don't use function calls within the condition of a for loop. -->
236339
<rule ref="Generic.CodeAnalysis.ForLoopWithTestFunctionCall"/>
237340

@@ -244,6 +347,9 @@
244347
<!-- Efficiency: don't unnecessarily use heredocs. -->
245348
<rule ref="Generic.Strings.UnnecessaryHeredoc"/>
246349

350+
<!-- Efficiency: simplify code when possible. -->
351+
<rule ref="Universal.CodeAnalysis.NoEchoSprintf"/>
352+
247353
<!-- Error prevention: warns when an inner loop uses the same incrementor as the outer loop. -->
248354
<rule ref="Generic.CodeAnalysis.JumbledIncrementer"/>
249355

@@ -255,6 +361,36 @@
255361
<!-- Error prevention: use parentheses when mixing boolean operators. -->
256362
<rule ref="Generic.CodeAnalysis.RequireExplicitBooleanOperatorPrecedence"/>
257363

364+
<!-- Error prevention: Don't allow mixed arrays (keyed/non-keyed). -->
365+
<rule ref="Universal.Arrays.MixedKeyedUnkeyedArray"/>
366+
367+
<!-- Error prevention: Don't allow mixed arrays (integer/string keys). -->
368+
<rule ref="Universal.Arrays.MixedArrayKeyTypes"/>
369+
370+
<!-- Error prevention: Prevent issues with duplicate arrays keys. -->
371+
<rule ref="Universal.Arrays.DuplicateArrayKey"/>
372+
373+
<!-- Error prevention: Prevent issues with precedent order. -->
374+
<rule ref="Universal.Operators.DisallowLogicalAndOr"/>
375+
376+
<!-- Error prevention: Prevent logic errors as most devs don't understand short ternary well enough. -->
377+
<rule ref="Universal.Operators.DisallowShortTernary"/>
378+
379+
<!-- Error prevention: Prevent issues when in/decremented variables get moved around and are suddenly no longer stand-alone. -->
380+
<rule ref="Universal.Operators.DisallowStandalonePostIncrementDecrement"/>
381+
382+
<!-- Error prevention: Prevent creation of constructor/destructors which will not work as expected. -->
383+
<rule ref="Universal.CodeAnalysis.ConstructorDestructorReturn"/>
384+
385+
<!-- Error prevention: Prevent unpredictable code behaviour. -->
386+
<rule ref="Universal.CodeAnalysis.ForeachUniqueAssignment"/>
387+
388+
<!-- Error prevention: Prevent incomprehensible code. -->
389+
<rule ref="Universal.CodeAnalysis.NoDoubleNegative"/>
390+
391+
<!-- Error prevention: don't allow people to do silly things. -->
392+
<rule ref="Universal.PHP.OneStatementInShortEchoTag"/>
393+
258394
<!-- Clean up: remove redundant code. -->
259395
<rule ref="Squiz.PHP.NonExecutableCode">
260396
<!-- Allow for return statement in otherwise empty function. -->
@@ -264,7 +400,16 @@
264400
<!-- Clean up: no empty statements. -->
265401
<rule ref="Generic.CodeAnalysis.EmptyPHPStatement"/>
266402

403+
<!-- Clean up: no lonely if statements. -->
404+
<rule ref="Universal.ControlStructures.DisallowLonelyIf"/>
405+
406+
<!-- Clean up: no useless aliases in import use statements. -->
407+
<rule ref="Universal.UseStatements.NoUselessAliases"/>
408+
267409
<!-- Parse error prevention: guard against merge conflict artifacts. -->
268410
<rule ref="Generic.VersionControl.GitMergeConflict"/>
269411

412+
<!-- Lower cognitive load. -->
413+
<rule ref="Universal.UseStatements.DisallowMixedGroupUse"/>
414+
270415
</ruleset>

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"php" : ">=5.4",
2525
"squizlabs/php_codesniffer" : "^3.13.3 || ^4.0.0",
2626
"phpcompatibility/php-compatibility" : "^10.0.0@dev",
27-
"dealerdirect/phpcodesniffer-composer-installer" : "^0.7 || ^1.0"
27+
"phpcsstandards/phpcsextra" : "^1.4.2"
2828
},
2929
"require-dev" : {
3030
"roave/security-advisories" : "dev-master"

0 commit comments

Comments
 (0)