Skip to content

Latest commit

 

History

History
159 lines (99 loc) · 6.21 KB

Drupal-PHPStorm-Lando-Setting-Code-Sniffer-Debugging.md

File metadata and controls

159 lines (99 loc) · 6.21 KB

Drupal + PHPStorm + Lando: Setting Code Sniffer, Debugging

Prerequisities

Project is based on Lando recipe drupal8, webroot is web and xdebug is enabled. .lando.yml example:

recipe: drupal8
services:
  appserver:
    webroot: web
    xdebug: true

PHPCS (Code sniffer)

Install PHPCS + standards into Drupal project

Add coder to composer.json

"drupal/coder": "^8.2"

or composer require

lando composer require drupal/coder

It will install squizlabs/php_codesniffer + Drupal coding standards sniffers.

Local PHPCS installation

Sometimes could be better to install PHPCS locally rather than install it inside project. Create some special dir for Coder and install via Composer.

composer require drupal/coder:8.2.12

Install PHPCS via Lando

.lando.yml

services:
  appserver:
    build:
      - "mkdir -p /app/coder && cd /app/coder && composer require drupal/coder:8.2.12"
      - "/app/coder/vendor/bin/phpcs --config-set installed_paths /app/coder/vendor/drupal/coder/coder_sniffer"

If you have PHPCS already installed via Lando, set only path to phpcs (/app/coder/vendor/bin/phpcs) into PHPstorm.

[ ] TODO: Find out solution: Set path to coding standards to /app/coder/vendor/drupal/coder/coder_sniffer for both Drupal + DrupalPractice standards at once causes error during checking code.

Optionally (you need it only if you want to check coding standards in CLI)

To manually register the Drupal and DrupalPractice Standard with PHPCS, you must set the installed paths. Go to appserver container.

lando ssh

You should stay at /app dir.

/app$ vendor/bin/phpcs --config-set installed_paths /app/vendor/drupal/coder/coder_sniffer

It will create file /vendor/squizlabs/php_codesniffer/CodeSniffer.conf and it should contain the valid path to sniffers. If PHPCS throws an errors, check this file.

Check status for Drupal standards.

app$ vendor/bin/phpcs -i
The installed coding standards are PSR2,.... PHPCS, Drupal and DrupalPractice

Manual PHPCS run

Now, you can run PHPCS manually to check your work.

app$ /path_to_coder/vendor/bin/phpcs --standard=Drupal,DrupalPractice path_to_module

e.g:
app$ vendor/bin/phpcs --standard=Drupal,DrupalPractice web/modules/contrib/default_content_deploy/

You can use . for current dir.

Use Lando tooling for checking Drupal coding standard.

Add handy tool for PHPCS.

tooling:
  phpcs:
    service: appserver
    cmd: "/app/coder/vendor/bin/phpcs --standard=Drupal,DrupalPractice"
    options:
    description: Run phpcs for given folder or file. 

Run new command

lando phpcs relative_path/to_your_module/folder
lando phpcs relative_path/to_your_module/file.module

Drupal Coding standards, Coder, CodeSniffer

  1. Set PHP interpreter.
  2. Go to File → Settings… → Languages & Frameworks →PHP →CLI Interpreter →Click on the tree dots →Add. Select From Docker, Vagrant, VM, Remote…
  3. Select Docker from radio button options.
  4. Set Image name to devwithlando/php:7.1-fpm (you can find out the name by executing docker ps command). Uncheck Visible only for this project. Check Docker container mapping (should be /app, not /opt)

  1. Go to File →Settings→Languages & Frameworks→PHP→Code Sniffer.

  2. Add Code Sniffer configuration.

  3. Set PHP Code Sniffer (phpcs) path to /app/vendor/bin/phpcs.

  4. Go to File →Settings→Editor→Inspection.

  5. Set Coding standards to Custom.

  6. Choose one.

  7. Set Path to ruleset /app/vendor/drupal/coder/coder_sniffer/Drupal/ruleset.xml.

Debugging

Debugging from browser

  1. No need to install Xdebug helper extension!

  2. Set breakpoint in PHPstrom.

  3. Start Listening for PHP Debug Connections.

  4. Refresh page in browser. An popup should appear in PHPstrom. Select the appropriate file. After confirming this popup, PHPstorm creates also new server (File →Settings→Languages & Frameworks→PHP→Servers) with name appserver.

Debugging drush commands from console

  1. Set PHP Server. If you already used debugging from browser, this setting should be already present.

  2. Go to File →Settings→Language & Frameworks→PHP→Debug.

  3. Set path mappings. /app → your/local/path/to/project ** /app/vendor/drush -> /usr/local/bin/drush

  4. Export environment variable. lando ssh appserver export PHP_IDE_CONFIG="serverName=appserver"

  5. Set breakpoint.

  6. Start Listening for PHP Debug Connectios.

  7. Use drush commands from the appserver container. lando ssh cd web drush status