Skip to content

Commit 8047c85

Browse files
committed
feat: add base ruleset
0 parents  commit 8047c85

File tree

2 files changed

+324
-0
lines changed

2 files changed

+324
-0
lines changed

GPA-Lab/ruleset.xml

Lines changed: 309 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,309 @@
1+
<?xml version="1.0"?>
2+
<ruleset
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
name="GPA Lab Standard"
5+
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd"
6+
>
7+
8+
<description>
9+
Base PHP rules. Largely cribbed from the WordPress Coding Standards.
10+
</description>
11+
12+
<arg name="colors"/>
13+
<arg name="extensions" value="php" />
14+
<arg value="s"/>
15+
16+
<!-- Default tab width for indentation fixes and such. -->
17+
<arg name="tab-width" value="2"/>
18+
19+
<!-- Covers rule: Use single and double quotes when appropriate.
20+
If you're not evaluating anything in the string, use single quotes. -->
21+
<rule ref="Squiz.Strings.DoubleQuoteUsage.NotRequired"/>
22+
23+
<!-- Covers rule: Your indentation should always reflect logical structure. -->
24+
<rule ref="Generic.WhiteSpace.ScopeIndent">
25+
<properties>
26+
<property name="exact" value="false"/>
27+
<property name="indent" value="2"/>
28+
<property name="tabIndent" value="false"/>
29+
<property name="ignoreIndentationTokens" type="array">
30+
<element value="T_HEREDOC"/>
31+
<element value="T_NOWDOC"/>
32+
<element value="T_INLINE_HTML"/>
33+
</property>
34+
</properties>
35+
</rule>
36+
37+
<!-- Covers rule: For switch structures case should indent one tab from the
38+
switch statement and break one tab from the case statement. -->
39+
<rule ref="PSR2.ControlStructures.SwitchDeclaration"/>
40+
41+
<!-- Prevent duplicate messages for the same issue. Covered by other sniffs. -->
42+
<rule ref="PSR2.ControlStructures.SwitchDeclaration.NotLower">
43+
<severity>0</severity>
44+
</rule>
45+
46+
<rule ref="PSR2.ControlStructures.SwitchDeclaration.BreakNotNewLine">
47+
<severity>0</severity>
48+
</rule>
49+
50+
<rule ref="PSR2.ControlStructures.SwitchDeclaration.BodyOnNextLine">
51+
<severity>0</severity>
52+
</rule>
53+
54+
<!-- Covers rule: ... while spaces can be used mid-line for alignment. -->
55+
<rule ref="Universal.WhiteSpace.DisallowInlineTabs"/>
56+
57+
<!-- Implied through the examples: align the assignment operator in a block of assignments. -->
58+
<rule ref="Generic.Formatting.MultipleStatementAlignment">
59+
<properties>
60+
<property name="maxPadding" value="40"/>
61+
</properties>
62+
</rule>
63+
64+
<!-- Covers rule: Braces shall be used for all blocks. -->
65+
<rule ref="Squiz.ControlStructures.ControlSignature"/>
66+
67+
<!-- Covers rule: Braces should always be used, even when they are not required. -->
68+
<rule ref="Generic.ControlStructures.InlineControlStructure"/>
69+
70+
<!-- Covers rule: Arrays must be declared using long array syntax. -->
71+
<rule ref="Universal.Arrays.DisallowShortArraySyntax"/>
72+
73+
<!-- Covers rule: ... use elseif for conditionals. -->
74+
<rule ref="PSR2.ControlStructures.ElseIfDeclaration"/>
75+
76+
<!-- Covers rule: When embedding multi-line PHP snippets within a HTML block, the
77+
PHP open and close tags must be on a line by themselves. -->
78+
<rule ref="Squiz.PHP.EmbeddedPhp"/>
79+
80+
<rule ref="Squiz.PHP.EmbeddedPhp.SpacingBefore">
81+
<severity>0</severity>
82+
</rule>
83+
84+
<rule ref="Squiz.PHP.EmbeddedPhp.Indent">
85+
<severity>0</severity>
86+
</rule>
87+
88+
<rule ref="Squiz.PHP.EmbeddedPhp.OpenTagIndent">
89+
<severity>0</severity>
90+
</rule>
91+
92+
<rule ref="Squiz.PHP.EmbeddedPhp.SpacingAfter">
93+
<severity>0</severity>
94+
</rule>
95+
96+
<!-- Covers rule: Never use shorthand PHP start tags. Always use full PHP tags. -->
97+
<rule ref="Generic.PHP.DisallowShortOpenTag"/>
98+
<rule ref="Generic.PHP.DisallowAlternativePHPTags"/>
99+
100+
<!-- Covers rule: Remove trailing whitespace at the end of each line of code. -->
101+
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace"/>
102+
103+
<!-- Covers rule: Omitting the closing PHP tag at the end of a file is preferred. -->
104+
<rule ref="PSR2.Files.ClosingTag"/>
105+
106+
<!-- Covers rule: Always put spaces after commas, and on both sides of logical,
107+
comparison, string and assignment operators. -->
108+
<rule ref="Squiz.Strings.ConcatenationSpacing">
109+
<properties>
110+
<property name="spacing" value="1"/>
111+
<property name="ignoreNewlines" value="true"/>
112+
</properties>
113+
</rule>
114+
115+
<!-- Covers rule: Define a function like so: function my_function( $param1 = 'foo', $param2 = 'bar' ) { -->
116+
<rule ref="Generic.Functions.OpeningFunctionBraceKernighanRitchie">
117+
<properties>
118+
<property name="checkClosures" value="true"/>
119+
</properties>
120+
</rule>
121+
<rule ref="Squiz.Functions.FunctionDeclarationArgumentSpacing">
122+
<properties>
123+
<property name="equalsSpacing" value="1"/>
124+
<property name="requiredSpacesAfterOpen" value="1"/>
125+
<property name="requiredSpacesBeforeClose" value="1"/>
126+
</properties>
127+
</rule>
128+
<rule ref="Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingBeforeClose">
129+
<severity>0</severity>
130+
</rule>
131+
132+
133+
<!-- Covers rule: Call a function, like so: my_function( $param1, func_param( $param2 ) ); -->
134+
<rule ref="PEAR.Functions.FunctionCallSignature">
135+
<properties>
136+
<property name="requiredSpacesAfterOpen" value="1"/>
137+
<property name="requiredSpacesBeforeClose" value="1"/>
138+
<property name="indent" value="2"/>
139+
140+
<!-- ... and for multi-line function calls, there should only be one parameter per line. -->
141+
<property name="allowMultipleArguments" value="false"/>
142+
</properties>
143+
</rule>
144+
145+
<rule ref="Generic.Functions.FunctionCallArgumentSpacing"/>
146+
147+
<!-- Rule: Perform logical comparisons, like so: if ( ! $foo ) { -->
148+
149+
<!-- Covers rule: Type casts must be lowercase. Always prefer the short form
150+
of type casts, (int) instead of (integer) and (bool) rather than (boolean).
151+
For float casts use (float). -->
152+
<rule ref="Generic.Formatting.SpaceAfterCast"/>
153+
<rule ref="Squiz.WhiteSpace.CastSpacing"/>
154+
<rule ref="PSR12.Keywords.ShortFormTypeKeywords"/>
155+
<!-- N.B.: This sniff also checks the case of (parameter/return) type declarations, not just type casts. -->
156+
<rule ref="Generic.PHP.LowerCaseType"/>
157+
158+
<!-- Covers rule: Unless otherwise specified, parentheses should have spaces inside of them. -->
159+
<rule ref="Generic.WhiteSpace.ArbitraryParenthesesSpacing">
160+
<properties>
161+
<property name="spacing" value="1"/>
162+
<property name="ignoreNewlines" value="true"/>
163+
</properties>
164+
</rule>
165+
166+
<!-- Covers rule: Class names should use capitalized words separated by underscores. -->
167+
<rule ref="PEAR.NamingConventions.ValidClassName"/>
168+
169+
<!-- Covers rule: Constants should be in all upper-case with underscores separating words. -->
170+
<rule ref="Generic.NamingConventions.UpperCaseConstantName"/>
171+
172+
<!-- Rule: The short ternary operator must not be used. -->
173+
<rule ref="Universal.Operators.DisallowShortTernary"/>
174+
175+
<!-- Rule: In general, readability is more important than cleverness or brevity. -->
176+
<rule ref="Squiz.PHP.DisallowMultipleAssignments"/>
177+
<rule ref="Generic.Formatting.DisallowMultipleStatements"/>
178+
179+
<!-- Rule: Unless absolutely necessary, loose comparisons should not be used,
180+
as their behaviour can be misleading. -->
181+
<rule phpcs-only="true" ref="Universal.Operators.StrictComparisons">
182+
<type>warning</type>
183+
</rule>
184+
185+
<!-- Rule: The goto statement must never be used. -->
186+
<rule ref="Generic.PHP.DiscourageGoto">
187+
<type>error</type>
188+
<message>The "goto" language construct should not be used.</message>
189+
</rule>
190+
191+
<!-- Rule: The eval() construct is very dangerous, and is impossible to secure. ... these must not be used. -->
192+
<rule ref="Squiz.PHP.Eval.Discouraged">
193+
<type>error</type>
194+
<message>eval() is a security risk so not allowed.</message>
195+
</rule>
196+
197+
<!-- Important to prevent issues with content being sent before headers. -->
198+
<rule ref="Generic.Files.ByteOrderMark"/>
199+
200+
<!-- All line endings should be \n. -->
201+
<rule ref="Generic.Files.LineEndings">
202+
<properties>
203+
<property name="eolChar" value="\n"/>
204+
</properties>
205+
</rule>
206+
207+
<!-- All files should end with a new line. -->
208+
<rule ref="Generic.Files.EndFileNewline"/>
209+
210+
<!-- No whitespace should come before semicolons. -->
211+
<rule ref="Squiz.WhiteSpace.SemicolonSpacing"/>
212+
213+
<!-- There should be no empty statements, i.e. lone semi-colons or open/close tags without content. -->
214+
<rule ref="Generic.CodeAnalysis.EmptyPHPStatement"/>
215+
216+
<!-- Lowercase PHP constants, like true, false and null. -->
217+
<rule ref="Generic.PHP.LowerCaseConstant"/>
218+
219+
<!-- Lowercase PHP keywords, like class, function and case. -->
220+
<rule ref="Generic.PHP.LowerCaseKeyword"/>
221+
222+
<!-- Class opening braces should be on the same line as the statement. -->
223+
<rule ref="Generic.Classes.OpeningBraceSameLine"/>
224+
225+
<!-- Object operators should not have whitespace around them unless they are multi-line. -->
226+
<rule ref="Squiz.WhiteSpace.ObjectOperatorSpacing">
227+
<properties>
228+
<property name="ignoreNewlines" value="true"/>
229+
</properties>
230+
</rule>
231+
232+
<!-- References to self in a class should be lower-case and not have extraneous spaces,
233+
per implicit conventions in the core codebase; the NotUsed code refers to using the
234+
fully-qualified class name instead of self, for which there are instances in core. -->
235+
<rule ref="Squiz.Classes.SelfMemberReference"/>
236+
<rule ref="Squiz.Classes.SelfMemberReference.NotUsed">
237+
<severity>0</severity>
238+
</rule>
239+
240+
<!-- Encourage having only one class/interface/trait per file.
241+
Moved from Extra to Core after discussion on Slack. -->
242+
<rule ref="Generic.Files.OneObjectStructurePerFile">
243+
<message>Best practices: Declare only one class/interface/trait in a file.</message>
244+
</rule>
245+
246+
<!-- Generic PHP best practices. -->
247+
<rule ref="Generic.PHP.DeprecatedFunctions"/>
248+
<rule ref="Generic.PHP.ForbiddenFunctions"/>
249+
<rule ref="Generic.Functions.CallTimePassByReference"/>
250+
<rule ref="Generic.CodeAnalysis.EmptyStatement"/>
251+
<rule ref="Generic.CodeAnalysis.ForLoopShouldBeWhileLoop"/>
252+
<rule ref="Generic.CodeAnalysis.ForLoopWithTestFunctionCall"/>
253+
<rule ref="Generic.CodeAnalysis.JumbledIncrementer"/>
254+
<rule ref="Generic.CodeAnalysis.UnconditionalIfStatement"/>
255+
<rule ref="Generic.CodeAnalysis.UnnecessaryFinalModifier"/>
256+
<rule ref="Generic.CodeAnalysis.UselessOverridingMethod"/>
257+
<rule ref="Generic.Classes.DuplicateClassName"/>
258+
<rule ref="Generic.Strings.UnnecessaryStringConcat">
259+
<properties>
260+
<property name="allowMultiline" value="true"/>
261+
</properties>
262+
</rule>
263+
<rule ref="Squiz.PHP.NonExecutableCode"/>
264+
<rule ref="Squiz.Operators.IncrementDecrementUsage"/>
265+
<rule ref="Squiz.Operators.ValidLogicalOperators"/>
266+
<rule ref="Squiz.Functions.FunctionDuplicateArgument"/>
267+
<rule ref="Squiz.PHP.DisallowSizeFunctionsInLoops"/>
268+
<rule ref="PEAR.Files.IncludingFile.BracketsNotRequired">
269+
<type>warning</type>
270+
</rule>
271+
<rule ref="PEAR.Files.IncludingFile.UseRequire">
272+
<type>warning</type>
273+
</rule>
274+
<rule ref="PEAR.Files.IncludingFile.UseRequireOnce">
275+
<type>warning</type>
276+
</rule>
277+
278+
<!-- Check correct spacing of language constructs. This also ensures that the
279+
above rule for not using brackets with require is fixed correctly. -->
280+
<rule ref="Generic.WhiteSpace.LanguageConstructSpacing"/>
281+
282+
<!-- Verify modifier keywords for declared methods and properties in classes. -->
283+
<rule ref="Squiz.Scope.MethodScope"/>
284+
<rule ref="PSR2.Classes.PropertyDeclaration"/>
285+
<rule ref="Squiz.WhiteSpace.ScopeKeywordSpacing"/>
286+
<rule ref="PSR2.Methods.MethodDeclaration"/>
287+
288+
<!-- Warn against using fully-qualified class names instead of the self keyword. -->
289+
<rule ref="Squiz.Classes.SelfMemberReference.NotUsed">
290+
<!-- Restore default severity of 5 which WordPress-Core sets to 0. -->
291+
<severity>5</severity>
292+
</rule>
293+
294+
<!-- Discourage use of the backtick operator (execution of shell commands). -->
295+
<rule ref="Generic.PHP.BacktickOperator"/>
296+
297+
<!-- Check for PHP Parse errors.-->
298+
<rule ref="Generic.PHP.Syntax"/>
299+
300+
<!-- Commented out code should not be committed. -->
301+
<rule ref="Squiz.PHP.CommentedOutCode">
302+
<properties>
303+
<property name="maxPercentage" value="40"/>
304+
</properties>
305+
</rule>
306+
307+
<!-- Check for single blank line after namespace declaration. -->
308+
<rule ref="PSR2.Namespaces.NamespaceDeclaration"/>
309+
</ruleset>

composer.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "gpalab/coding-standards",
3+
"type": "phpcodesniffer-standard",
4+
"description": "PHP_CodeSniffer rules (sniffs) to enforce GPA Lab coding conventions",
5+
"keywords": [
6+
"phpcs",
7+
"standards",
8+
"gpalab"
9+
],
10+
"require": {
11+
"php": "^7.2 || ^8.0",
12+
"squizlabs/php_codesniffer": "^3.6.2",
13+
"phpcsstandards/phpcsextra": "^1.0"
14+
}
15+
}

0 commit comments

Comments
 (0)