Skip to content

Repository files navigation

Swivl Coding Standard for PHP_CodeSniffer

This coding standard is much more strict version of PSR-12.

Installation

Install coding standard using composer:

composer require --dev swivl/php-coding-standard

Configuration

Create file phpcs.xml.dist in the root of your project with the content similar to:

<?xml version="1.0" encoding="UTF-8"?>
<rulesetxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">
<argname="basepath"value="."/>
<argname="cache"value=".phpcs-cache"/>
<argname="colors"/>
<argvalue="p"/>
<argname="extensions"value="php"/>
<argname="tab-width"value="4"/>
<argname="report-width"value="120"/>
<ruleref="vendor/swivl/php-coding-standard/Swivl/ruleset.xml"/>
<ruleref="Generic.Files.LineLength.TooLong">
<exclude-pattern>*/src/Migrations/*</exclude-pattern>
</rule>
<file>src/</file>
<file>tests/</file>
<exclude-pattern>*/Resources/*</exclude-pattern>
</ruleset>

Configure your IDE to use phpcs.xml.dist as the default ruleset for PHP_CodeSniffer.

Checking code style in your project

Check the whole project:

bin/phpcs

Check a single file or directory:

bin/phpcs path/to/file/or/directory

Automatically fix errors:

bin/phpcbf

Advanced configuration

Swivl.Commenting.DoctrineEntity

This sniff provides the following options:

<ruleref="Swivl.Commenting.DoctrineEntity">
<properties>
<propertyname="concreteTypeToBaseTypeMap"type="array">
<elementkey="FeedItemComment"value="CommentInterface"/>
</property>
<propertyname="useDynamicalCalculationForEnumColumnType"value="true"/>
</properties>
</rule>
  • concreteTypeToBaseTypeMap - when column is mapped to concrete type FeedItemComment, but modification methods are type hinted with base class CommentInterface
  • useDynamicalCalculationForEnumColumnType - when enum is mapped to some PHP type. For example, type="auth_enum_type" is mapped to PHP AuthType.

Swivl.Commenting.FunctionComment

This sniff provides the option:

<ruleref="Swivl.Commenting.FunctionComment">
<properties>
<propertyname="requiredPhpdoc"value="always"/>
<propertyname="minimumVisibility"value="private"/>
</properties>
</rule>
  • requiredPhpdoc - configures when PHPDoc is required for a method. Possible values:
    • always - PHPDoc is required for all methods.
    • map - PHPDoc is required for a method if at least 1 type hint or return type is in the list which is defined in the sniff in FunctionCommentSniff::REQUIRED_PHPDOC_MAP. It is the default behavior.
    • never - PHPDoc isn't required for all methods.

Swivl.Functions.ConstructorPropertyPromotion

This sniff provides the following options:

<ruleref="Swivl.Functions.ConstructorPropertyPromotion">
<properties>
<propertyname="indent"value="4"/>
</properties>
</rule>

Swivl.Commenting.UselessFunctionComment

This sniff provides the following options:

<ruleref="Swivl.Commenting.UselessFunctionComment">
<properties>
<propertyname="ignoreClasses"value="false"/>
<propertyname="ignoreInterfaces"value="false"/>
<propertyname="ignoreTraits"value="false"/>
<propertyname="includeNames"type="array">
<elementvalue="__construct"/>
</property>
<propertyname="excludeNames"type="array">
<elementvalue="__serialize"/>
</property>
<propertyname="maxDescriptionLines"value="1"/>
</properties>
</rule>

Swivl.Commenting.UselessClassComment

This sniff provides the following options:

<ruleref="Swivl.Commenting.UselessClassComment">
<properties>
<propertyname="maxDescriptionLines"value="1"/>
<propertyname="uselessTags"type="array">
<elementvalue="@package"/>
</property>
<propertyname="uselessDescriptions"type="array">
<elementvalue="Doctrine migration"/>
</property>
</properties>
</rule>

Dependencies update

Some sniffs from Swivl Coding Standard override sniffs from libraries which are in dependencies list. The following sniffs should be synchronized with the original code during dependencies update:

About

Swivl Coding Standard for PHP_CodeSniffer

Resources

Stars

3 stars

Watchers

8 watching

Forks

Releases

Packages

Used by

Contributors

Languages