TLint performs both linting and fixing, although some issues cannot be fixed automatically.
Every formatter has a linter, but not every linter has a formatter.
Prefer view(..., [...]) over view(...)->with(...).
<?php
namespace App;
class Controller
{
function index()
{
- return view('test.view')->with('testing', '1212');
+ return view('test.view', ['testing' => '1212']);
}
}
Import facades using their full namespace.
<?php
namespace Test;
-use DB;
-use Storage;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Storage;
Mailable values (from and subject etc) should be set in build().
public function __construct($url)
{
$this->url = $url;
- $this->from('[email protected]', config('name'));
- $this->subject(config('name') . ' Garage');
}
public function build()
{
+ $this->from('[email protected]', config('name'));
+ $this->subject(config('name') . ' Garage');
return $this->view('auth.emails.email-login');
}
The $dates property was deprecated in Laravel 8. Use $casts instead.
<?php
class Post extends Model
{
- protected $dates = ['email_verified_at'];
-
- protected $casts = [];
+ protected $casts = [
+ 'email_verified_at' => 'datetime',
+ ];
}
Remove doc blocks from the up and down method in migrations.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBuyRequestsTable extends Migration
{
- /**
- * Run the migrations.
- *
- * @return void
- */
public function up()
{
//
}
- /**
- * Reverse the migrations.
- *
- * @return void
- */
public function down()
{
//
}
}
No leading slashes on route paths.
<?php
-Route::get('/home', function () {
+Route::get('home', function () {
return '';
});
No space between blade template directive names and the opening paren: @section (
-> @section(
.
-@section ('sidebar')
+@section('sidebar')
This is the master sidebar.
@show
<div class="container">
- @yield ('content')
+ @yield('content')
</div>
-<span @class (['p-4'])>Padding</span>
+<span @class(['p-4'])>Padding</span>
-<input type="checkbox" value="active" @checked (old('active', $user->active)) />
+<input type="checkbox" value="active" @checked(old('active', $user->active)) />
<select name="version">
- @foreach ($product->versions as $version)
+ @foreach($product->versions as $version)
- <option @selected (old('version') == $version)>
+ <option @selected(old('version') == $version)>
{{ $version }}
</option>
@endforeach
</select>
@auth The user is authenticated @endauth
-@auth ('admin')
+@auth('admin')
// The user is authenticated...
@endauth
Class members of differing visibility must be separated by a blank line.
<?php
namespace App;
class Thing
{
protected const OK = 1;
+
private $ok;
}
Prefer Namespace\..
. over \Namespace\...
.
<?php
-use \Tighten\TLint;
-use \PHPUnit\Framework\TestCase;
+use Tighten\TLint;
+use PHPUnit\Framework\TestCase;
echo test;
Use the request(...) helper function directly to access request values wherever possible.
<?php
namespace App;
class Controller
{
public function index()
{
- return SavedVehicle::findOrFail(request()->get('savedVehicleId'));
+ return SavedVehicle::findOrFail(request('savedVehicleId'));
}
}
Use request()->validate(...) helper function or extract a FormRequest instead of using $this->validate(...) in controllers.
<?php
namespace App;
use App\Http\Controllers\Controller;
class ControllerA extends Controller
{
public function store()
{
- $this->validate(['name' => 'required'], ['name.required' => 'Name is required']);
+ request()->validate(['name' => 'required'], ['name.required' => 'Name is required']);
}
}
Put a space between blade control structure names and the opening paren:@if(
-> @if (
-@if(true)
+@if (true)
This is true.
-@elseif(false)
+@elseif (false)
This is false.
@endif
-@unless(true)
+@unless (true)
This isn't true.
@endunless
Spaces around blade rendered content: {{1 + 1}}
-> {{ 1 + 1 }}
.
-{{1 + 1}}
+{{ 1 + 1 }}
-{{ 1 + 1 }} {{1 + 1}}
+{{ 1 + 1 }} {{ 1 + 1 }}
-{{1 + 1 }}
+{{ 1 + 1 }}
Prefer the auth() helper function over the Auth Facade.
@extends('layouts.app')
-@if (!\Illuminate\Support\Facades\Auth::check())
+-@if (!auth()->check())
<label for="email">Email</label>
<input id="email" class="form-control" type="email" name="email" required>
@else
<input id="email" type="hidden" name="email" value="{{ auth()->user()->email }}" required>
@endif
Apply middleware in routes (not controllers).
Use blade {{ $model }} auto escaping for models, and double quotes via json_encode over @json blade directive: -> OR
Fully Qualified Class Names should only be used for accessing class names.
PHP_CodeSniffer is primarily used to help enforce PSR1.
PSR-1 section 2.3. A file SHOULD declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it SHOULD execute logic with side effects, but SHOULD NOT do both.
Disable side effects for index file.
<exclude-pattern>/public/index.php</exclude-pattern>
PSR-1 section 3. Namespaces and classes MUST follow PSR-0.
Disable missing namespace rule for tests and database files.
<exclude-pattern>*/database/*</exclude-pattern>
<exclude-pattern>*/tests/*</exclude-pattern>
PSR-1 section 3. Class names MUST be declared in StudlyCaps.
PSR-1 section 4.1. Class constants MUST be declared in all upper case with underscore separators.
PSR-1 section 4.3. Method names MUST be declared in camelCase().
Disable camel caps rule for tests
<exclude-pattern>*/tests/*</exclude-pattern>
No dd() and no 'dumps'.
<properties>
<property name="forbiddenFunctions" type="array">
<element key="dd" value="null"/>
<element key="dump" value="null"/>
<element key="var_dump" value="null"/>
<element key="ray" value="null"/>
</property>
</properties>
Use config() over env().
<exclude-pattern>/config/*</exclude-pattern>
Class name should match the file name.
Orders controller class elements to match the order provided in the configuration.
[
'order' => [
'use_trait',
'property_public_static',
'property_protected_static',
'property_private_static',
'constant_public',
'constant_protected',
'constant_private',
'property_public',
'property_protected',
'property_private',
'construct',
'method:__invoke',
'method_public_static',
'method_protected_static',
'method_private_static',
'method:index',
'method:create',
'method:store',
'method:show',
'method:edit',
'method:update',
'method:destroy',
'method_public',
'method_protected',
'method_private',
'magic',
],
]
Orders class elements to match the order provided in the configuration.
[
'order' => [
'use_trait',
'property_public_static',
'property_protected_static',
'property_private_static',
'constant_public',
'constant_protected',
'constant_private',
'property_public',
'property_protected',
'property_private',
'construct',
'method:__invoke',
'method_public_static',
'method_protected_static',
'method_private_static',
'method_public',
'method_protected',
'method_private',
'magic',
],
]
Orders PHPUnit fixtures at the top of files.
[
'order' => [
'use_trait',
'property_public_static',
'property_protected_static',
'property_private_static',
'constant_public',
'constant_protected',
'constant_private',
'property_public',
'property_protected',
'property_private',
'construct',
'method:__invoke',
'phpunit',
'method_public_static',
'method_protected_static',
'method_private_static',
'method_public',
'method_protected',
'method_private',
'magic',
],
]
Pint uses PHP CS Fixer under the hood to automatically fix issues using the following rules and configuration.
Each element of an array must be indented exactly once.
<?php
$foo = [
- 'bar' => [
- 'baz' => true,
- ],
+ 'bar' => [
+ 'baz' => true,
+ ],
];
PHP arrays should be declared using the configured syntax.
[
'syntax' => 'short',
]
<?php
-array(1,2);
+[1,2];
Binary operators should be surrounded by space as configured.
[
'default' => 'single_space',
]
<?php
-$a= 1 + $b^ $d !== $e or $f;
+$a = 1 + $b ^ $d !== $e or $f;
There MUST be one blank line after the namespace declaration.
<?php
namespace Sample\Sample;
-
$a;
<?php
namespace Sample\Sample;
+
Class Test{}
Ensure there is no code on the same line as the PHP open tag and it is followed by a blank line.
-<?php $a = 1;
+<?php
+
+$a = 1;
$b = 1;
An empty line feed must precede any configured statement.
[
'statements' => [
0 => 'continue',
1 => 'return',
],
]
foreach ($foo as $bar) {
if ($bar->isTired()) {
$bar->sleep();
+
continue;
}
}
if (true) {
$foo = $bar;
+
return;
}
Putting blank lines between use
statement groups.
<?php
use function AAC;
+
use const AAB;
+
use AAA;
<?php
use const AAAA;
use const BBB;
+
use Bar;
use AAC;
use Acme;
+
use function CCC\AA;
use function DDD;
<?php
use const BBB;
use const AAAA;
+
use Acme;
use AAC;
use Bar;
+
use function DDD;
use function CCC\AA;
<?php
use const AAAA;
use const BBB;
+
use Acme;
+
use function DDD;
+
use AAC;
+
use function CCC\AA;
+
use Bar;
The body of each structure MUST be enclosed by braces. Braces should be properly placed. Body of braces should be properly indented.
[
'allow_single_line_anonymous_class_with_empty_body' => true,
'allow_single_line_closure' => true,
'position_after_control_structures' => 'same',
'position_after_functions_and_oop_constructs' => 'next',
'position_after_anonymous_constructs' => 'next',
]
<?php
-class Foo {
- public function bar($baz) {
- if ($baz = 900) echo "Hello!";
+class Foo
+{
+ public function bar($baz)
+ {
+ if ($baz = 900) {
+ echo "Hello!";
+ }
- if ($baz = 9000)
+ if ($baz = 9000) {
echo "Wait!";
+ }
- if ($baz == true)
- {
+ if ($baz == true) {
echo "Why?";
- }
- else
- {
+ } else {
echo "Ha?";
}
- if (is_array($baz))
- foreach ($baz as $b)
- {
+ if (is_array($baz)) {
+ foreach ($baz as $b) {
echo $b;
}
+ }
}
}
<?php
$positive = function ($item) { return $item >= 0; };
$negative = function ($item) {
- return $item < 0; };
+ return $item < 0;
+};
<?php
-class Foo
-{
- public function bar($baz)
- {
- if ($baz = 900) echo "Hello!";
+class Foo {
+ public function bar($baz) {
+ if ($baz = 900) {
+ echo "Hello!";
+ }
- if ($baz = 9000)
+ if ($baz = 9000) {
echo "Wait!";
+ }
- if ($baz == true)
- {
+ if ($baz == true) {
echo "Why?";
- }
- else
- {
+ } else {
echo "Ha?";
}
- if (is_array($baz))
- foreach ($baz as $b)
- {
+ if (is_array($baz)) {
+ foreach ($baz as $b) {
echo $b;
}
+ }
}
}
Curly braces must be placed as configured.
[
'control_structures_opening_brace' => 'same_line',
'functions_opening_brace' => 'next_line_unless_newline_at_signature_end',
'anonymous_functions_opening_brace' => 'same_line',
'classes_opening_brace' => 'next_line_unless_newline_at_signature_end',
'anonymous_classes_opening_brace' => 'next_line_unless_newline_at_signature_end',
'allow_single_line_empty_anonymous_classes' => true,
'allow_single_line_anonymous_functions' => false,
]
<?php
// control_structures_opening_brace = same_line
-if (foo())
-{
+if (foo()) {
bar();
}
// functions_opening_brace = next_line_unless_newline_at_signature_end
-function foo() {
+function foo()
+{
}
// anonymous_functions_opening_brace = same_line
-$foo = function()
-{
+$foo = function() {
};
// classes_opening_brace = next_line_unless_newline_at_signature_end
-class Foo {
+class Foo
+{
}
// anonymous_classes_opening_brace = next_line_unless_newline_at_signature_end
-$foo = new class {
+$foo = new class
+{
};
// allow_single_line_empty_anonymous_classes = true
static::swap(
- $fake = new class extends TestCampaignClass
- {
+ $fake = new class extends TestCampaignClass {
//...
}
)
// allow_single_line_anonymous_functions = false
-$foo = function () { return true; };
+$foo = function () {
+ return true;
+};
A single space or none should be between cast and variable.
<?php
-$bar = ( string ) $a;
-$foo = (int)$b;
+$bar = (string) $a;
+$foo = (int) $b;
<?php
-$bar = ( string ) $a;
-$foo = (int)$b;
+$bar = (string) $a;
+$foo = (int) $b;
<?php
-$bar = ( string ) $a;
-$foo = (int) $b;
+$bar = (string)$a;
+$foo = (int)$b;
Class, trait and interface elements must be separated with one or none blank line.
[
'elements' => [
'method' => 'one',
],
]
final class Sample
protected function foo()
{
}
+
protected function bar()
{
}
-
-
}
Whitespace around the keywords of a class, trait, enum or interfaces definition should be one space.
[
'multi_line_extends_each_single_line' => true,
'single_item_single_line' => true,
'single_line' => true,
]
<?php
-class Foo
-extends Bar
-implements Baz, BarBaz
+class Foo extends Bar implements Baz, BarBaz
{}
<?php
-class Foo
-extends Bar
-implements Baz
+class Foo extends Bar implements Baz
{}
<?php
interface Bar extends
- Bar, BarBaz, FooBarBaz
+ Bar,
+ BarBaz,
+ FooBarBaz
{}
Namespace must not contain spacing, comments or PHPDoc.
<?php
-namespace Foo \ Bar;
+namespace Foo\Bar;
<?php
-echo foo /* comment */ \ bar();
+echo foo\bar();
Remove extra spaces in a nullable typehint.
<?php
-function sample(? string $str): ? string
+function sample(?string $str): ?string
{}
Concatenation should be spaced according configuration.
[
'spacing' => 'one',
]
<?php
-$foo = 'bar' . 3 . 'baz'.'qux';
+$foo = 'bar' . 3 . 'baz' . 'qux';
The PHP constants true
, false
, and null
MUST be written using the correct casing.
[
'case' => 'lower',
]
<?php
-$a = FALSE;
-$b = True;
-$c = nuLL;
+$a = false;
+$b = true;
+$c = null;
Equal sign in declare statement should be surrounded by spaces or not following configuration.
<?php
-declare(ticks = 1);
+declare(ticks=1);
<?php
-declare(ticks=1);
+declare(ticks = 1);
The keyword elseif
should be used instead of else if
so that all control keywords look like single words.
<?php
if ($a) {
-} else if ($b) {
+} elseif ($b) {
}
PHP code MUST use only UTF-8 without BOM (remove BOM).
-<?php
+<?php
echo "Hello!";
Converts implicit variables into explicit ones in double-quoted strings or heredoc syntax.
<?php
-$a = "My name is $name !";
-$b = "I live in $state->country !";
-$c = "I have $farm[0] chickens !";
+$a = "My name is {$name} !";
+$b = "I live in {$state->country} !";
+$c = "I have {$farm[0]} chickens !";
PHP code must use the long <?php
tags or short-echo <?=
tags and not other tag variations.
-<?
+<?php
echo "Hello!";
Transforms imported FQCN parameters and return types in function arguments to short version.
use Foo\Bar;
class SomeClass
{
- public function doSomething(\Foo\Bar $foo)
+ public function doSomething(Bar $foo)
{
}
}
use Foo\Bar\Baz;
class SomeClass
{
- public function doSomething(\Foo\Bar $foo): \Foo\Bar\Baz
+ public function doSomething(Bar $foo): Baz
{
}
}
Spaces should be properly placed in a function declaration.
class Foo
{
- public static function bar ( $baz , $foo )
+ public static function bar($baz , $foo)
{
return false;
}
}
-function foo ($bar, $baz)
+function foo($bar, $baz)
{
return false;
}
<?php
-$f = function () {};
+$f = function() {};
<?php
-$f = fn () => null;
+$f = fn() => null;
Ensure single space between function's argument and its typehint.
<?php
-function sample(array$a)
+function sample(array $a)
{}
<?php
-function sample(array $a)
+function sample(array $a)
{}
Renames PHPDoc tags.
<?php
/**
- * @inheritDocs
- * {@inheritdocs}
+ * @inheritDoc
+ * {@inheritDoc}
*/
<?php
/**
* @inheritDocs
- * {@inheritdocs}
+ * {@inheritDoc}
*/
<?php
/**
- * @inheritDocs
+ * @inheritDoc
* {@inheritdocs}
*/
<?php
/**
- * @inheritDocs
+ * @inheritDoc
* {@inheritdocs}
*/
Imports or fully qualifies global classes/functions/constants.
[
'import_classes' => true,
'import_constants' => true,
'import_functions' => true,
]
<?php
namespace Foo;
+use DateTimeImmutable;
+use function count;
+use const M_PI;
-if (\count($x)) {
- /** @var \DateTimeImmutable $d */
- $d = new \DateTimeImmutable();
- $p = \M_PI;
+if (count($x)) {
+ /** @var DateTimeImmutable $d */
+ $d = new DateTimeImmutable();
+ $p = M_PI;
}
Convert heredoc
to nowdoc
where possible.
<?php
-$a = <<<"TEST"
+$a = <<<'TEST'
Foo
TEST;
Include/Require and file path should be divided with a single space. File path should not be placed under brackets.
<?php
-require ("sample1.php");
-require_once "sample2.php";
-include "sample3.php";
-include_once("sample4.php");
+require "sample1.php";
+require_once "sample2.php";
+include "sample3.php";
+include_once "sample4.php";
Pre- or post-increment and decrement operators should be used if possible.
[
'style' => 'post',
]
<?php
-++$a;
---$b;
+$a++;
+$b--;
Code MUST use configured indentation type.
<?php
if (true) {
- echo 'Hello!';
+ echo 'Hello!';
}
Integer literals must be in correct case.
<?php
-$foo = 0Xff;
-$bar = 0B11111111;
+$foo = 0xFF;
+$bar = 0b11111111;
Lambda must not import variables it doesn't use.
<?php
-$foo = function() use ($bar) {};
+$foo = function() {};
Ensure there is no code on the same line as the PHP open tag.
-<?php $a = 1;
+<?php
+$a = 1;
$b = 3;
All PHP files must use same line ending.
List (array
destructuring) assignment should be declared using the configured syntax. Requires PHP >= 7.1.
<?php
-list($sample) = $array;
+[$sample] = $array;
<?php
-[$sample] = $array;
+list($sample) = $array;
Cast should be written in lower case.
<?php
- $a = (BOOLEAN) $b;
- $a = (BOOL) $b;
- $a = (INTEGER) $b;
- $a = (INT) $b;
- $a = (DOUBLE) $b;
- $a = (FLoaT) $b;
- $a = (reaL) $b;
- $a = (flOAT) $b;
- $a = (sTRING) $b;
- $a = (ARRAy) $b;
- $a = (OBJect) $b;
- $a = (UNset) $b;
- $a = (Binary) $b;
+ $a = (boolean) $b;
+ $a = (bool) $b;
+ $a = (integer) $b;
+ $a = (int) $b;
+ $a = (double) $b;
+ $a = (float) $b;
+ $a = (real) $b;
+ $a = (float) $b;
+ $a = (string) $b;
+ $a = (array) $b;
+ $a = (object) $b;
+ $a = (unset) $b;
+ $a = (binary) $b;
<?php
- $a = (BOOLEAN) $b;
- $a = (BOOL) $b;
- $a = (INTEGER) $b;
- $a = (INT) $b;
- $a = (DOUBLE) $b;
- $a = (FLoaT) $b;
- $a = (flOAT) $b;
- $a = (sTRING) $b;
- $a = (ARRAy) $b;
- $a = (OBJect) $b;
- $a = (UNset) $b;
- $a = (Binary) $b;
+ $a = (boolean) $b;
+ $a = (bool) $b;
+ $a = (integer) $b;
+ $a = (int) $b;
+ $a = (double) $b;
+ $a = (float) $b;
+ $a = (float) $b;
+ $a = (string) $b;
+ $a = (array) $b;
+ $a = (object) $b;
+ $a = (unset) $b;
+ $a = (binary) $b;
PHP keywords MUST be in lower case.
<?php
- FOREACH($a AS $B) {
- TRY {
- NEW $C($a, ISSET($B));
- WHILE($B) {
- INCLUDE "test.php";
+ foreach($a as $B) {
+ try {
+ new $C($a, isset($B));
+ while($B) {
+ include "test.php";
}
- } CATCH(\Exception $e) {
- EXIT(1);
+ } catch(\Exception $e) {
+ exit(1);
}
}
Class static references self
, static
and parent
MUST be in lower case.
class Foo extends Bar
{
public function baz1()
{
- return STATIC::baz2();
+ return static::baz2();
}
public function baz2($x)
{
- return $x instanceof Self;
+ return $x instanceof self;
}
- public function baz3(PaRent $x)
+ public function baz3(parent $x)
{
return true;
}
<?php
class Foo extends Bar
{
- public function baz(?self $x) : SELF
+ public function baz(?self $x) : self
{
return false;
}
Magic method definitions and calls must be using the correct casing.
<?php
class Foo
{
- public function __Sleep()
+ public function __sleep()
{
}
}
<?php
-$foo->__INVOKE(1);
+$foo->__invoke(1);
Magic constants should be referred to using the correct casing.
<?php
-echo __dir__;
+echo __DIR__;
In method arguments and method call, there MUST NOT be a space before each comma and there MUST be one space after each comma. Argument lists MAY be split across multiple lines, where each subsequent line is indented once. When doing so, the first item in the list MUST be on the next line, and there MUST be only one argument per line.
[
'on_multiline' => 'ignore',
]
<?php
-function sample($a=10,$b=20,$c=30) {}
-sample(1, 2);
+function sample($a=10, $b=20, $c=30) {}
+sample(1, 2);
Forbid multi-line whitespace before the closing semicolon or move the semicolon to the new line for chained calls.
[
'strategy' => 'no_multi_line',
]
<?php
function foo () {
- return 1 + 2
- ;
+ return 1 + 2;
}
Function defined by PHP should be called using the correct casing.
<?php
-STRLEN($str);
+strlen($str);
Native type hints for functions should use the correct case.
<?php
class Bar {
- public function Foo(CALLABLE $bar)
+ public function Foo(callable $bar)
{
return 1;
}
<?php
-function Foo(INT $a): Bool
+function Foo(int $a): bool
{
return true;
}
<?php
-function Foo(Iterable $a): VOID
+function Foo(iterable $a): void
{
echo 'Hello world';
}
<?php
-function Foo(Object $a)
+function Foo(object $a)
{
return 'hi!';
}
All instances created with new
keyword must (not) be followed by braces.
[
'named_class' => false,
'anonymous_class' => false,
]
<?php
-$y = new class() {};
+$y = new class {};
<?php
-$x = new X();
+$x = new X;
Master functions shall be used instead of aliases.
<?php
-$a = chop($b);
-close($b);
-$a = doubleval($b);
-$a = fputs($b, $c);
-$a = get_required_files();
-ini_alter($b, $c);
-$a = is_double($b);
-$a = is_integer($b);
-$a = is_long($b);
-$a = is_real($b);
-$a = is_writeable($b);
-$a = join($glue, $pieces);
-$a = key_exists($key, $array);
-magic_quotes_runtime($new_setting);
-$a = pos($array);
-$a = show_source($filename, true);
-$a = sizeof($b);
-$a = strchr($haystack, $needle);
-$a = imap_header($imap_stream, 1);
-user_error($message);
+$a = rtrim($b);
+closedir($b);
+$a = floatval($b);
+$a = fwrite($b, $c);
+$a = get_included_files();
+ini_set($b, $c);
+$a = is_float($b);
+$a = is_int($b);
+$a = is_int($b);
+$a = is_float($b);
+$a = is_writable($b);
+$a = implode($glue, $pieces);
+$a = array_key_exists($key, $array);
+set_magic_quotes_runtime($new_setting);
+$a = current($array);
+$a = highlight_file($filename, true);
+$a = count($b);
+$a = strstr($haystack, $needle);
+$a = imap_headerinfo($imap_stream, 1);
+trigger_error($message);
mbereg_search_getregs();
<?php
$a = is_double($b);
-mbereg_search_getregs();
+mb_ereg_search_getregs();
Master language constructs shall be used instead of aliases.
<?php
-die;
+exit;
Replace control structure alternative syntax to use braces.
<?php
-if(true):echo 't';else:echo 'f';endif;
+if(true) { echo 't';} else { echo 'f';}
-<?php if ($condition): ?>
+<?php if ($condition) { ?>
Lorem ipsum.
-<?php endif; ?>
+<?php } ?>
There should not be a binary flag before strings.
-<?php $a = b'foo';
+<?php $a = 'foo';
-<?php $a = b<<<EOT
+<?php $a = <<<EOT
foo
EOT;
There should be no empty lines after class opening brace.
<?php
final class Sample
{
-
protected function foo()
{
}
There should not be blank lines between docblock and the documented element.
/**
* This is the bar class.
*/
-
-
class Bar {}
The closing ?>
tag MUST be omitted from files containing only PHP.
class Sample
{
}
-?>
-
There should not be empty PHPDoc blocks.
-<?php /** */
+<?php
Remove useless (semicolon) statements.
-<?php $a = 1;;
+<?php $a = 1;
-<?php echo 1;2;
+<?php echo 1;
<?php while(foo()){
- continue 1;
+ continue ;
}
Removes extra blank lines and/or blank lines following configuration.
[
'tokens' => [
0 => 'extra',
1 => 'throw',
2 => 'use',
],
]
$foo = array("foo");
-
$bar = "bar";
function foo($bar)
{
throw new \Exception("Hello!");
-
}
namespace Foo;
use Bar\Baz;
-
use Baz\Bar;
class Bar
Remove leading slashes in use
clauses.
<?php
namespace Foo;
-use \Bar;
+use Bar;
The namespace declaration line shouldn't contain leading whitespace.
<?php
- namespace Test8a;
- namespace Test8b;
+namespace Test8a;
+namespace Test8b;
Either language construct print
or echo
should be used.
[
'use' => 'echo',
]
-<?php print 'example';
+<?php echo 'example';
Operator =>
should not be surrounded by multi-line whitespaces.
<?php
-$a = array(1
-
-=> 2);
+$a = array(1 => 2);
Short cast bool
using double exclamation mark should not be used.
<?php
-$a = !!$b;
+$a = (bool)$b;
Single-line whitespace before closing semicolon are prohibited.
-<?php $this->foo() ;
+<?php $this->foo();
When making a method or function call, there MUST NOT be a space between the method or function name and the opening parenthesis.
<?php
-require ('sample.php');
-echo (test (3));
-exit (1);
-$func ();
+require('sample.php');
+echo(test(3));
+exit(1);
+$func();
There must be no space around double colons (also called Scope Resolution Operator or Paamayim Nekudotayim).
-<?php echo Foo\Bar :: class;
+<?php echo Foo\Bar::class;
There MUST NOT be spaces around offset braces.
[
'positions' => [
0 => 'inside',
1 => 'outside',
],
]
<?php
-$sample = $b [ 'a' ] [ 'b' ];
+$sample = $b['a']['b'];
There MUST NOT be a space after the opening parenthesis. There MUST NOT be a space before the closing parenthesis.
<?php
-if ( $a ) {
- foo( );
+if ($a) {
+ foo();
}
<?php
-function foo( $bar, $baz )
+function foo($bar, $baz)
{
}
Removes @param
, @return
and @var
tags that don't provide any useful information.
[
'no_superfluous_phpdoc_tags' => [
'allow_mixed' => true,
'allow_unused_params' => true,
],
]
<?php
class Foo {
/**
- * @param Bar $bar
* @param mixed $baz
*/
public function doFoo(Bar $bar, $baz) {}
}
<?php
class Foo {
/**
- * @param Bar $bar
- * @param mixed $baz
* @param string|int|null $qux
*/
public function doFoo(Bar $bar, $baz /*, $qux = null */) {}
}
Remove trailing whitespace at the end of non-blank lines.
<?php
-$a = 1;
+$a = 1;
There MUST be no trailing spaces inside comment or PHPDoc.
<?php
-// This is
-// a comment.
+// This is
+// a comment.
Removes unneeded parentheses around control statements.
[
'statements' => [
0 => 'break',
1 => 'clone',
2 => 'continue',
3 => 'echo_print',
4 => 'return',
5 => 'switch_case',
6 => 'yield',
],
]
<?php
-while ($x) { while ($y) { break (2); } }
-clone($a);
-while ($y) { continue (2); }
-echo("foo");
-print("foo");
-return (1 + 2);
-switch ($a) { case($x); }
-yield(2);
+while ($x) { while ($y) { break 2; } }
+clone $a;
+while ($y) { continue 2; }
+echo "foo";
+print "foo";
+return 1 + 2;
+switch ($a) { case $x; }
+yield 2;
Removes unneeded curly braces that are superfluous and aren't part of a control structure's body.
-<?php {
+<?php
echo 1;
-}
+
switch ($b) {
- case 1: {
+ case 1:
break;
- }
+
}
<?php
-namespace Foo {
+namespace Foo;
function Bar(){}
-}
+
Variables must be set null
instead of using (unset)
casting.
<?php
-$a = (unset) $b;
+$a = null;
Unused use
statements must be removed.
<?php
use \DateTime;
-use \Exception;
new DateTime();
In function arguments there must not be arguments with default values before non-default ones.
<?php
-function example($foo = "two words", $bar) {}
+function example($foo, $bar) {}
There should not be an empty return
statement at the end of a function.
function example($b) {
if ($b) {
return;
}
- return;
+
}
In array declaration, there MUST NOT be a whitespace before each comma.
-<?php $x = array(1 , "2");
+<?php $x = array(1, "2");
<?php
$x = [<<<EOD
foo
-EOD
- , 'bar'
+EOD, 'bar'
];
Remove trailing whitespace at the end of blank lines.
<?php
-
+
$a = 1;
Array index should always be written by using square braces.
<?php
-echo $sample{$index};
+echo $sample[$index];
Logical NOT operators (!
) should have one trailing whitespace.
<?php
-if (!$bar) {
+if (! $bar) {
echo "Help!";
}
There should not be space before or after object operators ->
and ?->
.
-<?php $a -> b;
+<?php $a->b;
Ordering use
statements.
[
'sort_algorithm' => 'alpha',
'imports_order' => [
0 => 'const',
1 => 'class',
2 => 'function',
],
]
<?php
-use const BBB;
use const AAAA;
+use const BBB;
-use Acme;
use AAC;
+use Acme;
use Bar;
-use function DDD;
use function CCC\AA;
+use function DDD;
Classes must be in a path that matches their namespace, be at least one namespace deep and the class name should match the file name.
Pint explicitly sets this to false
.
Docblocks should have the same indentation as the documented subject.
<?php
class DocBlocks
{
-/**
- * Test constants
- */
+ /**
+ * Test constants
+ */
const INDENT = 1;
}
Fixes PHPDoc inline tags.
<?php
/**
- * @{TUTORIAL}
- * {{ @link }}
+ * {@TUTORIAL}
+ * {@link}
* @inheritDoc
*/
<?php
/**
- * @{TUTORIAL}
+ * {@TUTORIAL}
* {{ @link }}
* @inheritDoc
*/
@access
annotations should be omitted from PHPDoc.
class Foo
{
/**
* @internal
- * @access private
*/
private $bar;
}
@package
and @subpackage
annotations should be omitted from PHPDoc.
<?php
/**
* @internal
- * @package Foo
- * subpackage Bar
*/
class Baz
{
Classy that does not inherit must not have @inheritdoc
tags.
<?php
-/** {@inheritdoc} */
+/** */
class Sample
{
}
class Sample
{
/**
- * @inheritdoc
+ *
*/
public function Test()
{
Annotations in PHPDoc should be ordered so that @param
annotations come first, then @throws
annotations, then @return
annotations.
[
'order' => [
0 => 'param',
1 => 'return',
2 => 'throws',
],
]
/**
* Hello there!
*
- * @throws Exception|RuntimeException foo
* @custom Test!
- * @return int Return the number of changes.
* @param string $foo
* @param bool $bar Bar
+ * @throws Exception|RuntimeException foo
+ * @return int Return the number of changes.
*/
Scalar types should always be written in the same form. int
not integer
, bool
not boolean
, float
not real
or double
.
<?php
/**
- * @param integer $a
- * @param boolean $b
- * @param real $c
+ * @param int $a
+ * @param bool $b
+ * @param float $c
*
- * @return double
+ * @return float
*/
function sample($a, $b, $c)
{
<?php
/**
* @param integer $a
- * @param boolean $b
+ * @param bool $b
* @param real $c
*/
function sample($a, $b, $c)
Annotations in PHPDoc should be grouped together so that annotations of the same type immediately follow each other, and annotations of a different type are separated by a single blank line.
[
'groups' => [
0 => [
0 => 'deprecated',
1 => 'link',
2 => 'see',
3 => 'since',
],
1 => [
0 => 'author',
1 => 'copyright',
2 => 'license',
],
2 => [
0 => 'category',
1 => 'package',
2 => 'subpackage',
],
3 => [
0 => 'property',
1 => 'property-read',
2 => 'property-write',
],
4 => [
0 => 'param',
1 => 'return',
],
],
]
<?php
/**
* Description.
- * @param string $foo
- *
*
+ * @param string $foo
* @param bool $bar Bar
+ *
* @throws Exception|RuntimeException
+ *
* @return bool
*/
function fnc($foo, $bar) {}
Single line @var
PHPDoc should have proper spacing.
-<?php /**@var MyClass $a */
+<?php /** @var MyClass $a */
$a = test();
PHPDoc summary should end in either a full stop, exclamation mark, or question mark.
<?php
/**
- * Foo function is great
+ * Foo function is great.
*/
function foo () {}
Docblocks should only be used on structural elements.
Pint explicitly sets this to false
.
Forces PHPDoc tags to be either regular annotations or inline.
[
'tags' => [
'inheritdoc' => 'inline',
],
]
<?php
/**
- * @inheritdoc
+ * {@inheritdoc}
*/
PHPDoc should start and end with content, excluding the very first and last line of the docblocks.
<?php
/**
- *
* Foo must be final class.
- *
- *
*/
final class Foo {}
The correct case must be used for standard PHP types in PHPDoc.
<?php
/**
- * @param STRING|String[] $bar
+ * @param string|string[] $bar
*
- * @return inT[]
+ * @return int[]
*/
<?php
/**
- * @param BOOL $foo
+ * @param bool $foo
*
* @return MIXED
*/
Adds or removes @test annotations from tests, following configuration.
[
'style' => 'annotation',
]
<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
-public function testItDoesSomething() {}}
+/**
+ * @test
+ */
+public function itDoesSomething() {}}
@var
and @type
annotations of classy properties should not contain the name.
final class Foo
{
/**
- * @var int $bar
+ * @var int
*/
public $bar;
/**
- * @type $baz float
+ * @type float
*/
public $baz;
}
Adjust spacing around colon in return type declarations and backed enum types.
[
'space_before' => 'none',
]
<?php
-function foo(int $a):string {};
+function foo(int $a): string {};
Inside class or interface element self
should be preferred to the class name itself.
class Sample
{
const BAZ = 1;
- const BAR = Sample::BAZ;
+ const BAR = self::BAZ;
public function getBar()
{
- return Sample::BAR;
+ return self::BAR;
}
}
Cast (boolean)
and (integer)
should be written as (bool)
and (int)
, (double)
and (real)
as (float)
, (binary)
as (string)
.
<?php
-$a = (boolean) $b;
-$a = (integer) $b;
-$a = (double) $b;
-$a = (real) $b;
+$a = (bool) $b;
+$a = (int) $b;
+$a = (float) $b;
+$a = (float) $b;
-$a = (binary) $b;
+$a = (string) $b;
<?php
-$a = (boolean) $b;
-$a = (integer) $b;
-$a = (double) $b;
+$a = (bool) $b;
+$a = (int) $b;
+$a = (float) $b;
-$a = (binary) $b;
+$a = (string) $b;
Converts explicit variables in double-quoted strings and heredoc syntax from simple to complex format (${
to {$
).
<?php
$name = 'World';
-echo "Hello ${name}!";
+echo "Hello {$name}!";
<?php
$name = 'World';
echo <<<TEST
-Hello ${name}!
+Hello {$name}!
TEST;
A return statement wishing to return void
should not return null
.
Pint explicitly sets this to false
.
A PHP file without end tag must always end with a single empty line feed.
<?php
$a = 1;
+
<?php
$a = 1;
-
There should be exactly one blank line before a namespace declaration.
-<?php namespace A {}
+<?php
+
+namespace A {}
<?php
-
namespace A{}
There MUST NOT be more than one property or constant declared per statement.
[
'elements' => [
0 => 'const',
1 => 'property',
],
]
<?php
final class Example
{
- const FOO_1 = 1, FOO_2 = 2;
- private static $bar1 = array(1,2,3), $bar2 = [1,2,3];
+ const FOO_1 = 1;
+ const FOO_2 = 2;
+ private static $bar1 = array(1,2,3);
+ private static $bar2 = [1,2,3];
}
There MUST be one use keyword per declaration.
<?php
-use Foo, Sample, Sample\Sample as Sample2;
+use Foo;
+use Sample;
+use Sample\Sample as Sample2;
<?php
-use Space\Models\ {
- TestModelA,
- TestModelB,
- TestModel,
-};
+use Space\Models\TestModelA;
+use Space\Models\TestModelB;
+use Space\Models\TestModel;
Each namespace use MUST go on its own line and there MUST be one blank line after the use statements block.
namespace Foo;
use Bar;
use Baz;
+
final class Example
{
}
namespace Foo;
use Bar;
use Baz;
-
final class Example
{
}
Single-line comments and multi-line comments with only one line of actual content should use the //
syntax.
[
'comment_types' => [
0 => 'hash',
],
]
-<?php # comment
+<?php // comment
Convert double quotes to single quotes for simple strings.
<?php
-$a = "sample";
+$a = 'sample';
$b = "sample with 'single-quotes'";
<?php
-$a = "sample";
-$b = "sample with 'single-quotes'";
+$a = 'sample';
+$b = 'sample with \'single-quotes\'';
Fix whitespace after a semicolon.
<?php
- sample(); $test = 1;
- sample();$test = 2;
- for ( ;;++$sample) {
+ sample(); $test = 1;
+ sample(); $test = 2;
+ for ( ; ; ++$sample) {
}
<?php
-for ($i = 0; ; ++$i) {
+for ($i = 0;; ++$i) {
}
Replace all <>
with !=
.
<?php
-$a = $b <> $c;
+$a = $b != $c;
Each statement must be indented.
<?php
if ($baz == true) {
- echo "foo";
+ echo "foo";
}
else {
- echo "bar";
+ echo "bar";
}
A case should be followed by a colon and not a semicolon.
<?php
switch ($a) {
- case 1;
+ case 1:
break;
- default;
+ default:
break;
}
Removes extra spaces between colon and case value.
<?php
switch($a) {
- case 1 :
+ case 1:
break;
- default :
+ default:
return 2;
}
Standardize spaces around ternary operator.
-<?php $a = $a ?1 :0;
+<?php $a = $a ? 1 : 0;
Multi-line arrays, arguments list, parameters list and match
expressions must have a trailing comma.
[
'elements' => [
0 => 'arrays',
],
]
<?php
array(
1,
- 2
+ 2,
);
Arrays should be formatted like function/method arguments, without leading or trailing single line space.
<?php
-$sample = array( );
-$sample = array( 'a', 'b' );
+$sample = array();
+$sample = array('a', 'b');
A single space or none should be around union type and intersection type operators.
try
{
new Foo();
-} catch (ErrorA | ErrorB $e) {
+} catch (ErrorA|ErrorB $e) {
echo'error';}
try
{
new Foo();
-} catch (ErrorA|ErrorB $e) {
+} catch (ErrorA | ErrorB $e) {
echo'error';}
<?php
-function foo(int | string $x)
+function foo(int|string $x)
{
}
Unary operators should be placed adjacent to their operands.
<?php
-$sample ++;
--- $sample;
-$sample = ! ! $a;
-$sample = ~ $c;
-function & foo(){}
+$sample++;
+--$sample;
+$sample = !!$a;
+$sample = ~$c;
+function &foo(){}
Visibility MUST be declared on all properties and methods; abstract
and final
MUST be declared before the visibility; static
MUST be declared after the visibility.
[
'elements' => [
0 => 'method',
1 => 'property',
],
]
<?php
class Sample
{
- var $a;
- static protected $var_foo2;
+ public $a;
+ protected static $var_foo2;
- function A()
+ public function A()
{
}
}
In array declaration, there MUST be a whitespace after each comma.
<?php
-$sample = array(1,'a',$b,);
+$sample = array(1, 'a', $b, );