There are a lot of "coding tools standards" for PHP but I have been trying with phpcbf and php-cs-fixer but they gave me not good results.
For example (with bad...very bad indentation):
<?php
// A example with a horrific indentation
$title = 'This is the title';
$rows = array(array('aaa', 'bbb', 'ccc'),
array('ddd', 'eee', 'fff'),
array('ggg', 'hhh', 'iii'));
function print_rows($rows) {
foreach ($rows as $row) {
?>
<tr>
<?php
foreach ($row as $cell) {
?>
<td><?=$cell?></td>
<?php
}
?>
</tr>
<?php
}
}
?>
<html>
<head>
<title>A example with a horrific indentation
</title>
</head>
<body>
<h1>
<?php
echo $title;?></h1>
<table border="1">
<?php print_rows($rows);?>
</table>
</body>
</html>
The expected result should be (or near to this):
<?php
// A example with a horrific indentation
$title = 'This is the title';
$rows = array(
array('aaa', 'bbb', 'ccc'),
array('ddd', 'eee', 'fff'),
array('ggg', 'hhh', 'iii'));
function print_rows($rows) {
foreach ($rows as $row) {
?>
<tr>
<?php
foreach ($row as $cell) {
?>
<td><?=$cell?></td>
<?php
}
?>
</tr>
<?php
}
}
?>
<html>
<head>
<title>A example with a horrific indentation</title>
</head>
<body>
<h1>
<?php
echo $title;
?>
</h1>
<table border="1">
<?php print_rows($rows);?>
</table>
</body>
</html>
But with php-cs-fixer the output is:
$ php-cs-fixer fix example.php --rules=@PSR2
It is:
<?php
// A example with a horrific indentation
$title = 'This is the title';
$rows = array(array('aaa', 'bbb', 'ccc'),
array('ddd', 'eee', 'fff'),
array('ggg', 'hhh', 'iii'));
function print_rows($rows)
{
foreach ($rows as $row) {
?>
<tr>
<?php
foreach ($row as $cell) {
?>
<td><?=$cell?></td>
<?php
} ?>
</tr>
<?php
}
}
?>
<html>
<head>
<title>A example with a horrific indentation
</title>
</head>
<body>
<h1>
<?php
echo $title;?></h1>
<table border="1">
<?php print_rows($rows);?>
</table>
</body>
</html>
And with phpcbf is:
<?php
// A example with a horrific indentation
$title = 'This is the title';
$rows = array(array('aaa', 'bbb', 'ccc'),
array('ddd', 'eee', 'fff'),
array('ggg', 'hhh', 'iii'));
function print_rows($rows)
{
foreach ($rows as $row) {
?>
<tr>
<?php
foreach ($row as $cell) {
?>
<td><?=$cell?></td>
<?php
}
?>
</tr>
<?php
}
}
?>
<html>
<head>
<title>A example with a horrific indentation
</title>
</head>
<body>
<h1>
<?php
echo $title;?></h1>
<table border="1">
<?php print_rows($rows);?>
</table>
</body>
</html>
These two tools only format/indent the PHP code but they do not any thing with the inline html.