Skip to content

Custom Sniffs

Mitchell edited this page Apr 25, 2018 · 4 revisions

Table of Contents

Abstract class must be prefixed with Abstract

Incorrect example ❌

abstract class MyClass
{
    public abstract function getEntityManager();
}

Correct example πŸ‘

abstract class AbstractMyClass
{
    public abstract function getEntityManager();
}

Class and Namespace must be in PascalCase

Incorrect example ❌

namespace This\IS\a\BAD\namespaceE;

class MyClassIs09LookingFaULty
{
}

Correct example πŸ‘

namespace This\Is\A\Good\NameSpaceE;

class MyClassIsLookingGood
{
}

Interface must be postfixed with Interface

Incorrect example ❌

interface MyClass
{
}

Correct example πŸ‘

interface MyClassInterface
{
}

No vertical whitespace between use statements

Incorrect example ❌

use SomeSpace; // comment
use MoreSpace;

use  LostIn\Space;

Correct example πŸ‘

use SomeSpace; // comment
use MoreSpace;
use  LostIn\Space;

Only one use statement per line

Incorrect example ❌

use SomeOtherSpace as Hello, ThisShouldNotBeHere;

Correct example πŸ‘

use SomeOtherSpace as Hello;

Protected properties are not allowed

Incorrect example ❌

    private $bar;

    protected $foo;

    public $public_variable;

Correct example πŸ‘

    private $bar;

    public $public_variable;

Trait must be postfixed with Trait

Incorrect example ❌

trait MyClass
{
}

Correct example πŸ‘

trait MyClassTrait
{
}

Use statements alphabetically ordered

Incorrect example ❌

use A;
use C;
use B;

Correct example πŸ‘

use A;
use B;
use C;

Variable and property must be in snake case

Incorrect example ❌

private $badProperty;

Correct example πŸ‘

private $good_property;

@covers fully qualified name

Incorrect example ❌

/**
 * @covers Hostnet\Test\TestUnit\Bad
 */

Correct example πŸ‘

/**
 * @covers \Hostnet\Test\TestUnit\Good
 */

@covers counter part

Incorrect example ❌

namespace Foo;

class Bar
{
}
namespace Foo;

class BarTest
{
}

Correct example πŸ‘

namespace Foo;

class Bar
{
}
namespace Foo;

/**
 * @covers \Foo\Bar
 */
class BarTest
{
}

File comment copyright

  • Related class: Hostnet\Sniffs\Commenting\FileCommentCopyrightSniff
  • Name of the sniff: Hostnet.Commenting.FileCommentCopyright
  • Parameters:
    • years: Which years should be noted for the copyright, if not configured the _years is 'calculated'
    • copyright_holder: The legal holder of the copyright.
    • copyright_tag: The 'name' of the tag to search for. phpDocumenter uses @copyright.

Incorrect example ❌

<?php
/**
 * Test1234
 */
class abc {

}

Correct example πŸ‘

/**
 * @copyright 2017 Hostnet B.V.
 */

/**
 * Test1234
 */
class abc {

}

Declares strict

Incorrect example ❌

<?php
    echo 'hi there';
?>

Correct example πŸ‘

<?php
declare(strict_types=1);

Return type declaration

  • Related class: Hostnet\Sniffs\Functions\ReturnTypeDeclarationSniff
  • Name of the sniff: Hostnet.Functions.ReturnTypeDeclaration
  • Parameters:
    • closing_parenthesis_colon_spacing: spacing between the function's closing parenthesis and colon.
    • colon_return_type_spacing: spacing between the colon and the return type.

Incorrect example ❌

    public function this() : string
    {
        return;
    }

Correct example πŸ‘

    public function this(): string
    {
        return;
    }

Method name starts with 'getis'

Incorrect example ❌

  protected function getIsStuff()
  {
  }

Correct example πŸ‘

  protected function isStuff()
  {
  }

PHPUnit Namespace

This test was added during the transition between PHPUnit 6 and 7 to ensure that the right TestCase-file is being used.

Incorrect example ❌

class NamespaceTest extends PHPUnit_Framework_TestCase

Correct example πŸ‘

class NamespaceTest extends TestCase