Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use phpcs in sublime to learn coding standards?

I want to learn to code PHP to standard. I found this article and (on its advice) installed phpcs in sublime text 2. If I go to tools -->phpcode sniffer -->sniff this file it will find syntax errors but not coding standard deviations. (Here is the code I am trying to sniff, which I think has many deviations from standard https://codereview.stackexchange.com/questions/57045/simple-wrapper-for-an-api).

The docs say to configure phpcs to use a coding standard via phpcs_additional_args. If I go to sublimetext2->preferences->package settings->phpcode sniffer->settings user I see the following code:

   "phpcs_additional_args": {
        "--standard": "PSR2",
        "-n": ""
    },

Here is my full config file.

Do I have the workflow right? If so, why isn't it finding my mistakes? How do I use phpcs to teach myself coding standards? Even knowing that I am doing the correct steps but the plugin is not outputting the coding standard marks would be helpful.

like image 775
bernie2436 Avatar asked Jan 10 '23 04:01

bernie2436


2 Answers

Based on your configuration file, there are some things you must fix:

Following instructions are for linux (ubuntu), or mac

  1. Ensure you have installed PEAR
  2. Install php code sniffer

    pear install PHP_CodeSniffer

    reload your bash or zsh and then execute command, if you don't know how to do this, just restart your terminal app

    which phpcs

    copy the output and paste to "phpcs_executable_path": at phpcs config

    currently, you're pointing "phpcs_executable_path": to php, not phpcs, so thats why its doesn't work

  3. Install php mess detector

    pear channel-discover pear.phpmd.org pear channel-discover pear.pdepend.org pear install pdepend/PHP_Depend pear install --alldeps phpmd/PHP_PMD

    do like phpcs above

    reload enviroment and execute which phpmd, then fill "phpmd_executable_path": "" at phpcs config

  4. install php-cs-fixer

    sudo wget http://get.sensiolabs.org/php-cs-fixer.phar -O /usr/local/bin/php-cs-fixer sudo chmod a+x /usr/local/bin/php-cs-fixer

    change "php_cs_fixer_executable_path": "" to "php_cs_fixer_executable_path": "/usr/local/bin/php-cs-fixer"

  5. install scheck

    cd /opt/ git clone --depth=1 https://github.com/facebook/pfff.git cd pfff/ ./configure make depend make make opt

    change "scheck_executable_path" to "scheck_executable_path": "/opt/pfff/scheck"

Note: you may use sudo pear ... instead pear ...

like image 81
Saiqul Haq Avatar answered Jan 17 '23 17:01

Saiqul Haq


I use it with phpstorm because it near enough works out of the box, however I think this might be were you find your answer: https://github.com/benmatselby/sublime-phpcs

like image 26
swifty Avatar answered Jan 17 '23 17:01

swifty