Skip to content

Instantly share code, notes, and snippets.

@flesheater
Last active June 12, 2019 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flesheater/d009a1014f053f42dac70a850ca9ebc6 to your computer and use it in GitHub Desktop.
Save flesheater/d009a1014f053f42dac70a850ca9ebc6 to your computer and use it in GitHub Desktop.
PHP quality tools cheatsheet.

Code Sniffer

Checking if code covers coding standards.

Installable with composer

Codesniffer

composer require --dev "squizlabs/php_codesniffer=*"

Coder module with the Drupal standards

composer require --dev drupal/coder

Configurable through phpcs.xml

<?xml version="1.0"?>
<ruleset name="PHPCI">

    <description>Codestyle ruleset for Drupal</description>

    <rule ref="Drupal"/>
    <rule ref="DrupalPractice"/>

    <config name="installed_paths" value="vendor/drupal/coder/coder_sniffer"/>

</ruleset>

Usage

./vendor/bin/phpcs

Documentation

PHP Mess Detector

Raw metrics of PHP code.

Installable with composer

composer require --dev phpmd/phpmd

Configurable through phpmd.xml

<ruleset
    name="PHPMD rules"
    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"
>
    <rule ref="rulesets/cleancode.xml/BooleanArgumentFlag" />
    <rule ref="rulesets/cleancode.xml/ElseExpression" />
    <rule ref="rulesets/codesize.xml/CyclomaticComplexity" />
    <rule ref="rulesets/codesize.xml/ExcessiveClassComplexity" />
    <rule ref="rulesets/codesize.xml/ExcessiveClassLength" />
    <rule ref="rulesets/codesize.xml/ExcessiveMethodLength" />
    <rule ref="rulesets/codesize.xml/ExcessiveParameterList" />
    <rule ref="rulesets/design.xml/EvalExpression" />
    <rule ref="rulesets/design.xml/GotoStatement" />
    <rule ref="rulesets/naming.xml/ConstructorWithNameAsEnclosingClass" />
    <rule ref="rulesets/unusedcode.xml/UnusedFormalParameter" />
    <rule ref="rulesets/unusedcode.xml/UnusedPrivateField" />
    <rule ref="rulesets/unusedcode.xml/UnusedPrivateMethod" />
</ruleset>

Skip validating if not relevant

https://phpmd.org/rules/cleancode.html

/**
 * Running certain hook_update().
 *
 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
 */
function HOOK_update_8002(&$sandbox) {}

Usage

PHP execution errors.

 ./vendor/bin/phpmd web/modules/custom/ text cleancode --suffixes php,phtml,module

Unused code. (also covered by code sniffer)

./vendor/bin/phpmd web/modules/custom/ text unusedcode --suffixes php,phtml,module

Documentation

PHP CS Fixer

Fixes some problems found by phpcs

Installable with composer

composer require --dev friendsofphp/php-cs-fixer

Usage

  1. Run phpcs
./vendor/bin/phpcs web/modules/custom
  1. Identify A_FILE that could be fixed with PHP Csfixer.
FILE: /Users/A_FILE
----------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AND 2 WARNINGS AFFECTING 3 LINES
----------------------------------------------------------------------------------------------------------
 12 | WARNING | [ ] Unused variable $test.
 13 | ERROR   | [x] Short array syntax must be used to define arrays
 27 | WARNING | [ ] Unused variable $hello.
----------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------------------------------------------

Time: 117ms; Memory: 8MB
  1. Fixingt A_FILE with phpcbf
./vendor/bin/phpcbf A_FILE --standard=Drupal,DrupalPractice

Documentation

PHP Parallel Lint

Fast checking PHP syntax.

Installable with composer

composer require --dev jakub-onderka/php-parallel-lint

Usage

./vendor/bin/parallel-lint web/modules

Documentation

PhpCodeFixer

Installable with composer

composer require --dev wapmorgan/php-code-fixer

Usage

./vendor/bin/phpcf --target 7.2 web/modules/custom/

Documentation

PHPQA

Installable with composer

composer require --dev edgedesign/phpqa

Configurable through .phpqa.yml


Usage

./vendor/bin/phpqa --analyzedDirs web/modules/custom/

Add plugins

composer require --dev macfja/phpqa-extensions

Documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment