phpmacosinstallationcoding-stylephpcs

How to install PHP_CodeSniffer in macOSx?


I tried to install phpcs in the macOS Catalina but I was getting multiple errors. So in my answer, I shared the steps to install phpcs easily.


Solution

  • I tried the following steps and it's worked out. My macOS version is 10.15.4 (Catalina) and PHP version is 7.2.

    $ sudo cp /etc/php.ini.default /etc/php.ini;
    

    Then install go-pear

    $ curl -O https://pear.php.net/go-pear.phar
    $ php -d detect_unicode=0 go-pear.phar
    

    Here you'll have some question to install the pear.

    1. So type 1, and then press Enter.
    2. Enter /usr/local/pear
    3. Press Enter.
    4. Then, you will need to change the Binaries directory.
    5. Type 4, and then press Enter.
    6. Enter /usr/local/bin
    7. Press Enter.

    After installing pear, I got the following warning.

    ******************************************************************************
    WARNING: Cannot write to /etc/php.ini, but php.ini was successfully created
    at </usr/local/bin/php.ini-gopear>. Please replace the file </etc/php.ini> with
    </usr/local/bin/php.ini-gopear> or modify your php.ini by adding:
    
    include_path=".:/usr/local/bin/share/pear"
    

    So edited the /etc/php.ini file and added the above (include_path=".:/usr/local/bin/share/pear") line.

    After that install PHP_CodeSniffer -

    $ sudo pear upgrade-all;\n
    $ sudo pear install PHP_CodeSniffer;\n
    $ phpcs --version
    

    Finally got it -

    PHP_CodeSniffer version 3.5.5 (stable) by Squiz (http://www.squiz.net)