Skip to content

Clarity rules

This document details the rules available in the Clarity category.

RuleCode
Explicit Octalexplicit-octal
Literal Named Argumentliteral-named-argument
No Emptyno-empty
No Hash Emojino-hash-emoji
No Multi Assignmentsno-multi-assignments
No Nested Ternaryno-nested-ternary
No Shorthand Ternaryno-shorthand-ternary
No Variable Variableno-variable-variable
Str Containsstr-contains
Str Starts Withstr-starts-with
Tagged FIXMEtagged-fixme
Tagged TODOtagged-todo
Valid Docblockvalid-docblock

explicit-octal

Detects implicit octal numeral notation and suggests replacing it with explicit octal numeral notation.

Requirements

  • PHP version: >= 8.1.0

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"warning"

Examples

Correct code

php
<?php

$a = 0o123;

Incorrect code

php
<?php

$a = 0123;

literal-named-argument

Enforces that literal values used as arguments in function or method calls are passed as named arguments.

This improves readability by clarifying the purpose of the literal value at the call site. It is particularly helpful for boolean flags, numeric constants, and null values where the intent is often ambiguous without the parameter name.

Requirements

  • PHP version: >= 8.0.0

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"warning"

Examples

Correct code

php
<?php

function set_option(string $key, bool $enable_feature) {}

set_option(key: 'feature_x', enable_feature: true); // ✅ clear intent

Incorrect code

php
<?php

function set_option(string $key, bool $enable_feature) {}

set_option('feature_x', true); // ❌ intent unclear

no-empty

Detects the use of the empty() construct.

The empty() language construct can lead to ambiguous and potentially buggy code due to loose and counterintuitive definition of emptiness. It fails to clearly convey developer's intent or expectation, making it preferable to use explicit checks.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"error"

Examples

Correct code

php
<?php

if ($myArray === []) {
    // ...
}

Incorrect code

php
<?php

if (!empty($myArray)) {
    // ...
}

no-hash-emoji

Discourages usage of the #️⃣ emoji in place of the ASCII #.

While PHP allows the use of emojis in comments, it is generally discouraged to use them in place of the normal ASCII # symbol. This is because it can confuse readers and may break external tools that expect the normal ASCII # symbol.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"warning"

Examples

Correct code

php
<?php

# This is a comment

#[MyAttribute]
class Foo {}

Incorrect code

php
<?php

#️⃣ This is a comment

#️⃣[MyAttribute] <- not a valid attribute
class Foo {}

no-multi-assignments

Flags any instances of multiple assignments in a single statement. This can lead to confusion and unexpected behavior, and is generally considered poor practice.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"warning"

Examples

Correct code

php
<?php

$b = 0;
$a = $b;

Incorrect code

php
<?php

$a = $b = 0;

no-nested-ternary

Nested ternary expressions are disallowed to improve code clarity and prevent potential bugs arising from confusion over operator associativity.

In PHP 8.0 and later, the ternary operator (? :) is non-associative. Before PHP 8.0, it was left-associative, which is now deprecated. Most other programming languages treat it as right-associative. This inconsistency across versions and languages can make nested ternaries hard to reason about, even when using parentheses.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"warning"

Examples

Correct code

php
<?php

if ($user->isAdmin()) {
    $allowed = true;
} else {
    $allowed = $user->isEditor();
}

Incorrect code

php
<?php

$allowed = $user->isAdmin() ? true : ($user->isEditor() ? true : false);

no-shorthand-ternary

Detects the use of the shorthand ternary and elvis operators.

Both shorthand ternary operator ($a ? : $b) and elvis operator ($a ?: $b) relies on loose comparison.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"warning"

Examples

Correct code

php
<?php

$value = $foo ?? $default;
$value = $foo ? $foo : $default;

Incorrect code

php
<?php
$value = $foo ?: $default;
$value = $foo ? : $default;

no-variable-variable

Discourages usage of PHP's variable variables feature.

Variable variables can make code harder to read and maintain, as they introduce a level of indirection that can confuse readers and complicate static analysis.

Configuration

OptionTypeDefault
enabledbooleanfalse
levelstring"warning"

Examples

Correct code

php
<?php

$foo = 'bar';

echo $foo; // Outputs 'bar'

Incorrect code

php
<?php

$foo = 'bar';
$varName = 'foo';

echo $$varName; // Outputs 'bar'

str-contains

Detects strpos($a, $b) !== false comparisons and suggests replacing them with str_contains($a, $b) for improved readability and intent clarity.

Requirements

  • PHP version: >= 8.0.0

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"warning"

Examples

Correct code

php
<?php

$a = 'hello world';
$b = 'world';

if (str_contains($a, $b)) {
    echo 'Found';
}

Incorrect code

php
<?php

$a = 'hello world';
$b = 'world';

if (strpos($a, $b) !== false) {
    echo 'Found';
}

str-starts-with

Detects strpos($a, $b) === 0 comparisons and suggests replacing them with str_starts_with($a, $b) for improved readability and intent clarity.

Requirements

  • PHP version: >= 8.0.0

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"warning"

Examples

Correct code

php
<?php

$a = 'hello world';
$b = 'hello';
if (str_starts_with($a, $b)) {
    echo 'Found';
}

Incorrect code

php
<?php

$a = 'hello world';
$b = 'hello';
if (strpos($a, $b) === 0) {
    echo 'Found';
}

tagged-fixme

Detects FIXME comments that are not tagged with a user or issue reference. Untagged FIXME comments are not actionable and can be easily missed by the team. Tagging the FIXME comment with a user or issue reference ensures that the issue is tracked and resolved.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"warning"

Examples

Correct code

php
<?php

// FIXME(@azjezz) This is a valid FIXME comment.
// FIXME(azjezz) This is a valid FIXME comment.
// FIXME(#123) This is a valid FIXME comment.

Incorrect code

php
<?php

// FIXME: This is an invalid FIXME comment.

tagged-todo

Detects TODO comments that are not tagged with a user or issue reference. Untagged TODOs can be difficult to track and may be forgotten. Tagging TODOs with a user or issue reference makes it easier to track progress and ensures that tasks are not forgotten.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"warning"

Examples

Correct code

php
<?php

// TODO(@azjezz) This is a valid TODO comment.
// TODO(azjezz) This is a valid TODO comment.
// TODO(#123) This is a valid TODO comment.

Incorrect code

php
<?php

// TODO: This is an invalid TODO comment.

valid-docblock

Checks for syntax errors in docblock comments. This rule is disabled by default because it can be noisy and may not be relevant to all codebases.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"note"

Examples

Correct code

php
<?php

/**
 * For more information, {@see https://example.com}.
 *
 * @param int $a
 *
 * @return int
 */
function foo($a) {
    return $a;
}

Incorrect code

php
<?php

/**
 * For more information, {@see https://example.com
 *
 * @param int $a
 *
 * @return int
 */
function foo($a) {
    return $a;
}