php PHPCS 不在命令行上运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11786299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:05:17  来源:igfitidea点击:

PHPCS not running on the command line?

phpcodesniffer

提问by Pattie Reaves

When I type in phpcs --version, instead of getting the version number, I get something like this:

当我输入 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 <[email protected]>'

It looks like its not reading PHP correctly. What did I misconfigure?

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

All I did was sudo pear install PHP_CodeSniffer. When I run it again, I get:

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

pear/PHP_CodeSniffer is already installed and is the same as the released version 1.3.5

pear/PHP_CodeSniffer 已经安装,和发布的版本1.3.5一样

回答by Greg Sherwood

When PEAR does an install of PHP_CodeSniffer, it changes the first line in the main phpcs script so that the #!line points to the PHP executable on your system.

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

So before install, the line looks like this: #!@php_bin@and after install, it will look something like this (depending on where PHP is installed): #!/usr/bin/php

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

PEAR has a config setting which tells it where that PHP executable is installed. You can see this value by running pear config-showand looking for the value of PHP CLI/CGI binary (php_bin). You need to make sure this value is actually the location of PHP on your system or else all installs of scripts (like PHPUnit) will have a similar problem.

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

The best way to check this value is to run which phpand set that value for the PEAR config variable. Then reinstall PHP_CodeSniffer so the replacement is done again.

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

So for my system, I'd do this:

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

$ 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

If all goes well, you should see the correct #!line in your new phpcs file:

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

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

If that looks correct, you'll be able to run the phpcs command without problems.

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

回答by Mike Ryan

I struggled with this for a while but finally figured it out - if the PHP command you're invoking directly is a shell script wrapper around the real executable, you need to edit the first line of the phpcs script to invoke PHP via /usr/bin/env:

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

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

or, just

要不就

#!/usr/bin/env php

Full context for searchers - before the fix:

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

~$ 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 <[email protected]>'
~$ /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)

After fix:

修复后:

~$ 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)

And now I can configure CodeSniffer in PhpStorm.

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