User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

Crazy tasty spiderI had recently to switch from geany code editor to visual studio code (further VS Code). I had my geany setup to use joomla coding standards for my code formatting. I used phpcs to inform me on problems. I didn't know how to use php-cs-fixer to automatically fix issues before and I'd like to add this function.

Since I code not only for Joomla, I need to use PSR2 coding standards for non-joomla code and to use PSR2 for other PHP projects.

For example in PSR2 coding standard a statement should look like this

if (true) {
    echo "yes';
}

In Joomla, the brace must be on the new line.

if (true)
{
    echo "yes';
}

The solution was rather tricky for me. So I write this note also for myself for later reference. I had to install somethings globally, system-wide, and some only per joomla workspace (joomla installation folder where I develop my extensions). I use OpenSuSE linux, but this would not make much difference to apply the steps for another OS.

 So after installing VS Code, I had to install two plugins (in fact I use much more, but the only two concern the topic).

  • vscode-phpcs to check my code formatting problems (see the phpcs result screenshot to get the idea how it works)
  • vscode-php-cs-fixer to try to automatically fix most code formatting incosistency (press a keyboard shortcut and most braces, indents, missing/extra spaces etc. are fixed automatically)

Both VS code extensions would not work without some dependency software installed.

First of all we need to install phpcs itself. The vscode-phpcs tells us to install it system-wide using pear.

sudo pear install PHP_CodeSniffer

Alas composer global require "squizlabs/php_codesniffer=*" would not work with the VS Code plugin. The plugin doesn't see the executable file elsewhere except /usb/bin/phpcs. So pear installation is a must. And it can be installed per project via composer, which should also work.

PHP-CS-Fixer installation was done by using composer composer global require friendsofphp/php-cs-fixer

I also updated the PATH variable. Added to ~/.bashrc line export PATH="$PATH:~/.config/composer/vendor/bin"

But VS Code didn't see the php-cs-fixer executable with error: PHP CS Fixer: spawn php-cs-fixer ENOENT. executablePath not found.

On some reason though php-cs-fixer command is accessible from anywhere, VS Code plugin demands FULL path to it. So in your VS code settings add line

"php-cs-fixer.executablePath": "/home/gruz/.config/composer/vendor/bin/php-cs-fixer",

b_300_0_16777215_00_images_x_2018_01_29_02_51_52_ht.jpeg

After this both PHP linitng via phpcs and php-cs-fixer started to work in VS Code for PSR2. (default is PSR2).

Next the task was to make special behavior for a joomla installation.

First of all I had to install Joomla coding standards. The stable version supports only older PHPCS 2.x series, while actual is PHPCS 3.x. So according to the Joomla coding standards docs (which didn't work as described) and manual investigation I installed the latest Joomla coding standards:

Updated my /home/gruz/.config/composer/composer.json by adding 2 lines:

"minimum-stability": "alpha"

and

"require-dev": {

     ...
     "joomla/coding-standards": "dev-3.x-dev",

     ...
}

My global composer.json at the moment is https://gist.github.com/gruz/9e1b95389669fdce08fb39006ccf7728#file-composer-json

Surely for Joomla you don't need the line I have "laravel/installer": "^1.5".

Run composer global update

Next we must add the Joomla Coding Standards to the phpcs configration.

sudo phpcs --config-set installed_paths ~/.config/composer/vendor/joomla/coding-standards/

To check if phpcs sees the coding standards, run phpcs -i.The response should be like The installed coding standards are MySource, PEAR, PSR1, PSR2, Squiz, Zend and Joomla

Next we need to make two things for the Joomla folder (VS Code workspace should be tied to your Joomla folder)

In your joomla VS Code workspace set default phpcs standard to Joomla

b_300_0_16777215_00_images_x_2018_01_29_02_55_40_pv.jpeg

Place php-cs-fixer rules file to the joomla root

The file should be named .php_cs and you can find it here: https://gist.github.com/gruz/9e1b95389669fdce08fb39006ccf7728#file-php_cs

You can modify the file according to your needs. See docs: https://github.com/FriendsOfPHP/PHP-CS-Fixer

Alas, not everything is possible to fix in such a way. For example in Joomla Coding Standards there should be a blank line after a statement in curly braces. Php-cs-fixer has an option to add a blank line before a statement, but not after it.

Troubleshooting

During investigating I had several errors.

phpcs: Unable to locate phpcs. Please add phpcs to your global path or use composer depency manager to install it in your project locally.

The PHP CodeSniffer Linter server crashed 5 times in the last 3 minutes. The server will not be restarted.

VS Code didn't see phpcs installed globally via composer. Only pear installation worked.

There was aslo an error with some JSON bad characters. As far as I remember if was when I tried to install phpcs via composer under my project folder. Such an installation had output not in JSON format, which is used by the VS Code plugin. So I had to set the default output like this

./vendor/bin/phpcs --config-set report_format json

Please register to post comments.

Found a typo? Please select it and press Ctrl + Enter.
FaLang translation system by Faboba
Електронна бібліотека «Exlibris»: історичні дослідження, мемуаристика, публіцистика, художня література Українська етнографія: книги, курси лекцій, статті й матеріали, мапи, фотоальбоми, веб-ресурси Правителі України: портрети, життєписи, матеріали Твори Адріана Кащенка: романтика козацьких часів Народна війна 1917-1932

See a typo?

Select the text block and press

CTRL+Enter

Comments

  • NotificationAry - get emails on Joomla content is submitted, added or changed

    Gullo Gullo 03.12.2018 18:36
    Hello, I don't resolved my problem.... The Plug don't save the changes. I tried reinstallation but ...

    Read more...

     
  • MVC Override - change joomla extensions behaviour without changing core files

    rogerco rogerco 01.12.2018 23:30
    Sorry for the delay replying, the notification got trapped as spam on the mail server so I have only ...

    Read more...

     
  • NotificationAry - get emails on Joomla content is submitted, added or changed

    gruz gruz 28.11.2018 23:11
    Thanks for reporting. Need to find time to check.

    Read more...

     
  • NotificationAry - get emails on Joomla content is submitted, added or changed

    Gullo Gullo 28.11.2018 22:30
    Hello, the plugin NotificationAry don't save the changes. Joomla 3.9.1 NotificationAry 0.2.6

    Read more...

     
  • NotificationAry - get emails on Joomla content is submitted, added or changed

    gruz gruz 23.11.2018 08:25
    Yes, I'll work at it maybe tonigh, or at the weekend.

    Read more...