Skip to content

Correctness Rules

This document details the rules available in the Correctness category.

Available Rules

RuleCode
Assert Descriptionassert-description
Identity Comparisonidentity-comparison
Invalid Open Taginvalid-open-tag
No Assign In Conditionno-assign-in-condition
No Boolean Literal Comparisonno-boolean-literal-comparison
No Empty Catch Clauseno-empty-catch-clause
Parameter Typeparameter-type
Property Typeproperty-type
Return Typereturn-type
Strict Assertionsstrict-assertions
Strict Behaviorstrict-behavior
Strict Typesstrict-types

assert-description

Detects assert functions that do not have a description.

Assert functions should have a description to make it easier to understand the purpose of the assertion.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"warning"

Examples

Correct Code

php
<?php

assert($user->isActivated(), 'User MUST be activated at this point.');

Incorrect Code

php
<?php

assert($user->isActivated());

identity-comparison

Detects equality and inequality comparisons that should use identity comparison operators.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"warning"

Examples

Correct Code

php
<?php

if ($a === $b) {
    echo '$a is same as $b';
}

Incorrect Code

php
<?php

if ($a == $b) {
    echo '$a is same as $b';
}

invalid-open-tag

Detects misspelled PHP opening tags like <php? instead of <?php.

A misspelled opening tag will cause the PHP interpreter to treat the following code as plain text, leading to the code being output directly to the browser instead of being executed. This can cause unexpected behavior and potential security vulnerabilities.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"note"

Examples

Correct Code

php
<?php

echo 'Hello, world!';

Incorrect Code

php
<php?

echo 'Hello, world!';

no-assign-in-condition

Detects assignments in conditions which can lead to unexpected behavior and make the code harder to read and understand.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"warning"

Examples

Correct Code

php
<?php

$x = 1;
if ($x == 1) {
    // ...
}

Incorrect Code

php
<?php

if ($x = 1) {
    // ...
}

no-boolean-literal-comparison

Disallows comparisons where a boolean literal is used as an operand.

Comparing with a boolean literal (true or false) is redundant and can often be simplified. For example, if ($x === true) is equivalent to the more concise if ($x), and if ($y !== false) is the same as if ($y).

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"note"

Examples

Correct Code

php
<?php

if ($x) { /* ... */ }
if (!$y) { /* ... */ }

Incorrect Code

php
<?php

if ($x === true) { /* ... */ }
if ($y != false) { /* ... */ }

no-empty-catch-clause

Warns when a catch clause is empty.

An empty catch clause suppresses exceptions without handling or logging them, potentially hiding errors that should be addressed. This practice, known as "exception swallowing," can make debugging significantly more difficult.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"warning"

Examples

Correct Code

php
<?php

try {
    // some code that might throw an exception
} catch(Exception $e) {
    // Handle the error, log it, or re-throw it.
    error_log($e->getMessage());
}

Incorrect Code

php
<?php

try {
    // some code
} catch(Exception $e) {
    // This block is empty and swallows the exception.
}

parameter-type

Detects parameters that are missing a type hint.

Requirements

  • PHP Version: PHP >= 7.0.0

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"warning"
ignore-closurebooleanfalse
ignore-arrow-functionbooleanfalse

Examples

Correct Code

php
<?php

function foo(string $bar): void
{
    // ...
}

Incorrect Code

php
<?php

function foo($bar): void
{
    // ...
}

property-type

Detects class-like properties that are missing a type hint.

Requirements

  • PHP Version: PHP >= 7.4.0

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"warning"

Examples

Correct Code

php
<?php

class Foo
{
    public int $bar;
}

Incorrect Code

php
<?php

class Foo
{
    public $bar;
}

return-type

Detects functions, methods, closures, and arrow functions that are missing a return type hint.

Requirements

  • PHP Version: PHP >= 7.0.0

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"warning"
ignore-closurebooleanfalse
ignore-arrow-functionbooleanfalse

Examples

Correct Code

php
<?php

function foo(): int {
    return 42;
}

Incorrect Code

php
<?php

function foo() {
    return 42;
}

strict-assertions

Detects non-strict assertions in test methods. Assertions should use strict comparison methods, such as assertSame or assertNotSame instead of assertEquals or assertNotEquals.

Requirements

  • Integration: PHPUnit

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"warning"

Examples

Correct Code

php
<?php

declare(strict_types=1);

use PHPUnit\Framework\TestCase;

final class SomeTest extends TestCase
{
    public function testSomething(): void
    {
        $this->assertSame(42, 42);
    }
}

Incorrect Code

php
<?php

declare(strict_types=1);

use PHPUnit\Framework\TestCase;

final class SomeTest extends TestCase
{
    public function testSomething(): void
    {
        $this->assertEquals(42, 42);
    }
}

strict-behavior

Detects functions relying on loose comparison unless the $strict parameter is specified. The use of loose comparison for these functions may lead to hard-to-debug, unexpected behaviors.

Requirements

  • PHP Version: PHP >= 7.0.0

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"warning"
allow-loose-behaviorbooleanfalse

Examples

Correct Code

php
<?php

in_array(1, ['foo', 'bar', 'baz'], strict: true);

Incorrect Code

php
<?php

in_array(1, ['foo', 'bar', 'baz']);

strict-types

Detects missing declare(strict_types=1); statement at the beginning of the file.

Requirements

  • PHP Version: PHP >= 7.0.0

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"warning"
allow-disablingbooleanfalse

Examples

Correct Code

php
<?php

declare(strict_types=1);

echo "Hello, World!";

Incorrect Code

php
<?php

echo "Hello, World!";