Skip to content

Commit 43af260

Browse files
committed
PHPLIB-330: Add first version of our coding standard
1 parent a72cbe3 commit 43af260

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ vendor/
77
phpunit.phar
88
phpunit.xml
99
.phpunit.result.cache
10+
11+
# phpcs
12+
.phpcs-cache
13+
phpcs.xml

.phpcs/autoload.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
// Since doctrine/coding-standard requires PHP 7, we can't add it as a dependency
4+
// yet. This autoload file adds more information to the phpcs error message,
5+
// telling the user how they can fix the error presented to them by phpcs.
6+
if (! file_exists(__DIR__ . '/../vendor/doctrine/coding-standard')) {
7+
echo <<<ERRORMESSAGE
8+
==============================================================================
9+
ERROR: Doctrine coding standard is not installed. To rectify this, please run:
10+
composer require --dev doctrine/coding-standard=^6.0
11+
==============================================================================
12+
13+
14+
ERRORMESSAGE;
15+
}

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"require-dev": {
1919
"phpunit/phpunit": "^5.7.27 || ^6.4 || ^8.3",
2020
"sebastian/comparator": "^1.0 || ^2.0 || ^3.0",
21+
"squizlabs/php_codesniffer": "^3.4",
2122
"symfony/phpunit-bridge": "^4.4@dev"
2223
},
2324
"autoload": {

phpcs.xml.dist

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?xml version="1.0"?>
2+
<ruleset>
3+
<arg name="basepath" value="."/>
4+
<arg name="extensions" value="php"/>
5+
<arg name="parallel" value="80"/>
6+
<arg name="cache" value=".phpcs-cache"/>
7+
<arg name="colors" />
8+
9+
<!-- Ignore warnings, show progress of the run, and show sniff names -->
10+
<arg value="nps"/>
11+
12+
<autoload>.phpcs/autoload.php</autoload>
13+
14+
<file>.phpcs</file>
15+
<file>src</file>
16+
<file>tests</file>
17+
18+
<rule ref="Doctrine">
19+
<!-- Exclude sniffs that require newer PHP versions -->
20+
<!-- Available with PHP 7.0 -->
21+
<exclude name="SlevomatCodingStandard.TypeHints.DeclareStrictTypes" />
22+
<!-- In addition to requiring PHP 7.0, this sniff will cause a significant amount of BC breaks. Proceed with caution! -->
23+
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration" />
24+
<exclude name="SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly" />
25+
<exclude name="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator" />
26+
27+
<!-- Available with PHP 7.1 -->
28+
<exclude name="SlevomatCodingStandard.Classes.ClassConstantVisibility" />
29+
<exclude name="SlevomatCodingStandard.PHP.ShortList.LongListUsed" />
30+
<exclude name="SlevomatCodingStandard.TypeHints.NullableTypeForNullDefaultValue" />
31+
32+
<!-- No statement alignment so far -->
33+
<exclude name="Generic.Formatting.MultipleStatementAlignment" />
34+
35+
<!-- Class naming sniffs are excluded to preserve BC -->
36+
<exclude name="SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming" />
37+
<exclude name="SlevomatCodingStandard.Classes.SuperfluousExceptionNaming" />
38+
<exclude name="SlevomatCodingStandard.Classes.SuperfluousInterfaceNaming" />
39+
<exclude name="SlevomatCodingStandard.Classes.SuperfluousTraitNaming" />
40+
41+
<!-- Forbid useless annotations - Git and LICENCE file provide more accurate information -->
42+
<!-- Disable forbidden annotation sniff as excluding @api from the list doesn't work -->
43+
<exclude name="SlevomatCodingStandard.Commenting.ForbiddenAnnotations.AnnotationForbidden" />
44+
45+
<!-- Keep long typehints (for now) -->
46+
<exclude name="SlevomatCodingStandard.PHP.TypeCast.InvalidCastUsed" />
47+
<exclude name="SlevomatCodingStandard.TypeHints.LongTypeHints" />
48+
49+
<!-- Don't require a full stop after @throws tags -->
50+
<exclude name="Squiz.Commenting.FunctionComment.ThrowsNoFullStop" />
51+
52+
<!-- Disable some sniffs as they can cause functional changes. These will be enabled later -->
53+
<exclude name="Generic.PHP.ForbiddenFunctions.FoundWithAlternative" />
54+
<exclude name="SlevomatCodingStandard.Classes.UnusedPrivateElements" />
55+
<exclude name="SlevomatCodingStandard.ControlStructures.DisallowYodaComparison" />
56+
<exclude name="SlevomatCodingStandard.ControlStructures.EarlyExit" />
57+
<exclude name="SlevomatCodingStandard.ControlStructures.UselessIfConditionWithReturn" />
58+
<exclude name="SlevomatCodingStandard.Functions.StaticClosure" />
59+
<exclude name="SlevomatCodingStandard.Functions.UnusedInheritedVariablePassedToClosure" />
60+
<exclude name="SlevomatCodingStandard.Operators.DisallowEqualOperators" />
61+
62+
<!-- These sniffs cause a large diff, so enable them in separate steps -->
63+
<exclude name="SlevomatCodingStandard.Commenting.DocCommentSpacing.IncorrectAnnotationsGroup" />
64+
<exclude name="SlevomatCodingStandard.Commenting.RequireOneLinePropertyDocComment" />
65+
<exclude name="Squiz.Strings.DoubleQuoteUsage" />
66+
67+
<!-- Sniff currently breaks, see https://github.com/slevomat/coding-standard/issues/727 -->
68+
<exclude name="SlevomatCodingStandard.Namespaces.NamespaceSpacing" />
69+
</rule>
70+
71+
<!-- Change use statement sorting to be compatible with PSR-12 -->
72+
<rule ref="SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses">
73+
<properties>
74+
<property name="psr12Compatible" value="true"/>
75+
</properties>
76+
</rule>
77+
78+
<!-- Forbid fully qualified names even for colliding names -->
79+
<rule ref="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly">
80+
<properties>
81+
<property name="allowFallbackGlobalConstants" value="false"/>
82+
<property name="allowFallbackGlobalFunctions" value="false"/>
83+
<property name="allowFullyQualifiedGlobalClasses" value="false"/>
84+
<property name="allowFullyQualifiedGlobalConstants" value="false"/>
85+
<property name="allowFullyQualifiedGlobalFunctions" value="false"/>
86+
<property phpcs-only="true" name="allowFullyQualifiedNameForCollidingClasses" value="false"/>
87+
<property phpcs-only="true" name="allowFullyQualifiedNameForCollidingConstants" value="false"/>
88+
<property phpcs-only="true" name="allowFullyQualifiedNameForCollidingFunctions" value="false"/>
89+
<property name="searchAnnotations" value="true"/>
90+
</properties>
91+
</rule>
92+
93+
<rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps">
94+
<exclude-pattern>/src/GridFS/StreamWrapper</exclude-pattern>
95+
<exclude-pattern>/tests/DocumentationExamplesTest.php</exclude-pattern>
96+
</rule>
97+
98+
<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
99+
<exclude-pattern>/tests/Compat/PolyfillAssertTrait.php</exclude-pattern>
100+
</rule>
101+
</ruleset>

0 commit comments

Comments
 (0)