4

当我输入 phpcs --version 时,我得到的不是版本号,而是这样的:

/Applications/drupal/php/bin/phpcs: line 2: ?php: No such file or directory
/Applications/drupal/php/bin/phpcs: line 3: /Applications: is a directory
/Applications/drupal/php/bin/phpcs: line 4: Applications: command not found
/Applications/drupal/php/bin/phpcs: line 5: Applications: command not found
/Applications/drupal/php/bin/phpcs: line 6: Applications: command not found
/Applications/drupal/php/bin/phpcs: line 7: Applications: command not found
/Applications/drupal/php/bin/phpcs: line 8: Applications: command not found
/Applications/drupal/php/bin/phpcs: line 9: Applications: command not found
/Applications/drupal/php/bin/phpcs: line 10: Applications: command not found
/Applications/drupal/php/bin/phpcs: line 11: syntax error near unexpected token `newline'
/Applications/drupal/php/bin/phpcs: line 11: ` * @author    Greg Sherwood <gsherwood@squiz.net>'

看起来它没有正确读取 PHP。我配置错了什么?

我所做的只是 sudo pear install PHP_CodeSniffer。当我再次运行它时,我得到:

pear/PHP_CodeSniffer 已安装,与发布的版本 1.3.5 相同

4

2 回答 2

22

当 PEAR 安装 PHP_CodeSniffer 时,它会更改主 phpcs 脚本中的第一行,以便该#!行指向系统上的 PHP 可执行文件。

所以在安装之前,该行看起来像这样:#!@php_bin@安装之后,它看起来像这样(取决于 PHP 的安装位置):#!/usr/bin/php

PEAR 有一个配置设置,它告诉它 PHP 可执行文件的安装位置。您可以通过运行pear config-show并查找 的值来查看该值PHP CLI/CGI binary (php_bin)。您需要确保此值实际上是 PHP 在您的系统上的位置,否则所有脚本安装(如 PHPUnit)都会有类似的问题。

检查此值的最佳方法是运行which php并为 PEAR 配置变量设置该值。然后重新安装 PHP_CodeSniffer 以便再次完成替换。

所以对于我的系统,我会这样做:

$ which php
/usr/bin/php
$ sudo pear config-set php_bin /usr/bin/php
config-set succeeded
$ sudo pear uninstall php_codesniffer
uninstall ok: channel://pear.php.net/PHP_CodeSniffer-1.3.5
$ sudo pear install php_codesniffer
downloading PHP_CodeSniffer-1.3.5.tgz ...
Starting to download PHP_CodeSniffer-1.3.5.tgz (345,113 bytes)
......................................................................done: 345,113 bytes
install ok: channel://pear.php.net/PHP_CodeSniffer-1.3.5

如果一切顺利,您应该会#!在新的 phpcs 文件中看到正确的行:

$ which phpcs
/usr/local/bin/phpcs
$ head -n 1 /usr/local/bin/phpcs
#!/usr/bin/php

如果看起来正确,您将能够毫无问题地运行 phpcs 命令。

于 2012-08-02T23:13:26.787 回答
7

我为此苦苦挣扎了一段时间,但最终弄明白了——如果您直接调用的 PHP 命令是围绕真实可执行文件的 shell 脚本包装器,则需要编辑 phpcs 脚本的第一行以通过/usr/调用 PHP箱/环境

#!/usr/bin/env /Applications/acquia-drupal/php5_3/bin/php

要不就

#!/usr/bin/env php

搜索者的完整上下文 - 修复之前:

~$ head -n 1 pear/bin/phpcs 
#!/Applications/acquia-drupal/php5_3/bin/php
~$ phpcs --version
/Users/mryan/pear/bin/phpcs: line 2: ?php: No such file or directory
/Users/mryan/pear/bin/phpcs: line 3: /Applications: is a directory
/Users/mryan/pear/bin/phpcs: line 4: Desktop: command not found
/Users/mryan/pear/bin/phpcs: line 5: Desktop: command not found
/Users/mryan/pear/bin/phpcs: line 6: Desktop: command not found
/Users/mryan/pear/bin/phpcs: line 7: Desktop: command not found
/Users/mryan/pear/bin/phpcs: line 8: Desktop: command not found
/Users/mryan/pear/bin/phpcs: line 9: Desktop: command not found
/Users/mryan/pear/bin/phpcs: line 10: Desktop: command not found
/Users/mryan/pear/bin/phpcs: line 11: syntax error near unexpected token `newline'
/Users/mryan/pear/bin/phpcs: line 11: ` * @author    Greg Sherwood <gsherwood@squiz.net>'
~$ /Applications/acquia-drupal/php5_3/bin/php pear/bin/phpcs --version
PHP_CodeSniffer version 1.4.5 (stable) by Squiz Pty Ltd. (http://www.squiz.com.au)

修复后:

~$ head -n 1 pear/bin/phpcs
#!/usr/bin/env php
~$ phpcs --version
PHP_CodeSniffer version 1.4.5 (stable) by Squiz Pty Ltd. (http://www.squiz.com.au)

现在我可以在 PhpStorm 中配置 CodeSniffer。

于 2013-04-05T18:57:14.387 回答