Skip to content

How to set up Code Sniffer for PHPStorm with Lando

Hannes Kirsman edited this page Nov 11, 2022 · 2 revisions

PS. This setup was written by using PhpStorm 2022.2.3, Lando 3.6.4, Ubuntu 20.04.5 LTS. Give feedback if you got it working with some other setup.

First install Code Quality tool https://github.com/wunderio/code-quality#installation (minimum version 2.2.1). 2.2.1 includes a ruleset called WunderAll which groups together WunderDrupal and WunderSecurity rulesets. This is needed because in PHPStorm it’s not possible to choose multiple rules.

Start your Lando project

lando start

Take note of the image running Lando PHP image:

docker ps --format "table {{.Image}}\t{{.Command}}\t{{.Status}}" | grep php

In our case, it’s: devwithlando/php:8.1-fpm-4.

Open PhpStorm.

Set up CLI interpreter. For this go to File → Settings → PHP → Quality Tools → PHP_CodeSniffer. Click on the three dots on the "Configuration" line.

1

Press + button and then three dots in the "PHP_CodeSniffer By Interpreter" dialog to add a new CLI Interpreter.

2

Press + button and then choose "Select From Docker, Vagrant, VM, VSL, Remote…"

3

Select Docker from the radio button options and press "New…" on the "Server" line.

4

Default settings should be ok and there should be a message "Connection successful. Press OK.

5

Set "Image name" to previously found devwithlando/php:8.1-fpm-4 and press OK.

6

Uncheck "Visible only for this project", also you should see your PHP version and config file names loaded from the Lando instance. Press OK.

7

Now choose the newly created interpreter and press OK.

8

Click on the folder icon on the "Docker container" line.

9

Click on the pencil icon and in the "Volume binding" dialog change "Container path" to /app. Press OK and OK.

10

Change PHP_CodeSniffer path to /app/vendor/bin/phpcs and "Path to phpcbf" to /app/vendor/bin/phpcbf and press Validate. You should see a message bottom of the window with something like "OK, PHP_CodeSniffer version 3.7.1 (stable) by Squiz". Press Apply and then click on the link "PHP_CodeSniffer inspection".

11

Make sure "Check files with extensions" has all the needed extensions. Mine was missing module. Then choose WunderAll from the "Coding standard". Press OK in all windows and you should be good to go.

12

Now you should see PHPStorm underlining problematic code and when hovering there with the mouse, you’ll see more information and it's starting with phpcs:. Also if you click on the "PHP Code Beautifier and Fixer: fix the whole file" then it does exactly what it says :)

13

Clone this wiki locally