Skip to content

Commit c44dbb2

Browse files
committed
Initial commit
0 parents  commit c44dbb2

File tree

13 files changed

+731
-0
lines changed

13 files changed

+731
-0
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
indent_size = 4
7+
indent_style = space
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false

.gitattributes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/.github export-ignore
2+
.editorconfig export-ignore
3+
.gitattributes export-ignore
4+
.gitignore export-ignore

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.idea
2+
/vendor
3+
composer.lock

CodingStandard/ruleset.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<ruleset
3+
name="Backdevs Coding Standard"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="../vendor/squizlabs/php_codesniffer/phpcs.xsd"
6+
>
7+
<description>Backdevs Coding Standard.</description>
8+
9+
<autoload>../autoload-standalone.php</autoload>
10+
11+
<config name="installed_paths" value="../../slevomat/coding-standard"/>
12+
13+
<rule ref="../phpcs/rulesets/base.xml"/>
14+
<rule ref="../phpcs/rulesets/psr.xml"/>
15+
<rule ref="../phpcs/rulesets/slevomat.xml"/>
16+
</ruleset>

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Backdevs
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Backdevs' PHP Coding Standard
2+
3+
A [PHP_CodeSniffer](https://github.com/PHPCSStandards/PHP_CodeSniffer) coding standard and other static analysis tools configuration files for Backdevs' PHP projects.
4+
5+
## Included default configurations
6+
- [PHP_CodeSniffer](./phpcs/phpcs.xml)
7+
- [PHP Mess Detector](./phpmd/phpmd.xml)
8+
9+
## Installation
10+
11+
Composer:
12+
```shell
13+
composer require --dev backdevs/coding-standard
14+
```
15+
16+
## Usage
17+
18+
### PHP_CodeSniffer:
19+
20+
In your project's `phpcs.xml` file add the following line:
21+
```xml
22+
<rule ref="BackdevsCodingStandard"/>
23+
```
24+
25+
If you don't have a `phpcs.xml` file, here's an example for a Laravel project:
26+
```xml
27+
<?xml version="1.0"?>
28+
<ruleset
29+
name="Backdevs Coding Standard"
30+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
31+
xsi:noNamespaceSchemaLocation="./vendor/squizlabs/php_codesniffer/phpcs.xsd"
32+
>
33+
<description>Backdevs Coding Standard.</description>
34+
35+
<arg name="extensions" value="php"/>
36+
<arg name="colors" />
37+
38+
<arg value="sp"/>
39+
40+
<file>app</file>
41+
<file>bootstrap</file>
42+
<file>config</file>
43+
<file>database</file>
44+
<file>routes</file>
45+
<file>tests</file>
46+
47+
<exclude-pattern>cache/*</exclude-pattern>
48+
49+
<rule ref="BackdevsCodingStandard"/>
50+
</ruleset>
51+
```
52+
53+
Now you should be able to run:
54+
```shell
55+
vendor/bin/phpcs
56+
```
57+
58+
### PHP Mess Detector:
59+
If you don't have the `phpmd/phpmd` package installed, you can install it by running:
60+
```shell
61+
composer require --dev phpmd/phpmd
62+
```
63+
64+
Then, in your project's `phpmd.xml` file add the following line:
65+
```xml
66+
<rule ref="vendor/backdevs/coding-standard/phpmd/phpmd.xml"/>
67+
```
68+
69+
If you don't have a `phpmd.xml` file, here's a simple example:
70+
```xml
71+
<?xml version="1.0"?>
72+
<ruleset
73+
name="Backdevs Coding Standard"
74+
xmlns="http://pmd.sf.net/ruleset/1.0.0"
75+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
76+
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
77+
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd"
78+
>
79+
<description>Backdevs Coding Standard.</description>
80+
81+
<rule ref="vendor/backdevs/coding-standard/phpmd/phpmd.xml"/>
82+
</ruleset>
83+
```
84+
85+
Now you can run:
86+
```shell
87+
vendor/bin/phpmd app,bootstrap,config,database,routes,tests text phpmd.xml
88+
```

autoload-standalone.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
if (defined('BACKDEVS_CODING_STANDARD_AUTOLOAD') === false) {
6+
if (is_file(__DIR__ . '/vendor/autoload.php')) {
7+
require_once __DIR__ . '/vendor/autoload.php';
8+
}
9+
10+
define('BACKDEVS_CODING_STANDARD_AUTOLOAD', true);
11+
}

composer.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "backdevs/coding-standard",
3+
"description": "Backdevs PHP_CodeSniffer Coding Standard and other static analysis tools' configurations.",
4+
"type": "phpcodesniffer-standard",
5+
"minimum-stability": "stable",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Bogdan Nicorici",
10+
"email": "bogdan@backdevs.net"
11+
}
12+
],
13+
"require": {
14+
"php": "^8.3",
15+
"squizlabs/php_codesniffer": "^3.10",
16+
"slevomat/coding-standard": "^8.15",
17+
"phpmd/phpmd": "^2.15"
18+
},
19+
"autoload": {
20+
"psr-4": {
21+
"BackdevsCodingStandard\\": "CodingStandard/"
22+
}
23+
},
24+
"config": {
25+
"allow-plugins": {
26+
"dealerdirect/phpcodesniffer-composer-installer": true
27+
}
28+
}
29+
}

phpcs/phpcs.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0"?>
2+
<ruleset
3+
name="Backdevs PHP_CodeSniffer default configuration"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="../vendor/squizlabs/php_codesniffer/phpcs.xsd"
6+
>
7+
<description>Backdevs PHP_CodeSniffer default configuration.</description>
8+
9+
<arg name="extensions" value="php"/>
10+
<arg name="colors"/>
11+
12+
<arg value="sp"/>
13+
14+
<config name="installed_paths" value="../../slevomat/coding-standard,../../backdevs/coding-standard"/>
15+
16+
<rule ref="BackdevsCodingStandard"/>
17+
</ruleset>

phpcs/rulesets/base.xml

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="Backdevs Base" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../vendor/squizlabs/php_codesniffer/phpcs.xsd">
3+
<!-- Forbid `array(...)` -->
4+
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
5+
<!-- Forbid duplicate classes -->
6+
<rule ref="Generic.Classes.DuplicateClassName"/>
7+
<!-- Forbid empty statements -->
8+
<rule ref="Generic.CodeAnalysis.EmptyStatement">
9+
<!-- But allow empty catch -->
10+
<exclude name="Generic.CodeAnalysis.EmptyStatement.DetectedCatch"/>
11+
</rule>
12+
<!-- Forbid final methods in final classes -->
13+
<rule ref="Generic.CodeAnalysis.UnnecessaryFinalModifier"/>
14+
<!-- Forbid useless empty method overrides -->
15+
<rule ref="Generic.CodeAnalysis.UselessOverridingMethod"/>
16+
<!-- Forbid inline HTML in PHP code -->
17+
<rule ref="Generic.Files.InlineHTML"/>
18+
<!-- Force whitespace after a type cast -->
19+
<rule ref="Generic.Formatting.SpaceAfterCast"/>
20+
<!-- Forbid PHP 4 constructors -->
21+
<rule ref="Generic.NamingConventions.ConstructorName"/>
22+
<!-- Forbid any content before opening tag -->
23+
<rule ref="Generic.PHP.CharacterBeforePHPOpeningTag"/>
24+
<!-- Forbid deprecated functions -->
25+
<rule ref="Generic.PHP.DeprecatedFunctions"/>
26+
<!-- Forbid alias functions, i.e. `sizeof()`, `delete()` -->
27+
<rule ref="Generic.PHP.ForbiddenFunctions">
28+
<properties>
29+
<property name="forbiddenFunctions" type="array"
30+
value="
31+
chop => rtrim,
32+
close => closedir,
33+
delete => unset,
34+
doubleval => floatval,
35+
fputs => fwrite,
36+
ini_alter => ini_set,
37+
is_double => is_float,
38+
is_integer => is_int,
39+
is_long => is_int,
40+
is_null => null,
41+
is_real => is_float,
42+
is_writeable => is_writable,
43+
join => implode,
44+
key_exists => array_key_exists,
45+
pos => current,
46+
show_source => highlight_file,
47+
sizeof => count,
48+
strchr => strstr
49+
"/>
50+
</properties>
51+
</rule>
52+
<!-- Forbid useless inline string concatenation -->
53+
<rule ref="Generic.Strings.UnnecessaryStringConcat">
54+
<!-- But multiline is useful for readability -->
55+
<properties>
56+
<property name="allowMultiline" type="boolean" value="true"/>
57+
</properties>
58+
</rule>
59+
<!-- Forbid backtick operator -->
60+
<rule ref="Generic.PHP.BacktickOperator"/>
61+
<!-- Forbid short open tag -->
62+
<rule ref="Generic.PHP.DisallowShortOpenTag"/>
63+
<!-- Forbid `php_sapi_name()` function -->
64+
<rule ref="Generic.PHP.SAPIUsage"/>
65+
66+
<!-- Forbid comments starting with # -->
67+
<rule ref="PEAR.Commenting.InlineComment"/>
68+
69+
<!-- Forbid spaces around square brackets -->
70+
<rule ref="Squiz.Arrays.ArrayBracketSpacing"/>
71+
<!-- Force array declaration structure -->
72+
<rule ref="Squiz.Arrays.ArrayDeclaration">
73+
<!-- Disable arrow alignment -->
74+
<exclude name="Squiz.Arrays.ArrayDeclaration.DoubleArrowNotAligned"/>
75+
<!-- Uses indentation of only single space -->
76+
<exclude name="Squiz.Arrays.ArrayDeclaration.KeyNotAligned"/>
77+
<!-- Allow multiple values on a single line -->
78+
<exclude name="Squiz.Arrays.ArrayDeclaration.SingleLineNotAllowed"/>
79+
<!-- Allow single values on multi line array -->
80+
<exclude name="Squiz.Arrays.ArrayDeclaration.MultiLineNotAllowed"/>
81+
<!-- Disable alignment of braces -->
82+
<exclude name="Squiz.Arrays.ArrayDeclaration.CloseBraceNotAligned"/>
83+
<!-- Disable alignment of values with opening brace -->
84+
<exclude name="Squiz.Arrays.ArrayDeclaration.ValueNotAligned"/>
85+
<!-- Checked by SlevomatCodingStandard.Arrays.TrailingArrayComma.MissingTrailingComma -->
86+
<exclude name="Squiz.Arrays.ArrayDeclaration.NoCommaAfterLast"/>
87+
</rule>
88+
<!-- Forbid class being in a file with different name -->
89+
<rule ref="Squiz.Classes.ClassFileName"/>
90+
<!-- Force `self::` for self-reference, force lower-case self, forbid spaces around `::` -->
91+
<rule ref="Squiz.Classes.SelfMemberReference"/>
92+
<!-- Force phpDoc alignment -->
93+
<rule ref="Squiz.Commenting.DocCommentAlignment">
94+
<!-- Allow extra spaces after star, i.e. for indented annotations -->
95+
<exclude name="Squiz.Commenting.DocCommentAlignment.SpaceAfterStar"/>
96+
</rule>
97+
<!-- Force rules for function phpDoc -->
98+
<rule ref="Squiz.Commenting.FunctionComment">
99+
<!-- Allow `@throws` without description -->
100+
<exclude name="Squiz.Commenting.FunctionComment.EmptyThrows"/>
101+
<!-- Does not work properly with PHP 7 / short-named types -->
102+
<exclude name="Squiz.Commenting.FunctionComment.IncorrectParamVarName"/>
103+
<!-- Does not support collections, i.e. `string[]` -->
104+
<exclude name="Squiz.Commenting.FunctionComment.IncorrectTypeHint"/>
105+
<!-- Forces incorrect types -->
106+
<exclude name="Squiz.Commenting.FunctionComment.InvalidReturn"/>
107+
<!-- Breaks with compound return types, i.e. `string|null` -->
108+
<exclude name="Squiz.Commenting.FunctionComment.InvalidReturnNotVoid"/>
109+
<!-- Breaks when all params are not documented -->
110+
<exclude name="Squiz.Commenting.FunctionComment.InvalidTypeHint"/>
111+
<!-- Doc comment is not required for every method -->
112+
<exclude name="Squiz.Commenting.FunctionComment.Missing"/>
113+
<!-- Do not require comments for `@param` -->
114+
<exclude name="Squiz.Commenting.FunctionComment.MissingParamComment"/>
115+
<!-- Do not require `@param` for all parameters -->
116+
<exclude name="Squiz.Commenting.FunctionComment.MissingParamTag"/>
117+
<!-- Do not require `@return` for void methods -->
118+
<exclude name="Squiz.Commenting.FunctionComment.MissingReturn"/>
119+
<!-- Comments don't have to be sentences -->
120+
<exclude name="Squiz.Commenting.FunctionComment.ParamCommentFullStop"/>
121+
<!-- Comments don't have to be sentences -->
122+
<exclude name="Squiz.Commenting.FunctionComment.ParamCommentNotCapital"/>
123+
<!-- Breaks when all params are not documented -->
124+
<exclude name="Squiz.Commenting.FunctionComment.ParamNameNoMatch"/>
125+
<!-- Doesn't respect inheritance -->
126+
<exclude name="Squiz.Commenting.FunctionComment.ScalarTypeHintMissing"/>
127+
<!-- Don't require @param variable names to be aligned -->
128+
<exclude name="Squiz.Commenting.FunctionComment.SpacingAfterParamType"/>
129+
<!-- Doesn't work with self as typehint -->
130+
<exclude name="Squiz.Commenting.FunctionComment.TypeHintMissing"/>
131+
</rule>
132+
<!-- Forbid global functions -->
133+
<rule ref="Squiz.Functions.GlobalFunction"/>
134+
<!-- Forbid `AND` and `OR`, require `&&` and `||` -->
135+
<rule ref="Squiz.Operators.ValidLogicalOperators"/>
136+
<!-- Forbid `global` -->
137+
<rule ref="Squiz.PHP.GlobalKeyword"/>
138+
<!-- Forbid functions inside functions -->
139+
<rule ref="Squiz.PHP.InnerFunctions"/>
140+
<!-- Require PHP function calls in lowercase -->
141+
<rule ref="Squiz.PHP.LowercasePHPFunctions"/>
142+
<!-- Forbid dead code -->
143+
<rule ref="Squiz.PHP.NonExecutableCode"/>
144+
<!-- Forbid `$this` inside static function -->
145+
<rule ref="Squiz.Scope.StaticThisUsage"/>
146+
<!-- Forbid strings in `"` unless necessary -->
147+
<rule ref="Squiz.Strings.DoubleQuoteUsage"/>
148+
<rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
149+
<message>Variable "%s" not allowed in double quoted string; use sprintf() or concatenation instead</message>
150+
</rule>
151+
<!-- Forbid braces around string in `echo` -->
152+
<rule ref="Squiz.Strings.EchoedStrings"/>
153+
<!-- Forbid spaces in type casts -->
154+
<rule ref="Squiz.WhiteSpace.CastSpacing"/>
155+
<!-- Forbid blank line after function opening brace -->
156+
<rule ref="Squiz.WhiteSpace.FunctionOpeningBraceSpace"/>
157+
<!-- Require space after language constructs -->
158+
<rule ref="Squiz.WhiteSpace.LanguageConstructSpacing"/>
159+
<!-- Require space around logical operators -->
160+
<rule ref="Squiz.WhiteSpace.LogicalOperatorSpacing"/>
161+
<!-- Forbid spaces around `->` operator -->
162+
<rule ref="Squiz.WhiteSpace.ObjectOperatorSpacing">
163+
<properties>
164+
<property name="ignoreNewlines" type="boolean" value="true"/>
165+
</properties>
166+
</rule>
167+
<!-- Forbid superfluous whitespaces -->
168+
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">
169+
<properties>
170+
<!-- turned on by PSR2 -> turning back off -->
171+
<property name="ignoreBlankLines" type="boolean" value="false"/>
172+
</properties>
173+
</rule>
174+
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.EmptyLines">
175+
<!-- turned off by PSR2 -> turning back on -->
176+
<severity>5</severity>
177+
</rule>
178+
</ruleset>

0 commit comments

Comments
 (0)