Custom PHP Code Sniffer sniffs to help find Code Smells (Odor).
Inspired by: https://github.com/object-calisthenics/phpcs-calisthenics-rules
- What Is it?
- How to Install?
- How to Use?
- Omitting Sniffs
- Running Specific Sniffs
- Sniffs Included
- Customizing Sniffs
- Customizations Available
- Contributing
- License
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.
Install via Composer:
composer require bmitch/codor --dev
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.
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)
Or you can also specify which sniffs to specifically run:
vendor/bin/phpcs --standard=codor.xml --sniffs=Codor.ControlStructures.NoElse src
Does not allow for any else or elseif statements.
Functions/methods must be no more than 20 lines.
Functions/methods must have no more than 3 parameters.
Functions/methods must not return null.
Classes must be no more than 200 lines.
Functions/methods cannot contain "And" or "Or". This could be a sign of a function that does more than one thing.
Functions/methods cannot have more than 1 level of indentation.
Nested if statements are not allowed.
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>
Codor.Files.FunctionLengthmaxLength: The maximum number of lines a function/method can be (default = 200).Codor.Files.FunctionParametermaxParameters: The maximum number of parameters a function/method can have (default = 3).Codor.Classes.ClassLengthmaxLength: The maximum number of lines a Class can be (default = 20).Codor.Files.IndentationLevelindentationLimit: Cannot have more than or equal to this number of indentations (default = 2).
Please see CONTRIBUTING.md
The MIT License (MIT). Please see License File for more information.
