Custom PHPCS sniffs to find Code Smells
PHP
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src/Codor
tests
.gitignore
.travis.yml
CHANGELOG.md
CONTRIBUTING.md
LICENSE.md
README.md
codor.xml
composer.json
phpcs.xml
phpunit.xml

README.md

Code Odor Sniffer

Custom PHP Code Sniffer sniffs to help find Code Smells (Odor).

Build Status codecov Scrutinizer Code Quality Code Climate Packagist Packagist

Inspired by: https://github.com/object-calisthenics/phpcs-calisthenics-rules

What is it?

This package is a set of custom Sniffs for the PHP Code Sniffer that you can use in your CI build to ensure the ingegrity of your code base.

How to Install?

Install via Composer:

composer require bmitch/codor --dev

How to Use?

Create a PHPCS ruleset XML (codor.xml or whatever filename you want) file in the root of your project.

<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="Project">
    <description>Project Coding Standard</description>

    <rule ref="vendor/bmitch/codor/src/Codor/ruleset.xml"/>
</ruleset>

Then run it with the command:

vendor/bin/phpcs --standard=codor.xml src 

Where src is the location of the source code you want to check.

Omitting Sniffs

You may not want to run all the sniffs provided so you can specify which sniffs you want to exclude with the --exclude flag like:

vendor/bin/phpcs --standard=codor.xml --exclude=Codor.ControlStructures.NoElse src

(if you want to exclude multiple just separate them with a comma)

Running Specific Sniffs

Or you can also specify which sniffs to specifically run:

vendor/bin/phpcs --standard=codor.xml --sniffs=Codor.ControlStructures.NoElse src

Sniffs Included

Codor.ControlStructures.NoElse

Does not allow for any else or elseif statements.

Codor.Files.FunctionLength

Functions/methods must be no more than 20 lines.

Codor.Files.FunctionParameter

Functions/methods must have no more than 3 parameters.

Codor.Files.ReturnNull

Functions/methods must not return null.

Codor.Classes.ClassLength

Classes must be no more than 200 lines.

Codor.Files.FunctionNameContainsAndOr

Functions/methods cannot contain "And" or "Or". This could be a sign of a function that does more than one thing.

Codor.Files.IndentationLevel

Functions/methods cannot have more than 1 level of indentation.

Codor.ControlStructures.NestedIf

Nested if statements are not allowed.

Customizing Sniffs

Some of the sniff rules can be customized to your liking. For example, if you'd want the Codor.Files.FunctionLength to make sure your functions are no more than 30 lines instead of 20, you can do that. Here's an example of a codor.xml file with that customization:

<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="Project">
    <description>Project Coding Standard</description>

    <rule ref="vendor/bmitch/codor/src/Codor/ruleset.xml"/>
    <rule ref="Codor.Files.FunctionLength">
        <properties>
            <property name="maxLength" value="30"/>
        </properties>
    </rule>
</ruleset>

Customizations Available

  • Codor.Files.FunctionLength
    • maxLength: The maximum number of lines a function/method can be (default = 200).
  • Codor.Files.FunctionParameter
    • maxParameters: The maximum number of parameters a function/method can have (default = 3).
  • Codor.Classes.ClassLength
    • maxLength: The maximum number of lines a Class can be (default = 20).
  • Codor.Files.IndentationLevel
    • indentationLimit: Cannot have more than or equal to this number of indentations (default = 2).

Contributing

Please see CONTRIBUTING.md

License

The MIT License (MIT). Please see License File for more information.