dcandido/code-sniffer

v1.6.0 2022-09-01 16:55 UTC

This package is auto-updated.

Last update: 2025-05-29 01:38:35 UTC


README

Table of contents

Requirements

How to install

composer require dcandido/quality-tools --dev

How to use

Enabling the rules

Add it to your project phpcs.xml ruleset:

<?xml version="1.0"?>
<ruleset name="IMSRuleset">
  <description>The coding standard for Project.</description>

  <rule ref="./vendor/dcandido/code-sniffer/ruleset.xml"/>

  <file>app</file>
  <file>bootstrap</file>

  <exclude-pattern>bootstrap/cache/*</exclude-pattern>
  <exclude-pattern>tests/Unit/*</exclude-pattern>
  <exclude-pattern>resources</exclude-pattern>
  <exclude-pattern>bootstrap/autoload.php</exclude-pattern>
  <exclude-pattern>*/migrations/*</exclude-pattern>
  <exclude-pattern>*/seeds/*</exclude-pattern>
  <exclude-pattern>vendor/*</exclude-pattern>
  <exclude-pattern>public/*</exclude-pattern>
  <exclude-pattern>*.js</exclude-pattern>

  <!-- Show progression -->
  <arg value="pv"/> <!-- p for progress, s to see list for each gitblame author, v to see authors with no violations, s include source codes in the report -->
  <arg name="report" value="summary"/><!-- summary, gitblame, source -->
  <arg name="colors"/>
</ruleset>

Usage

Run Command:

./vendor/bin/phpcs

If you have errors, you will see the following:

$ phpcs 

FILE: /path/to/code/file1.php
--------------------------------------------------------------------------------
FOUND 5 ERROR(S) AFFECTING 5 LINE(S)
--------------------------------------------------------------------------------
  2 | ERROR | Missing file doc comment
 20 | ERROR | PHP keywords must be lowercase; expected "false" but found "FALSE"
 47 | ERROR | Line not indented correctly; expected 4 spaces but found 1
 51 | ERROR | Missing function doc comment
 88 | ERROR | Line not indented correctly; expected 9 spaces but found 6
--------------------------------------------------------------------------------

FILE: /path/to/code/file2.php
--------------------------------------------------------------------------------
FOUND 1 ERROR(S) AND 1 WARNING(S) AFFECTING 1 LINE(S)
--------------------------------------------------------------------------------
 21 | ERROR   | PHP keywords must be lowercase; expected "false" but found
    |         | "FALSE"
 21 | WARNING | Equals sign not aligned with surrounding assignments
--------------------------------------------------------------------------------

You can run phpcbf to fix the errors automatically:

./vendor/bin/phpcbf
PHPCBF RESULT SUMMARY
----------------------------------------------------------------------
FILE                                                  FIXED  REMAINING
----------------------------------------------------------------------
.../path/to/code/file.php                               7      0
.../path/to/code/file2.php                              5      0

Sniffing code in PHPStorm

See PHP Code Sniffer in PhpStorm on how to set up CodeSniffer in PHPStorm.