Setting up linter-phpcs using Composer-PHP-Codesniffer and WPCS


#1

Hey guys, can someone please give me a hand getting linter-phpcs and the WPCS installed on my new Mac? On my old machine I used Homebrew to install php_codesniffer, but this no longer works, and I’ve been recommended to use Composer instead.

I’m trying to follow a tutorial by Jason Reznick.

I ran the script below to install composer -

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

Then I ran the script below to move composer.phar into my path -

mv composer.phar /usr/local/bin/composer

I now have a new directory /Users/paulcarter/.composer/

I then ran the following script to install PHP Codesniffer -

composer global require "squizlabs/php_codesniffer=*"

Where I’m stuck is adding the phpcs binary to my PATH.

Currently they’re located in /Users/paulcarter/.composer/vendor/bin

I tried just physically dragging phpcs and phpcbf into /usr/local/bin but when I run phpcs -i I get an error message -bash: phpcs: command not found, so I’m guessing that this is not the way to move phpcs into my PATH.

I also tried opening a new Terminal window in /Users/paulcarter/.composer/vendor/bin/ then running -

Pauls-MBP:bin paulcarter$ mv phpcs /usr/local/bin/phpcs.

This moved phpcs into the folder I wanted, but running phpcs -i still gives the same error.

Unix is not my strong suit so any help would be greatly appreciated.

Paul


#2

What do you see when you run echo $path?


#3

Hey DamnedScholar, thanks for taking a look; it was you who helped me get this setup on my old machine in 2016.

Last login: Fri Sep 21 09:17:19 on console
Pauls-MBP:~ paulcarter$ echo $path
Pauls-MBP:~ paulcarter$

#4

Maybe OS X requires that the variable be capitalized (my Linux shell doesn’t). Try echo $PATH.

Here’s a tutorial about how to work with the PATH on Mac.


#5

Thanks for the tutorial, I’ll check it out over the weekend.

Last login: Fri Sep 21 13:10:13 on ttys000
Pauls-MBP:~ paulcarter$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
Pauls-MBP:~ paulcarter$

#6

Check the permissions on phpcs with this command:

ls -l /usr/local/bin

#7
Pauls-MBP:~ paulcarter$ ls -l /usr/local/bin
total 4224
lrwxr-xr-x  1 paulcarter  wheel       71 17 Sep 15:27 apm -> /Applications/Atom.app/Contents/Resources/app/apm/node_modules/.bin/apm
lrwxr-xr-x  1 paulcarter  wheel       53 17 Sep 15:27 atom -> /Applications/Atom.app/Contents/Resources/app/atom.sh
-rwxr-xr-x  1 paulcarter  staff  1875611 19 Sep 14:52 composer
lrwxr-xr-x@ 1 paulcarter  wheel       38 20 Sep 08:53 phpcs -> ../squizlabs/php_codesniffer/bin/phpcs
Pauls-MBP:~ paulcarter$

#8

Is ../squizlabs/php_codesniffer/bin/phpcs the correct path to the actual file? I don’t think it is. Based on what you’ve said, I suspect that it’s /Users/paulcarter/.composer/vendor/squizlabs/php_codesniffer/bin/phpcs. What we’re seeing there is that apm, atom, and phpcs are all symbolic links, not actual executables. In the case of the first two, the symbolic link target is an absolute path, but phpcs has a relative path and I think that your moving of the file is what broke the connection. I think that you should move the file back and add /Users/paulcarter/.composer/vendor/bin to your PATH.


#9

Hey DamnedScholar, thanks for the link to that tutorial it was exactly what was required. I moved the phpcs binary back and then ran nano ~/.bash_profile

I set the path as /Users/paulcarter/.composer/vendor/bin and now when I run echo $PATH I get -

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/paulcarter/.composer/vendor/bin

Now when I run phpcs -i I get -

The installed coding standards are PEAR, Zend, PSR2, MySource, Squiz, PSR1 and PSR12

I went ahead and installed WPCS and configured linter-phpcs in Atom; so far so good.

Interestingly, when I run ls -l /usr/local/bin I see the same information regarding phpcs -

lrwxr-xr-x@ 1 paulcarter wheel 38 20 Sep 08:53 phpcs -> ../squizlabs/php_codesniffer/bin/phpcs

Since I’ve added /Users/paulcarter/.composer/vendor/bin to my PATH I decided to just delete the copy of phpcs from /usr/local/bin

I’m still not 100% in understanding how/why I broke the connection to the file by moving it. I’m guessing that moving executables is not the same as moving other file types??

Once again, thank you so much for your help; more valuable lessons learnt and I feel much more comfortable tackling this second time round.

Best regards,

Paul


#10

The phpcs you moved isn’t an executable. It’s a symbolic link that points at an executable. Since the link has a relative address, its location is essential to it working.


#11

Ahh, thank you, that makes sense.

Paul