Skip to content

Consistency Rules

This document details the rules available in the Consistency category.

Available Rules

RuleCode
Array Stylearray-style
Assertion Styleassertion-style
Block Statementblock-statement
Braced String Interpolationbraced-string-interpolation
Class Nameclass-name
Constant Nameconstant-name
Enum Nameenum-name
Function Namefunction-name
Interface Nameinterface-name
Lowercase Keywordlowercase-keyword
Lowercase Type Hintlowercase-type-hint
No Alias Functionno-alias-function
No Hash Commentno-hash-comment
No Php Tag Terminatorno-php-tag-terminator
No Trailing Spaceno-trailing-space
Trait Nametrait-name

array-style

Suggests using the short array style [..] instead of the long array style array(..), or vice versa, depending on the configuration. The short array style is more concise and is the preferred way to define arrays in PHP.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"note"
stylestring"short"

Examples

Correct Code

php
<?php

// By default, `style` is 'short', so this snippet is valid:
$arr = [1, 2, 3];

Incorrect Code

php
<?php

// By default, 'short' is enforced, so array(...) triggers a warning:
$arr = array(1, 2, 3);

assertion-style

Enforces a consistent style for PHPUnit assertion calls within test methods.

Maintaining a consistent style (e.g., always using static:: or $this->) improves code readability and helps enforce team-wide coding standards in test suites. This rule can be configured to enforce the preferred style.

Requirements

  • Integration: PHPUnit

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"warning"
stylestring"static"

Examples

Correct Code

php
<?php
// configured style: "static"
final class SomeTest extends TestCase
{
    public function testSomething(): void
    {
        static::assertTrue(true);
    }
}

Incorrect Code

php
<?php
// configured style: "static"
final class SomeTest extends TestCase
{
    public function testSomething(): void
    {
        $this->assertTrue(true); // Incorrect style
        self::assertFalse(false); // Incorrect style
    }
}

block-statement

Enforces that if, else, for, foreach, while, do-while statements always use a block statement body ({ ... }) even if they contain only a single statement.

This improves readability and prevents potential errors when adding new statements.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"note"

Examples

Correct Code

php
<?php

if (true) {
    echo "Hello";
}

for ($i = 0; $i < 10; $i++) {
    echo $i;
}

Incorrect Code

php
<?php

if (true)
    echo "Hello";

for ($i = 0; $i < 10; $i++)
    echo $i;

braced-string-interpolation

Enforces the use of curly braces around variables within string interpolation.

Using curly braces ({$variable}) within interpolated strings ensures clarity and avoids potential ambiguity, especially when variables are followed by alphanumeric characters. This rule promotes consistent and predictable code.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"note"

Examples

Correct Code

php
<?php

$a = "Hello, {$name}!";
$b = "Hello, {$name}!";
$c = "Hello, {$$name}!";
$d = "Hello, {${$object->getMethod()}}!";

Incorrect Code

php
<?php

$a = "Hello, $name!";
$b = "Hello, ${name}!";
$c = "Hello, ${$name}!";
$d = "Hello, ${$object->getMethod()}!";

class-name

Detects class declarations that do not follow class naming convention.

Class names should be in class case, also known as PascalCase.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"help"
psrbooleantrue

Examples

Correct Code

php
<?php

class MyClass {}

Incorrect Code

php
<?php

class my_class {}

class myClass {}

class MY_CLASS {}

constant-name

Detects constant declarations that do not follow constant naming convention.

Constant names should be in constant case, also known as UPPER_SNAKE_CASE.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"help"

Examples

Correct Code

php
<?php

const MY_CONSTANT = 42;

class MyClass {
    public const int MY_CONSTANT = 42;
}

Incorrect Code

php
<?php

const myConstant = 42;
const my_constant = 42;
const My_Constant = 42;

class MyClass {
    public const int myConstant = 42;
    public const int my_constant = 42;
    public const int My_Constant = 42;
}

enum-name

Detects enum declarations that do not follow class naming convention.

Enum names should be in class case, also known as PascalCase.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"help"

Examples

Correct Code

php
<?php

enum MyEnum {}

Incorrect Code

php
<?php

enum my_enum {}
enum myEnum {}
enum MY_ENUM {}

function-name

Detects function declarations that do not follow camel or snake naming convention.

Function names should be in camel case or snake case, depending on the configuration.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"help"
camelbooleanfalse
eitherbooleanfalse

Examples

Correct Code

php
<?php

function my_function() {}

Incorrect Code

php
<?php

function MyFunction() {}

function My_Function() {}

interface-name

Detects interface declarations that do not follow class naming convention.

Interface names should be in class case and suffixed with Interface, depending on the configuration.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"help"
psrbooleantrue

Examples

Correct Code

php
<?php

interface MyInterface {}

Incorrect Code

php
<?php

interface myInterface {}
interface my_interface {}
interface MY_INTERFACE {}

lowercase-keyword

Enforces that PHP keywords (like if, else, return, function, etc.) be written in lowercase. Using uppercase or mixed case is discouraged for consistency and readability.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"help"

Examples

Correct Code

php
<?php

if (true) {
    echo "All keywords in lowercase";
} else {
    return;
}

Incorrect Code

php
<?PHP

IF (TRUE) {
    ECHO "Keywords not in lowercase";
} ELSE {
    RETURN;
}

lowercase-type-hint

Enforces that PHP type hints (like void, bool, int, float, etc.) be written in lowercase. Using uppercase or mixed case is discouraged for consistency and readability.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"help"

Examples

Correct Code

php
<?php

function example(int $param): void {
    return;
}

Incorrect Code

php
<?php

function example(Int $param): VOID {
    return;
}

no-alias-function

Detects usage of function aliases (e.g., diskfreespace instead of disk_free_space) and suggests calling the canonical (original) function name instead. This is primarily for consistency and clarity.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"note"

Examples

Correct Code

php
<?php

// 'disk_free_space' is the proper name instead of 'diskfreespace'
$freeSpace = disk_free_space("/");

Incorrect Code

php
<?php

// 'diskfreespace' is an alias for 'disk_free_space'
$freeSpace = diskfreespace("/");

no-hash-comment

Detects shell-style comments ('#') in PHP code. Double slash comments ('//') are preferred in PHP, as they are more consistent with the language's syntax and are easier to read.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"warning"

Examples

Correct Code

php
<?php

// This is a good comment.

Incorrect Code

php
<?php

# This is a shell-style comment.

no-php-tag-terminator

Discourages the use of ?><?php as a statement terminator. Recommends using a semicolon (;) instead for clarity and consistency.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"note"

Examples

Correct Code

php
<?php

echo "Hello World";

Incorrect Code

php
<?php

echo "Hello World" ?><?php

no-trailing-space

Detects trailing whitespace at the end of comments. Trailing whitespace can cause unnecessary diffs and formatting issues, so it is recommended to remove it.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"note"

Examples

Correct Code

php
<?php

// This is a good comment.

Incorrect Code

php
<?php

// This is a comment with trailing whitespace.

trait-name

Detects trait declarations that do not follow class naming convention. Trait names should be in class case and suffixed with Trait, depending on the configuration.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"help"
psrbooleantrue

Examples

Correct Code

php
<?php

trait MyTrait {}

Incorrect Code

php
<?php

trait myTrait {}
trait my_trait {}
trait MY_TRAIT {}