Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdannbv committed Aug 26, 2024
0 parents commit c44dbb2
Show file tree
Hide file tree
Showing 13 changed files with 731 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.github export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.idea
/vendor
composer.lock
16 changes: 16 additions & 0 deletions CodingStandard/ruleset.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<ruleset
name="Backdevs Coding Standard"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../vendor/squizlabs/php_codesniffer/phpcs.xsd"
>
<description>Backdevs Coding Standard.</description>

<autoload>../autoload-standalone.php</autoload>

<config name="installed_paths" value="../../slevomat/coding-standard"/>

<rule ref="../phpcs/rulesets/base.xml"/>
<rule ref="../phpcs/rulesets/psr.xml"/>
<rule ref="../phpcs/rulesets/slevomat.xml"/>
</ruleset>
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Backdevs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
88 changes: 88 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Backdevs' PHP Coding Standard

A [PHP_CodeSniffer](https://github.com/PHPCSStandards/PHP_CodeSniffer) coding standard and other static analysis tools configuration files for Backdevs' PHP projects.

## Included default configurations
- [PHP_CodeSniffer](./phpcs/phpcs.xml)
- [PHP Mess Detector](./phpmd/phpmd.xml)

## Installation

Composer:
```shell
composer require --dev backdevs/coding-standard
```

## Usage

### PHP_CodeSniffer:

In your project's `phpcs.xml` file add the following line:
```xml
<rule ref="BackdevsCodingStandard"/>
```

If you don't have a `phpcs.xml` file, here's an example for a Laravel project:
```xml
<?xml version="1.0"?>
<ruleset
name="Backdevs Coding Standard"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/squizlabs/php_codesniffer/phpcs.xsd"
>
<description>Backdevs Coding Standard.</description>

<arg name="extensions" value="php"/>
<arg name="colors" />

<arg value="sp"/>

<file>app</file>
<file>bootstrap</file>
<file>config</file>
<file>database</file>
<file>routes</file>
<file>tests</file>

<exclude-pattern>cache/*</exclude-pattern>

<rule ref="BackdevsCodingStandard"/>
</ruleset>
```

Now you should be able to run:
```shell
vendor/bin/phpcs
```

### PHP Mess Detector:
If you don't have the `phpmd/phpmd` package installed, you can install it by running:
```shell
composer require --dev phpmd/phpmd
```

Then, in your project's `phpmd.xml` file add the following line:
```xml
<rule ref="vendor/backdevs/coding-standard/phpmd/phpmd.xml"/>
```

If you don't have a `phpmd.xml` file, here's a simple example:
```xml
<?xml version="1.0"?>
<ruleset
name="Backdevs Coding Standard"
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd"
>
<description>Backdevs Coding Standard.</description>

<rule ref="vendor/backdevs/coding-standard/phpmd/phpmd.xml"/>
</ruleset>
```

Now you can run:
```shell
vendor/bin/phpmd app,bootstrap,config,database,routes,tests text phpmd.xml
```
11 changes: 11 additions & 0 deletions autoload-standalone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

if (defined('BACKDEVS_CODING_STANDARD_AUTOLOAD') === false) {
if (is_file(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
}

define('BACKDEVS_CODING_STANDARD_AUTOLOAD', true);
}
29 changes: 29 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "backdevs/coding-standard",
"description": "Backdevs PHP_CodeSniffer Coding Standard and other static analysis tools' configurations.",
"type": "phpcodesniffer-standard",
"minimum-stability": "stable",
"license": "MIT",
"authors": [
{
"name": "Bogdan Nicorici",
"email": "[email protected]"
}
],
"require": {
"php": "^8.3",
"squizlabs/php_codesniffer": "^3.10",
"slevomat/coding-standard": "^8.15",
"phpmd/phpmd": "^2.15"
},
"autoload": {
"psr-4": {
"BackdevsCodingStandard\\": "CodingStandard/"
}
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
17 changes: 17 additions & 0 deletions phpcs/phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<ruleset
name="Backdevs PHP_CodeSniffer default configuration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../vendor/squizlabs/php_codesniffer/phpcs.xsd"
>
<description>Backdevs PHP_CodeSniffer default configuration.</description>

<arg name="extensions" value="php"/>
<arg name="colors"/>

<arg value="sp"/>

<config name="installed_paths" value="../../slevomat/coding-standard,../../backdevs/coding-standard"/>

<rule ref="BackdevsCodingStandard"/>
</ruleset>
178 changes: 178 additions & 0 deletions phpcs/rulesets/base.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?xml version="1.0"?>
<ruleset name="Backdevs Base" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../vendor/squizlabs/php_codesniffer/phpcs.xsd">
<!-- Forbid `array(...)` -->
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
<!-- Forbid duplicate classes -->
<rule ref="Generic.Classes.DuplicateClassName"/>
<!-- Forbid empty statements -->
<rule ref="Generic.CodeAnalysis.EmptyStatement">
<!-- But allow empty catch -->
<exclude name="Generic.CodeAnalysis.EmptyStatement.DetectedCatch"/>
</rule>
<!-- Forbid final methods in final classes -->
<rule ref="Generic.CodeAnalysis.UnnecessaryFinalModifier"/>
<!-- Forbid useless empty method overrides -->
<rule ref="Generic.CodeAnalysis.UselessOverridingMethod"/>
<!-- Forbid inline HTML in PHP code -->
<rule ref="Generic.Files.InlineHTML"/>
<!-- Force whitespace after a type cast -->
<rule ref="Generic.Formatting.SpaceAfterCast"/>
<!-- Forbid PHP 4 constructors -->
<rule ref="Generic.NamingConventions.ConstructorName"/>
<!-- Forbid any content before opening tag -->
<rule ref="Generic.PHP.CharacterBeforePHPOpeningTag"/>
<!-- Forbid deprecated functions -->
<rule ref="Generic.PHP.DeprecatedFunctions"/>
<!-- Forbid alias functions, i.e. `sizeof()`, `delete()` -->
<rule ref="Generic.PHP.ForbiddenFunctions">
<properties>
<property name="forbiddenFunctions" type="array"
value="
chop => rtrim,
close => closedir,
delete => unset,
doubleval => floatval,
fputs => fwrite,
ini_alter => ini_set,
is_double => is_float,
is_integer => is_int,
is_long => is_int,
is_null => null,
is_real => is_float,
is_writeable => is_writable,
join => implode,
key_exists => array_key_exists,
pos => current,
show_source => highlight_file,
sizeof => count,
strchr => strstr
"/>
</properties>
</rule>
<!-- Forbid useless inline string concatenation -->
<rule ref="Generic.Strings.UnnecessaryStringConcat">
<!-- But multiline is useful for readability -->
<properties>
<property name="allowMultiline" type="boolean" value="true"/>
</properties>
</rule>
<!-- Forbid backtick operator -->
<rule ref="Generic.PHP.BacktickOperator"/>
<!-- Forbid short open tag -->
<rule ref="Generic.PHP.DisallowShortOpenTag"/>
<!-- Forbid `php_sapi_name()` function -->
<rule ref="Generic.PHP.SAPIUsage"/>

<!-- Forbid comments starting with # -->
<rule ref="PEAR.Commenting.InlineComment"/>

<!-- Forbid spaces around square brackets -->
<rule ref="Squiz.Arrays.ArrayBracketSpacing"/>
<!-- Force array declaration structure -->
<rule ref="Squiz.Arrays.ArrayDeclaration">
<!-- Disable arrow alignment -->
<exclude name="Squiz.Arrays.ArrayDeclaration.DoubleArrowNotAligned"/>
<!-- Uses indentation of only single space -->
<exclude name="Squiz.Arrays.ArrayDeclaration.KeyNotAligned"/>
<!-- Allow multiple values on a single line -->
<exclude name="Squiz.Arrays.ArrayDeclaration.SingleLineNotAllowed"/>
<!-- Allow single values on multi line array -->
<exclude name="Squiz.Arrays.ArrayDeclaration.MultiLineNotAllowed"/>
<!-- Disable alignment of braces -->
<exclude name="Squiz.Arrays.ArrayDeclaration.CloseBraceNotAligned"/>
<!-- Disable alignment of values with opening brace -->
<exclude name="Squiz.Arrays.ArrayDeclaration.ValueNotAligned"/>
<!-- Checked by SlevomatCodingStandard.Arrays.TrailingArrayComma.MissingTrailingComma -->
<exclude name="Squiz.Arrays.ArrayDeclaration.NoCommaAfterLast"/>
</rule>
<!-- Forbid class being in a file with different name -->
<rule ref="Squiz.Classes.ClassFileName"/>
<!-- Force `self::` for self-reference, force lower-case self, forbid spaces around `::` -->
<rule ref="Squiz.Classes.SelfMemberReference"/>
<!-- Force phpDoc alignment -->
<rule ref="Squiz.Commenting.DocCommentAlignment">
<!-- Allow extra spaces after star, i.e. for indented annotations -->
<exclude name="Squiz.Commenting.DocCommentAlignment.SpaceAfterStar"/>
</rule>
<!-- Force rules for function phpDoc -->
<rule ref="Squiz.Commenting.FunctionComment">
<!-- Allow `@throws` without description -->
<exclude name="Squiz.Commenting.FunctionComment.EmptyThrows"/>
<!-- Does not work properly with PHP 7 / short-named types -->
<exclude name="Squiz.Commenting.FunctionComment.IncorrectParamVarName"/>
<!-- Does not support collections, i.e. `string[]` -->
<exclude name="Squiz.Commenting.FunctionComment.IncorrectTypeHint"/>
<!-- Forces incorrect types -->
<exclude name="Squiz.Commenting.FunctionComment.InvalidReturn"/>
<!-- Breaks with compound return types, i.e. `string|null` -->
<exclude name="Squiz.Commenting.FunctionComment.InvalidReturnNotVoid"/>
<!-- Breaks when all params are not documented -->
<exclude name="Squiz.Commenting.FunctionComment.InvalidTypeHint"/>
<!-- Doc comment is not required for every method -->
<exclude name="Squiz.Commenting.FunctionComment.Missing"/>
<!-- Do not require comments for `@param` -->
<exclude name="Squiz.Commenting.FunctionComment.MissingParamComment"/>
<!-- Do not require `@param` for all parameters -->
<exclude name="Squiz.Commenting.FunctionComment.MissingParamTag"/>
<!-- Do not require `@return` for void methods -->
<exclude name="Squiz.Commenting.FunctionComment.MissingReturn"/>
<!-- Comments don't have to be sentences -->
<exclude name="Squiz.Commenting.FunctionComment.ParamCommentFullStop"/>
<!-- Comments don't have to be sentences -->
<exclude name="Squiz.Commenting.FunctionComment.ParamCommentNotCapital"/>
<!-- Breaks when all params are not documented -->
<exclude name="Squiz.Commenting.FunctionComment.ParamNameNoMatch"/>
<!-- Doesn't respect inheritance -->
<exclude name="Squiz.Commenting.FunctionComment.ScalarTypeHintMissing"/>
<!-- Don't require @param variable names to be aligned -->
<exclude name="Squiz.Commenting.FunctionComment.SpacingAfterParamType"/>
<!-- Doesn't work with self as typehint -->
<exclude name="Squiz.Commenting.FunctionComment.TypeHintMissing"/>
</rule>
<!-- Forbid global functions -->
<rule ref="Squiz.Functions.GlobalFunction"/>
<!-- Forbid `AND` and `OR`, require `&&` and `||` -->
<rule ref="Squiz.Operators.ValidLogicalOperators"/>
<!-- Forbid `global` -->
<rule ref="Squiz.PHP.GlobalKeyword"/>
<!-- Forbid functions inside functions -->
<rule ref="Squiz.PHP.InnerFunctions"/>
<!-- Require PHP function calls in lowercase -->
<rule ref="Squiz.PHP.LowercasePHPFunctions"/>
<!-- Forbid dead code -->
<rule ref="Squiz.PHP.NonExecutableCode"/>
<!-- Forbid `$this` inside static function -->
<rule ref="Squiz.Scope.StaticThisUsage"/>
<!-- Forbid strings in `"` unless necessary -->
<rule ref="Squiz.Strings.DoubleQuoteUsage"/>
<rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
<message>Variable "%s" not allowed in double quoted string; use sprintf() or concatenation instead</message>
</rule>
<!-- Forbid braces around string in `echo` -->
<rule ref="Squiz.Strings.EchoedStrings"/>
<!-- Forbid spaces in type casts -->
<rule ref="Squiz.WhiteSpace.CastSpacing"/>
<!-- Forbid blank line after function opening brace -->
<rule ref="Squiz.WhiteSpace.FunctionOpeningBraceSpace"/>
<!-- Require space after language constructs -->
<rule ref="Squiz.WhiteSpace.LanguageConstructSpacing"/>
<!-- Require space around logical operators -->
<rule ref="Squiz.WhiteSpace.LogicalOperatorSpacing"/>
<!-- Forbid spaces around `->` operator -->
<rule ref="Squiz.WhiteSpace.ObjectOperatorSpacing">
<properties>
<property name="ignoreNewlines" type="boolean" value="true"/>
</properties>
</rule>
<!-- Forbid superfluous whitespaces -->
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">
<properties>
<!-- turned on by PSR2 -> turning back off -->
<property name="ignoreBlankLines" type="boolean" value="false"/>
</properties>
</rule>
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.EmptyLines">
<!-- turned off by PSR2 -> turning back on -->
<severity>5</severity>
</rule>
</ruleset>
10 changes: 10 additions & 0 deletions phpcs/rulesets/psr.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<ruleset name="Backdevs PSR" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../vendor/squizlabs/php_codesniffer/phpcs.xsd">
<rule ref="PSR1"/>

<rule ref="PSR2">
<exclude name="PSR2.ControlStructures.ControlStructureSpacing.SpacingAfterOpenBrace" />
</rule>

<rule ref="PSR12"/>
</ruleset>
Loading

0 comments on commit c44dbb2

Please sign in to comment.