2017 © Pedro Peláez
 

library codor

Custom PHPCS sniffs to find Code Smells.

image

bmitch/codor

Custom PHPCS sniffs to find Code Smells.

  • Friday, March 2, 2018
  • by bmitch
  • Repository
  • 2 Watchers
  • 20 Stars
  • 9,091 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 4 Forks
  • 24 Open issues
  • 31 Versions
  • 12 % Grown

The README.md

Code Odor Sniffer

:nose: :poop: Custom PHP Code Sniffer sniffs to help find Code Smells (Odor)., (*1)

Build Status codecov Scrutinizer Code Quality Code Climate Packagist Packagist, (*2)

License

Inspired by: https://github.com/object-calisthenics/phpcs-calisthenics-rules, (*3)

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., (*4)

Compatibility

  • PHP 7.1+ please use v1.0.0 and above.
  • PHP 5.6 and below please use any version below v1.0.0.

How to Install?

Install via Composer:, (*5)

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., (*6)


<ruleset name="Project">
    <description>Project Coding Standard</description>

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

Then run it with the command:, (*7)

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

Where src is the location of the source code you want to check., (*8)

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:, (*9)

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

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

Running Specific Sniffs

Or you can also specify which sniffs to specifically run:, (*11)

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

Suppressing the sniffs on specific pieces of code

Please see the PHPCS documentation:
https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#ignoring-files-and-folders, (*12)

Sniffs Included

Codor.ControlStructures.NoElse

Does not allow for any else or elseif statements., (*13)

:x:, (*14)

if ($foo) {
    return 'bar';
} else {
    return 'baz';
}

:white_check_mark:, (*15)

if ($foo) {
    return 'bar';
}
return 'baz';

Codor.Files.FunctionLength

Functions/methods must be no more than 20 lines., (*16)

:x:, (*17)

public function foo()
{
    // more than 20 lines
}

:white_check_mark:, (*18)

public function foo()
{
    // no more than 20 lines
}

Codor.Files.FunctionParameter

Functions/methods must have no more than 3 parameters., (*19)

:x:, (*20)

public function foo($bar, $baz, $bop, $portugal)
{
    // 
}

:white_check_mark:, (*21)

public function foo($bar, $baz, $bop)
{
    // 
}

Codor.Files.ReturnNull

Functions/methods must not return null., (*22)

:x:, (*23)

public function getAdapter($bar)
{
    if ($bar === 'baz') {
        return new BazAdapter;
    }
    return null;
}

:white_check_mark:, (*24)

public function getAdapter($bar)
{
    if ($bar === 'baz') {
        return new BazAdapter;
    }
    return NullAdapter;
}

Codor.Files.MethodFlagParameter

Functions/methods cannot have parameters that default to a boolean., (*25)

:x:, (*26)

public function getCustomers($active = true)
{
    if ($active) {
        // Code to get customers from who are active
    }

    // Code to get all customers
}

:white_check_mark:, (*27)

public function getAllCustomers()
{    
    // Code to get all customers
}

public function getAllActiveCustomers()
{    
    // Code to get customers from who are active
}

Codor.Classes.ClassLength

Classes must be no more than 200 lines., (*28)

:x:, (*29)

class ClassTooLong
{
    // More than 200 lines
}

:white_check_mark:, (*30)

class ClassNotTooLong
{
    // No more than 200 lines
}

Codor.Classes.ConstructorLoop

Class constructors must not contain any loops., (*31)

:x:, (*32)

public function __construct()
{
    for($index = 1; $index < 100; $index++) {
        // foo
    }
}

:white_check_mark:, (*33)

public function __construct()
{
    $this->someMethod();
}

private function someMethod()
{
    for($index = 1; $index < 100; $index++) {
        // foo
    }
}

Codor.Classes.Extends

Warns if a class extends another class. Goal is to promote composition over inheritance (https://en.wikipedia.org/wiki/Composition_over_inheritance)., (*34)

:x:, (*35)

class GasolineCar extends Vehicle
{
    //
}

class GasolineVehicle extends Vehicle
{
    //
}

:white_check_mark:, (*36)

class Vehicle
{
    private $fuel;

    public function __construct(FuelInterface $fuel)
    {
        $this->fuel;    
    }
}

class Gasoline implements FuelInterface
{

}

$gasolineCar = new Vehicle($gasoline);

Codor.Classes.FinalPrivate

Final classes should not contain protected methods or variables. Should use private instead., (*37)

:x:, (*38)

final class Foo 
{
    protected $baz;

    protected function bar()
    {
        //
    }
}

:white_check_mark:, (*39)

final class Foo 
{
    private $baz;

    private function bar()
    {
        //
    }
}

Codor.Classes.NewInstance

Classes should not instantiate objects. Should use dependency injection., (*40)

:x:, (*41)

class NewInConstructor
{
    private MyClass $myClass;

    public function __construct()
    {
        $this->myClass = new MyClass();
    }
}

:white_check_mark:, (*42)

class NewInConstructor
{
    private MyClass $myClass;

    public function __construct(MyClass $myClass)
    {
        $this->myClass = $myClass;
    }
}

Codor.Classes.PropertyDeclaration

Produces an error if your class uses undeclared member variables. Only warns if class extends another class., (*43)

:x:, (*44)

class Foo 
{
    private function bar()
    {
        $this->baz = 13;
    }
}

:white_check_mark:, (*45)

class Foo 
{
    private $baz;

    private function bar()
    {
        $this->baz = 13;
    }
}

Codor.Files.FunctionNameContainsAndOr

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

:x:, (*47)

public function validateStringAndUpdateDatabase()
{
    // code to validate string
    // code to update database
}

:white_check_mark:, (*48)

public function validateString()
{
    // code to validate string
}

public function updateDatabase()
{
    // code to update database
}

Codor.Files.IndentationLevel

Functions/methods cannot have more than 2 level of indentation., (*49)

:x:, (*50)

public function foo($collection)
{
    foreach ($collection as $bar) {
        foreach ($bar as $baz) {
            //
        }
    }
}

:white_check_mark:, (*51)

public function foo($collection)
{
    foreach ($collection as $bar) {
        $this->process($bar);
    }
}

private function process($bar)
{
    foreach ($bar as $baz) {
        //
    }
}

Codor.ControlStructures.NestedIf

Nested if statements are not allowed., (*52)

:x:, (*53)

public function allowedToDrink($person)
{
    if ($person->age === 19) {
        if ($person->hasValidId()) {
            return true;
        }
    }

    return false;
}

:white_check_mark:, (*54)

public function allowedToDrink($person)
{
    if ($person->age !== 19) {
        return false;
    }

    if (! $person->hasValidId()) {
        return false;
    }

    return true;
}

Codor.Syntax.NullCoalescing

Produces an error if a line contains a ternary operator that could be converted to a Null Coalescing operator., (*55)

:x:, (*56)

$username = isset($customer['name']) ? $customer['name'] : 'nobody';

:white_check_mark:, (*57)

$username = $customer['name'] ?? 'nobody';

Codor.Syntax.LinesAfterMethod

Only allows for 1 line between functions/methods. Any more than 1 will produce an error., (*58)

:x:, (*59)

public function foo()
{
    //
}


public function bar()
{
    //
}

:white_check_mark:, (*60)

public function foo()
{
    //
}

public function bar()
{
    //
}

Codor.TypeHints.MixedReturnType

Prevents you from having a mixed type returned in a doc block., (*61)

:x:, (*62)

/**
 * @return mixed
 */
public function foo()
{
    //
}

:white_check_mark:, (*63)

/**
 * @return string
 */
public function foo()
{
    //
}

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:, (*64)


<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 have (default = 20).
  • 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 have (default = 200).
  • Codor.Files.IndentationLevel
    • indentationLimit: Cannot have more than or equal to this number of indentations (default = 2).

Similar Packages

  • https://github.com/object-calisthenics/phpcs-calisthenics-rules
  • https://github.com/slevomat/coding-standard

Contributing

Please see CONTRIBUTING.md, (*65)

License

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

The Versions

29/03 2017

0.0.11

0.0.11.0

Custom PHPCS sniffs to find Code Smells.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Bill Mitchell

28/03 2017

0.0.10

0.0.10.0

Custom PHPCS sniffs to find Code Smells.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Bill Mitchell

30/12 2016

0.0.9

0.0.9.0

Custom PHPCS sniffs to find Code Smells.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Bill Mitchell

29/12 2016

0.0.8

0.0.8.0

Custom PHPCS sniffs to find Code Smells.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Bill Mitchell

29/12 2016

0.0.7

0.0.7.0

Custom PHPCS sniffs to find Code Smells.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Bill Mitchell

28/12 2016

0.0.6

0.0.6.0

Custom PHPCS sniffs to find Code Smells.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Bill Mitchell

11/12 2016

0.0.5

0.0.5.0

Custom PHPCS sniffs to find Code Smells.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Bill Mitchell

10/12 2016

0.0.4

0.0.4.0

Custom PHPCS sniffs to find Code Smells.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Bill Mitchell

10/12 2016

0.0.3

0.0.3.0

Custom PHPCS sniffs to find Code Smells.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Bill Mitchell

09/12 2016

0.0.2

0.0.2.0

Custom PHPCS sniffs to find Code Smells.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Bill Mitchell

07/12 2016

0.0.1

0.0.1.0

Custom PHPCS sniffs to find Code Smells.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Bill Mitchell