代码之家  ›  专栏  ›  技术社区  ›  Hammerite

为什么lint不告诉我解析错误的行号和性质?

  •  16
  • Hammerite  · 技术社区  · 14 年前

    我从Windows批处理文件调用php lint,如下所示:

    @echo off
    for %%f in (*.php) do php -l %%f
    

    当文件包含语法错误时,它只输出 Errors parsing xxx.php . 有没有办法让它告诉我错误的性质,以及它在哪一行?也许是另一个开关?

    3 回复  |  直到 6 年前
        1
  •  35
  •   Evan Donovan    6 年前

    如果您得到“错误解析foo.php”消息而没有任何详细信息,那么运行php lint时,此代码将显示错误:

    php -d display_errors=1 -l foo.php

    谢谢查尔斯!

    例子:

    [somewhere]# php -l submit.php
    
    Errors parsing submit.php
    
    [somewhere]# php -d display_errors=1 -l submit.php
    
    Parse error: syntax error, unexpected T_VARIABLE in submit.php on line 226
    Errors parsing submit.php
        2
  •  21
  •   Hammerite    14 年前

    我已经接受了查尔斯的回答,但我想我应该添加一些细节,因为我必须做一些额外的狩猎来找出该做什么。

    问题是我没有看到 stderr 输出,所以我开始添加 2>&1 相关命令结束。这仍然没有帮助,所以我意识到问题是PHP根本没有输出stderr的东西。我进入php安装目录,在php.in i中查找,发现默认情况下, display_errors Off . 把它改成 On 现在它开始工作了。

        3
  •  0
  •   Charles    14 年前

    您使用的是什么版本的PHP?从5.3开始,行数包含在lint输出中:

    [charles@server ~]$ cat syntax_error.php
    <?php
    echo "This line is legal\n";
    echo I'm a syntax error!\n;
    echo "This line never gets reached.\n"
    
    [charles@server ~]$ php -l syntax_error.php
    PHP Parse error:  syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting ',' or ';' in syntax_error.php on line 3
    
    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting ',' or ';' in syntax_error.php on line 3
    Errors parsing syntax_error.php
    [charles@server ~]$
    

    错误出现两次,因为它将同时指向stdout和stderr。这是一个 长的 自从我在Windows上处理批处理文件以来,也许您使用的版本只在stderr上发出错误,而批处理文件正在丢弃stderr的输出?