dongzou7134 2019-06-03 13:49
浏览 54

是否有用于PHP编码标准的命令行(GNU / Linux)工具(使用html内联)?

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
    • ¥500 52810做蓝牙接受端
    • ¥15 基于PLC的三轴机械手程序
    • ¥15 多址通信方式的抗噪声性能和系统容量对比
    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作