PHPCS没有在命令行上运行?

时间:2012-08-02 21:47:56

标签: php codesniffer

当我输入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版本相同

2 个答案:

答案 0 :(得分:19)

当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命令。

答案 1 :(得分:4)

我在这方面挣扎了一段时间但终于想通了 - 如果您直接调用的PHP命令是真实可执行文件的shell脚本包装器,您需要编辑phpcs脚本的第一行以通过调用PHP来实现/usr/bin/env

#!/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。