carthage-software/mago
Mago is an extremely fast PHP linter, formatter, and static analyzer written in Rust. It brings Rust-inspired speed and reliability to PHP projects with a modern toolchain and great developer experience, plus multiple install options (script, Homebrew, Composer).
title: Consistency rules
This document details the rules available in the Consistency category.
| Rule | Code |
|---|---|
| Ambiguous Constant Access | ambiguous-constant-access |
| Ambiguous Function Call | ambiguous-function-call |
| Array Style | array-style |
| Assertion Style | assertion-style |
| Block Statement | block-statement |
| Braced String Interpolation | braced-string-interpolation |
| Class Name | class-name |
| Constant Name | constant-name |
| Enum Name | enum-name |
| File Name | file-name |
| Function Name | function-name |
| Interface Name | interface-name |
| Lowercase Keyword | lowercase-keyword |
| Lowercase Type Hint | lowercase-type-hint |
| Method Name | method-name |
| No Alias Function | no-alias-function |
| No Alternative Syntax | no-alternative-syntax |
| No Fully Qualified Global Class-Like | no-fully-qualified-global-class-like |
| No Fully Qualified Global Constant | no-fully-qualified-global-constant |
| No Fully Qualified Global Function | no-fully-qualified-global-function |
| No Hash Comment | no-hash-comment |
| No Php Tag Terminator | no-php-tag-terminator |
| No Trailing Space | no-trailing-space |
| Property Name | property-name |
| Trait Name | trait-name |
| Variable Name | variable-name |
ambiguous-constant-accessEnforces that all constant references made from within a namespace are explicit.
When an unqualified constant like PHP_VERSION is referenced from within a namespace,
PHP performs a runtime fallback check (current namespace -> global namespace). This
ambiguity can lead to unexpected behavior if a constant with the same name is later
defined in the namespace.
Making references explicit improves readability and prevents bugs.
| Option | Type | Default |
|---|---|---|
enabled |
boolean |
false |
level |
string |
"help" |
<?php
namespace App;
use const PHP_VERSION;
// OK: Explicitly imported
$version1 = PHP_VERSION;
// OK: Explicitly global
$version2 = \PHP_VERSION;
<?php
namespace App;
// Ambiguous: could be App\PHP_VERSION or \PHP_VERSION
$version = PHP_VERSION;
ambiguous-function-callEnforces that all function calls made from within a namespace are explicit.
When an unqualified function like strlen() is called from within a namespace, PHP
performs a runtime fallback check (current namespace -> global namespace). This
ambiguity prevents PHP from performing powerful compile-time optimizations,
such as replacing a call to strlen() with the highly efficient STRLEN opcode.
Making calls explicit improves readability, prevents bugs, and allows for significant performance gains in some cases.
| Option | Type | Default |
|---|---|---|
enabled |
boolean |
false |
level |
string |
"help" |
<?php
namespace App;
use function strlen;
// OK: Explicitly imported
$length1 = strlen("hello");
// OK: Explicitly global
$length2 = \strlen("hello");
// OK: Explicitly namespaced
$value = namespace\my_function();
<?php
namespace App;
// Ambiguous: could be App\strlen or \strlen
$length = strlen("hello");
array-styleSuggests 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.
| Option | Type | Default |
|---|---|---|
enabled |
boolean |
true |
level |
string |
"note" |
style |
string |
"short" |
<?php
// By default, `style` is 'short', so this snippet is valid:
$arr = [1, 2, 3];
<?php
// By default, 'short' is enforced, so array(...) triggers a warning:
$arr = array(1, 2, 3);
assertion-styleEnforces 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.
PHPUnit| Option | Type | Default |
|---|---|---|
enabled |
boolean |
true |
level |
string |
"warning" |
style |
string |
"static" |
<?php
// configured style: "static"
final class SomeTest extends TestCase
{
public function testSomething(): void
{
static::assertTrue(true);
}
}
<?php
// configured style: "static"
final class SomeTest extends TestCase
{
public function testSomething(): void
{
$this->assertTrue(true); // Incorrect style
self::assertFalse(false); // Incorrect style
}
}
block-statementEnforces 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.
| Option | Type | Default |
|---|---|---|
enabled |
boolean |
true |
level |
string |
"note" |
<?php
if (true) {
echo "Hello";
}
for ($i = 0; $i < 10; $i++) {
echo $i;
}
<?php
if (true)
echo "Hello";
for ($i = 0; $i < 10; $i++)
echo $i;
braced-string-interpolationEnforces 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.
| Option | Type | Default |
|---|---|---|
enabled |
boolean |
true |
level |
string |
"note" |
<?php
$a = "Hello, {$name}!";
$b = "Hello, {$name}!";
$c = "Hello, {$$name}!";
$d = "Hello, {${$object->getMethod()}}!";
<?php
$a = "Hello, $name!";
$b = "Hello, ${name}!";
$c = "Hello, ${$name}!";
$d = "Hello, ${$object->getMethod()}!";
class-nameDetects class declarations that do not follow class naming convention.
Class names should be in class case, also known as PascalCase.
| Option | Type | Default |
|---|---|---|
enabled |
boolean |
true |
level |
string |
"help" |
psr |
boolean |
false |
<?php
class MyClass {}
<?php
class my_class {}
class myClass {}
class MY_CLASS {}
constant-nameDetects constant declarations that do not follow constant naming convention.
Constant names should be in constant case, also known as UPPER_SNAKE_CASE.
| Option | Type | Default |
|---|---|---|
enabled |
boolean |
true |
level |
string |
"help" |
<?php
const MY_CONSTANT = 42;
class MyClass {
public const int MY_CONSTANT = 42;
}
<?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-nameDetects enum declarations that do not follow class naming convention.
Enum names should be in class case, also known as PascalCase.
| Option | Type | Default |
|---|---|---|
enabled |
boolean |
true |
level |
string |
"help" |
<?php
enum MyEnum {}
<?php
enum my_enum {}
enum myEnum {}
enum MY_ENUM {}
file-nameEnsures that a file containing a single class-like definition is named after that definition.
For example, a file containing class Foo must be named Foo.php.
Optionally, this rule can also check functions: a file containing a single function foo
must be named foo.php.
| Option | Type | Default |
|---|---|---|
enabled |
boolean |
true |
level |
string |
"warning" |
check-functions |
boolean |
false |
<?php
// File: test.php
namespace App;
class test
{
}
<?php
// File: test.php
namespace App;
class Foo
{
}
function-nameDetects 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.
| Option | Type | Default |
|---|---|---|
enabled |
boolean |
true |
level |
string |
"help" |
camel |
boolean |
false |
either |
boolean |
false |
<?php
function my_function() {}
<?php
function MyFunction() {}
function My_Function() {}
interface-nameDetects interface declarations that do not follow class naming convention.
Interface names should be in class case and suffixed with Interface, depending on the configuration.
| Option | Type | Default |
|---|---|---|
enabled |
boolean |
true |
level |
string |
"help" |
psr |
boolean |
false |
<?php
interface MyInterface {}
<?php
interface myInterface {}
interface my_interface {}
interface MY_INTERFACE {}
lowercase-keywordEnforces that PHP keywords (like if, else, return, function, etc.) be written
in lowercase. Using uppercase or mixed case is discouraged for consistency and readability.
| Option | Type | Default |
|---|---|---|
enabled |
boolean |
true |
level |
string |
"help" |
<?php
if (true) {
echo "All keywords in lowercase";
} else {
return;
}
<?PHP
IF (TRUE) {
ECHO "Keywords not in lowercase";
} ELSE {
RETURN;
}
lowercase-type-hintEnforces 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.
| Option | Type | Default |
|---|---|---|
enabled |
boolean |
true |
level |
string |
"help" |
<?php
function example(int $param): void {
return;
}
<?php
function example(Int $param): VOID {
return;
}
method-nameDetects method declarations that do not follow the configured naming convention.
By default, method names should be in camelCase. Magic methods (prefixed with __)
are always excluded.
The use-snake-case-for-tests option enforces snake_case for test methods
(names starting with test), which is a common convention in PHPUnit.
| Option | Type | Default |
|---|---|---|
enabled |
boolean |
false |
level |
string |
"help" |
camel |
boolean |
true |
either |
boolean |
false |
use-snake-case-for-tests |
boolean |
false |
<?php
class Foo
{
public function getName(): string {}
public function setName(string $name): void {}
}
<?php
class Foo
{
public function GetName(): string {}
public function set_name(string $name): void {}
}
no-alias-functionDetects 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.
| Option | Type | Default |
|---|---|---|
enabled |
boolean |
true |
level |
string |
"note" |
<?php
// 'disk_free_space' is the proper name instead of 'diskfreespace'
$freeSpace = disk_free_space("/");
<?php
// 'diskfreespace' is an alias for 'disk_free_space'
$freeSpace = diskfreespace("/");
no-alternative-syntaxDetects the use of alternative syntax for control structures
(endif, endwhile, endfor, endforeach, endswitch).
The brace-style syntax is preferred for consistency with the rest of the codebase and is the convention used by the Symfony coding standards.
| Option | Type | Default |
|---|---|---|
enabled |
boolean |
false |
level |
string |
"warning" |
<?php
if ($condition) {
echo 'yes';
}
<?php
if ($condition):
echo 'yes';
endif;
no-fully-qualified-global-class-likeDisallows fully-qualified class-like references within a namespace.
Instead of using the backslash prefix (e.g., new \DateTime() or \Exception
in a type hint), prefer an explicit use import statement. This improves
readability and keeps imports centralized at the top of the file.
| Option | Type | Default |
|---|---|---|
enabled |
boolean |
false |
level |
string |
"help" |
<?php
namespace App;
use DateTime;
use Exception;
$dt = new DateTime();
function foo(DateTime $dt): Exception {}
<?php
namespace App;
$dt = new \DateTime();
function foo(\DateTime $dt): \Exception {}
no-fully-qualified-global-constantDisallows fully-qualified references to global constants within a namespace.
Instead of using the backslash prefix (e.g., \PHP_VERSION),
prefer an explicit use const import statement. This improves
readability and keeps imports centralized at the top of the file.
| Option | Type | Default |
|---|---|---|
enabled |
boolean |
false |
level |
string |
"help" |
<?php
namespace App;
use const PHP_VERSION;
$version = PHP_VERSION;
<?php
namespace App;
$version = \PHP_VERSION;
no-fully-qualified-global-functionDisallows fully-qualified references to global functions within a namespace.
Instead of using the backslash prefix (e.g., \strlen()),
prefer an explicit use function import statement. This improves
readability and keeps imports centralized at the top of the file.
| Option | Type | Default |
|---|---|---|
enabled |
boolean |
false |
level |
string |
"help" |
<?php
namespace App;
use function strlen;
$length = strlen("hello");
<?php
namespace App;
$length = \strlen("hello");
no-hash-commentDetects 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.
| Option | Type | Default |
|---|---|---|
enabled |
boolean |
true |
level |
string |
"warning" |
<?php
// This is a good comment.
<?php
# This is a shell-style comment.
no-php-tag-terminatorDiscourages the use of ?><?php as a statement terminator. Recommends using a semicolon
(;) instead for clarity and consistency.
| Option | Type | Default |
|---|---|---|
enabled |
boolean |
true |
level |
string |
"note" |
<?php
echo "Hello World";
<?php
echo "Hello World" ?><?php
no-trailing-spaceDetects trailing whitespace at the end of comments. Trailing whitespace can cause unnecessary diffs and formatting issues, so it is recommended to remove it.
| Option | Type | Default |
|---|---|---|
enabled |
boolean |
true |
level |
string |
"note" |
<?php
// This is a good comment.
<?php
// This is a comment with trailing whitespace.
property-nameDetects class property declarations that do not follow camel or snake naming convention.
Property names should be in camel case or snake case, depending on the configuration.
| Option | Type | Default |
|---|---|---|
enabled |
boolean |
false |
level |
string |
"help" |
camel |
boolean |
true |
either |
boolean |
false |
<?php
final class Foo {
public string $myProperty;
public function __construct(
public int $myPromotedProperty,
) {}
}
<?php
final class Foo {
public string $My_Property;
public function __construct(
public int $My_Promoted_Property,
) {}
}
trait-nameDetects trait declarations that do not follow class naming convention.
Trait names should be in class case and suffixed with Trait, depending on the configuration.
| Option | Type | Default |
|---|---|---|
enabled |
boolean |
true |
level |
string |
"help" |
psr |
boolean |
false |
<?php
trait MyTrait {}
<?php
trait myTrait {}
trait my_trait {}
trait MY_TRAIT {}
variable-nameDetects variable declarations that do not follow camel or snake naming convention.
Variable names should be in camel case or snake case, depending on the configuration.
| Option | Type | Default |
|---|---|---|
enabled |
boolean |
false |
level |
string |
"help" |
camel |
boolean |
false |
either |
boolean |
true |
check-parameters |
boolean |
true |
<?php
$my_variable = 1;
function foo($my_param) {}
<?php
$MyVariable = 1;
$My_Variable = 2;
function foo($MyParam) {}
How can I help you explore Laravel packages today?