indentfixable
Ensure that the indentation is consistent
Options
type
The type of whitespace to use
Type: string
Default: space
Possible values: tab
, space
length
The length of the indent
Type: integer
Default: 4
Examples
- Examples of correct code for this rule using default options
Usegroup, classes, methods, switch, if, arrays and comments
php
<?php
use Root\{Common, Util, Extra};
use Root\Exceptions\CustomException;
use Symfony\Component\Console\{
Command\Command,
Input\InputInterface,
Input\InputOption,
Output\OutputInterface,
Logger\ConsoleLogger
};
class Test
{
function __construct()
{
$this->hello();
}
function hello(
)
{
echo 'hello';
}
function hello2()
{
if (TRUE) {
echo 'hello';
} else {
echo 'bye';
}
while (TRUE) {
echo 'hello';
}
do {
echo 'hello';
} while (TRUE);
}
function hello3()
{
switch ($hello) {
case 'hello':
break;
}
}
}
class Test2
{
function __construct()
{
// $this->open();
}
public function open()
{
if (TRUE) echo 'hello';
foreach ($tokens as $token) echo $token;
}
/**
* This is a comment 1.
* This is a comment 2.
* This is a comment 3.
* This is a comment 4.
*/
public function close()
{
if (TRUE) {
if (TRUE) {
} else if (FALSE) {
foreach ($tokens as $token) {
switch ($token) {
case '1':
case '2':
if (true) {
if (false) {
if (false) {
if (false) {
echo 'hello';
}
}
}
}
break;
case '5':
break;
}
do {
while (true) {
foreach ($tokens as $token) {
for ($i = 0; $i < $token; $i++) {
echo 'hello';
}
}
}
} while (true);
}
}
}
}
/*
This is another c style comment 1.
This is another c style comment 2.
This is another c style comment 3.
This is another c style comment 4.
This is another c style comment 5.
*/
/*
*
*
*
*/
/**
*/
/*
This comment has a newline in it.
*/
public function read()
{
echo 'hello';
$array = array(
'this',
'that' => array(
'hello',
'hello again' => array(
'hello',
),
),
);
}
}
abstract class Test3
{
public function parse()
{
foreach ($t as $ndx => $token) {
if (is_array($token)) {
echo 'here';
} else {
$ts[] = array("token" => $token, "value" => '');
$last = count($ts) - 1;
switch ($token) {
case '(':
if ($last >= 3 &&
$ts[0]['token'] != T_CLASS &&
$ts[$last - 2]['token'] == T_OBJECT_OPERATOR &&
$ts[$last - 3]['token'] == T_VARIABLE ) {
if (true) {
echo 'hello';
}
}
array_push($braces, $token);
break;
}
}
}
}
}
$array = [
];
$another_array = array(
);
Functions, binary expressions, inline comment, multi-line strings, heredoc
php
<?php
/**
* This is a test function
*
* @param int $repeat The number of times to repeat the string
* @return string The repeated string
*/
function test($repeat)
{
if(true){
$o = <<<EOF
this is some text
this is some text
this is some text
this is some text
this is some text
this is some text
EOF;
}
return $o;
}
$repeated = functionIndent(
2334,
566,
"string"
);
if ($a === true || $a === true || $a === true || $a === true ||
$a === true || $a === true || $a === true || $a === true) {
echo 'hello';
}
if ($true) {
/* First comment line
*
* Comment test here
* Comment test here
*
*/
/* First comment line
*
* Comment test here
* Comment test here
*
this si something */
}
$test = $variable === true ? 'True' : 'False';
$ternary =
$test === 'True'
?
'Go Ahead' :
'Stop';
$this->test(
$variable === 'testing'
? 'debug' :
'production');
$ternary =
$test === 'True'
? $value === 'True'
? 'Go Ahead' :
'Stop'
: 'Stop';
function testNoop()
{
/* taken from http://de3.php.net/manual/en/reserved.php */
# $m[] = 'declare';
/* taken from http://de3.php.net/manual/en/reserved.php */
# $m[] = 'declare';
}
function testNoop2(){// noop function
}
foreach ($elements as $element) {
if ($something) {
// Do IF.
} else if ($somethingElse) {
// Do ELSE.
}
}
foreach ($elements as $element) {
// Foreach with a comment
echo ($element) ? 'true' : 'false';
}
if ($condition) {
echo "This is a long
string that spans $numLines lines
without indenting.
";
}
if ($condition) {
echo
'This is a long
string that spans multiple lines
with indenting.
';
}
if ($condition) {
echo 'This is a long
string that spans multiple lines
with indenting.';
}
Switch statements
php
<?php
switch ($foo) {
case 1:
switch ($bar) {
default:
echo $string[1];
}
break;
}
function temp($foo, $bar) {
switch ($foo) {
case 1:
switch ($bar) {
default:
return $foo;
}
break;
}
}
switch ($foo) {
case 1:
switch ($bar) {
default:
if ($something) {
echo $string[1];
} else if ($else) {
switch ($else) {
default:
}
}
}
break;
}
switch ($name) {
case "1":
case "2":
case "3":
return true;
}
switch ($name) {
case "1":
case "2":
case "3":
default :
return true;
}
switch ($foo) {
case 1:
switch ($bar) {
default:
if ($something) {
echo $string[1];
} else if ($else) {
switch ($else) {
case 1:
// Do something.
break;
default:
// Do something.
break;
}
}
}
break;
case 2:
// Do something;
break;
}
switch ($httpResponseCode) {
case 100:
case 101:
case 102:
exit;
default:
exit;
}
Function call argument, properties access
php
<?php
$object
?->setBar($foo)
?->setFoo($bar);
$someObject?->someFunction("some", "parameter")
->someOtherFunc(23, 42)?->
someOtherFunc2($one, $two)
->someOtherFunc3(23, 42)
?->andAThirdFunction();
$object
?->setBar($foo)
?->setFoo($bar);
$someObject?->someFunction("some", "parameter")
->someOtherFunc(
23,
42
)?->someOtherFunc2($one, $two)
->someOtherFunc3(23, 42)
?->andAThirdFunction();
$someObject
->startSomething(paramName: $value)
->someOtherFunc(nameA: 23, nameB: 42)
->endSomething($value, name: $value)
->endEverything();
Try/catch statements
php
<?php
try
{
echo inverse(0) . "\n";
}
catch (ExceptionType1 $e) {
echo inverse(5) . "\n";
} catch (ExceptionType2 $e) {
error_log($e->getMessage());
}
finally {
echo "Final Step";
}
try {
echo inverse(0) . "\n";
} catch (ExceptionType1 $e) {
echo inverse(5) . "\n";
}
try { echo inverse(0) . "\n";
} catch (ExceptionType1 $e) { echo inverse(5) . "\n";
}
Match expression
php
<?php
match ($food) { 'apple' => 'This food is an apple',
'orange' => 'This food is a orange',
'cake' => 'This food is a cake',
};
match($month_name) {
'jan' => 31,
'feb' => is_leap_year($year) ? 29 : 28,
'mar' => 31,
'apr' => 30,
'may' => 31,
'jun' => 30,
'jul' => 31,
'aug' => 31,
'sep' => 30,
'oct' => 31,
'nov' => 30,
'dec' => 31,
default => throw new InvalidArgumentException("Invalid month"),
};
enum Color {
case Red;
case Green;
case Blue;
}
enum continents {
case Asia;
case Europe;
case Africa;
case NorthAmerica;
case SouthAmerica;
case Antarctica;
case Australia;
}
Attributes
php
<?php
#[ORM\Entity]
#[ORM\Table(name: 'symfony_demo_tag')]
class Tag implements \JsonSerializable
{
#[ORM\Id]
#[ORM\GeneratedValue]
private ?int $id = null;
#[ORM\Column(type: Types::STRING, unique: true)]
private readonly string $name;
#[Deprecated(
reason: 'since Symfony 5.2, use setPublic() instead',
replacement: '%class%->setPublic(!%parameter0%)'
)]
public function __construct(string $name)
{
$this->name = $name;
}
#[
ORM\GeneratedValue(
value: '44577',
outcome: 'outcome value'
),
ORM\Column(),
ORM\CustomIdGenerator(class: test),
ORM\Id
]
public function updateToken(string $series, #[\SensitiveParameter] string $token_value, \DateTime $last_used)
{
echo "token UPDATE";
}
}
#[FooAttribute]
function foo_func(#[FooParamAttrib('Foo1')] $foo)
{
}
#[FooAttribute('hello')]
#[BarClassAttrib(42)]
class Foo
{
#[ConstAttr]
#[FooAttribute(null)]
private const FOO_CONST = 28;
private const BAR_CONST = 28;
#[PropAttr(Foo::BAR_CONST, 'string')]
private string $foo;
#[SomeoneElse\FooMethodAttrib]
public function getFoo(#[FooClassAttrib(28) ] $a): string
{
}
}
#[Attribute]
class FooAttribute
{
public function __construct(?string $param1 = null)
{
}
}
#[Attribute]
class ClassAttrib
{
public function __construct(int $index)
{
}
}
- Examples of incorrect code for this rule using default options
Usegroup, classes, methods, switch and if statements
php
<?php
use Root\{Common, Util, Extra};
use Root\Exceptions\CustomException;
use Symfony\Component\Console\{
Command\Command,
Input\InputInterface,
Input\InputOption,
Output\OutputInterface,
Logger\ConsoleLogger
};
class Test {
public function close()
{
if (TRUE) {
if (TRUE) {
} else if (FALSE) {
foreach ($tokens as $token) {
switch ($token) {
case '1':
case '2':
if (true) {
if (false) {
if (false) {
if (false) {
echo 'hello';
}
}
}
}
break;
case '5':
break;
}
do {
while (true) {
foreach ($tokens as $token) {
for ($i = 0; $i < $token; $i++) {
echo 'hello';
}
}
}
} while (true);
}
}
}
}
}
Control structures, functions, binary expressions, inline comment, multi-line strings, heredoc
php
<?php
/**
* This is a test function
*
* @param int $repeat The number of times to repeat the string
* @return string The repeated string
*/
function test($repeat)
{
if(true){
$o = <<<EOF
this is some text
this is some text
this is some text
this is some text
this is some text
this is some text
EOF;
}
return $o;
}
if ($a === true || $a === true || $a === true || $a === true ||
$a === true || $a === true || $a === true || $a === true) {
echo 'hello';
}
if ($true) {
/* First comment line
*
* Comment test here
* Comment test here
*
*/
/* First comment line
*
* Comment test here
* Comment test here
*
this si something */
}
function testNoop()
{
/* taken from http://de3.php.net/manual/en/reserved.php */
# $m[] = 'declare';
/* taken from http://de3.php.net/manual/en/reserved.php */
# $m[] = 'declare';
}
foreach ($elements as $element) {
if ($something) {
// Do IF.
} else if ($somethingElse) {
// Do ELSE.
}
}
if ($condition) {
echo "This is a long
string that spans $numLines lines
without indenting.
";
}
Match and enum expressions
php
<?php
match ($food) { 'apple' => 'This food is an apple',
'orange' => 'This food is a orange',
'cake' => 'This food is a cake',
};
match($month_name) {
'jan' => 31,
'feb' => is_leap_year($year) ? 29 : 28,
'mar' => 31,
'apr' => 30,
'may' => 31,
'jun' => 30,
'jul' => 31,
'aug' => 31,
'sep' => 30,
'oct' => 31,
'nov' => 30,
'dec' => 31,
default => throw new InvalidArgumentException("Invalid month"),
};
enum Color {
case Red;
case Green;
case Blue;
}
enum continents { case Asia;
case Europe;
case Africa; case NorthAmerica;
case SouthAmerica;case Antarctica;
case Australia;}
Attributes
php
<?php
#[ORM\Entity]
#[ORM\Table(name: 'symfony_demo_tag')]
class Tag implements \JsonSerializable
{
#[ORM\Id]
#[ORM\GeneratedValue]
private ?int $id = null;
#[ORM\Column(type: Types::STRING, unique: true)]
private readonly string $name;
#[Deprecated(
reason: 'since Symfony 5.2, use setPublic() instead',
replacement: '%class%->setPublic(!%parameter0%)'
)]
public function __construct(string $name)
{
$this->name = $name;
}
#[
ORM\GeneratedValue(
value: '44577',
outcome: 'outcome value'
),
ORM\Column(),
ORM\CustomIdGenerator(class: test),
ORM\Id
]
public function updateToken(string $series, #[\SensitiveParameter] string $token_value, \DateTime $last_used)
{
echo "token UPDATE";
}
}
#[FooAttribute]
function foo_func(#[FooParamAttrib('Foo1')] $foo)
{
}
#[FooAttribute('hello')]
#[BarClassAttrib(42)]
class Foo
{
#[ConstAttr]
#[FooAttribute(null)]
private const FOO_CONST = 28;
private const BAR_CONST = 28;
#[PropAttr(Foo::BAR_CONST, 'string')]
private string $foo;
#[SomeoneElse\FooMethodAttrib]
public function getFoo(#[FooClassAttrib(28) ] $a): string
{
}
}
#[Attribute]
class FooAttribute
{
public function __construct(string $param1)
{
}
}
type
- Examples of correct code for this rule using type option
A PHP file indented with tabs
php
<?php
/* taqwim "psr/indent" :{ type: "tab", length: 1 } */
use Symfony\Component\Console\{
Command\Command,
Input\InputInterface,
Input\InputOption,
Output\OutputInterface,
Logger\ConsoleLogger
};
class Test {
public function close()
{
if (TRUE) {
if (TRUE) {
} else if (FALSE) {
foreach ($tokens as $token) {
switch ($token) {
case '1':
case '2':
if (true) {
if (false) {
if (false) {
if (false) {
echo 'hello';
}
}
}
}
break;
case '5':
break;
}
do {
while (true) {
foreach ($tokens as $token) {
for ($i = 0; $i < $token; $i++) {
echo 'hello';
}
}
}
} while (true);
}
}
}
}
}
- Examples of incorrect code for this rule using type option
A PHP file indented with tabs
php
<?php
/* taqwim "psr/indent" :{ type: "space", length: 1 } */
use Symfony\Component\Console\{
Command\Command,
Input\InputInterface,
Input\InputOption,
Output\OutputInterface,
Logger\ConsoleLogger
};
class Test {
public function close()
{
if (TRUE) {
if (TRUE) {
} else if (FALSE) {
foreach ($tokens as $token) {
switch ($token) {
case '1':
case '2':
if (true) {
if (false) {
if (false) {
if (false) {
echo 'hello';
}
}
}
}
break;
case '5':
break;
}
do {
while (true) {
foreach ($tokens as $token) {
for ($i = 0; $i < $token; $i++) {
echo 'hello';
}
}
}
} while (true);
}
}
}
}
}