6

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

/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>'

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

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

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

hakre
  • 193,403
  • 52
  • 435
  • 836
Pattie Reaves
  • 175
  • 1
  • 3
  • 11

2 Answers2

24

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.

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

PEAR has a config setting which tells it where that PHP executable is installed. You can see this value by running pear config-show and 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.

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

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:

$ 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.

Greg Sherwood
  • 6,992
  • 2
  • 29
  • 24
  • I have the right line at the top of my phpcs file — it matches the location of my php binary — but it still is not running correctly. It does run correctly if I do php phpcs --version from parent directory. – Pattie Reaves Aug 03 '12 at 15:29
  • The only way I can get PHPCS to output what you see is to actually remove that first line, or to put some content before it (the #! has to be the very first content of the file). Is it possible that you have some weird characters at the top from a bad install (or a newline even)? – Greg Sherwood Aug 03 '12 at 23:15
  • This is what I see: `[pattie@Pattie-Reavess-MacBook-Pro ~]$ head -n 1 /Applications/drupal/php/bin/phpcs #!/Applications/drupal/php/bin/php` – Pattie Reaves Aug 06 '12 at 14:11
  • Looks ok to me. If you can run `/Applications/drupal/php/bin/php /Applications/drupal/php/bin/phpcs --version` and it works, then I have no idea what is wrong. That's exactly what the script should be doing. (could also try `php /Applications/drupal/php/bin/phpcs --version`) I'd probably check executable flags on everything, but you've already said `php phpcs --version` works, so I doubt that is the problem. – Greg Sherwood Aug 08 '12 at 05:26
  • 1
    After digging in some more, its definitely a problem with my bitnami compilation of PHP and not how PEAR or PHP_CodeSniffer is installed. – Pattie Reaves Aug 08 '12 at 14:22
  • I had a similar problem, and this fixed it. Thanks! – Vlad Preda Jan 30 '13 at 12:58
  • @LucianoMammino etc: check out the other answer to this question. it worked for me after a reinstall did not. thanks all! – Doug Johnson Jul 08 '13 at 17:37
  • It solved my issue, thankyou! – Rahul Gandhi Feb 07 '22 at 17:15
7

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:

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

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.

Mike Ryan
  • 121
  • 2
  • 4