Setting up Developer Tools and Packages Within PhpStorm For Laravel

Dmitry Khorev
4 min readJun 22, 2022

--

Setting up developer tools and packages within PhpStorm for Laravel
Setting up developer tools and packages within PhpStorm for Laravel

During previous articles, we installed some dependencies and plugins for PhpStorm and Laravel. Now let’s dive into the configuration part.

Setting good defaults

I will start by setting some good defaults, that help with automated code formatting and fit well with other external linting/formatting tools both for PHP and JavaScript, and Node.js development.

PHP — setup PSR-12 code style as default

JavaScript — setup JS default style guide

PhpStorm features

Next, we will go over and configure anything exclusive to PhpStorm.

PHP executable

You can choose any language level and install the executable. PhpStorm will provide insights into your code based on the PHP version.

PHP executable
PHP executable

JavaScript and Node.js language settings

JavaScript and Node.js language settings
JavaScript and Node.js language settings

Actions on Save

Good to have — format only changed lines. Keeps your PRs smaller and more focused.

Actions on Save
Actions on Save

Code folding

A very nice feature for larger projects works both for PHP and JavaScript code (and some other languages too).

When you open a new file for edit, selected items of a class are folded, so you can get a nice overview of the whole file without listing too much.

Turn ON PSR-12 Inspections

We already touched on the inspections topic in my first article in this series. Right now we are going to enable PSR-12 inspections, which are disabled by default in new installs. PHP > Code style > PSR-12

PSR-12 Inspections
PSR-12 Inspections

Apart from that, I recommend enabling all inspections for PHP and then disabling them 1 by 1 if something annoys you (like “Every class should have a PhpDoc”).

File and Code templates

Just a helper template for when you need to create new files in your project. For example, I like to add “declare(strict_types=1);” to all my PHP files.

File and Code templates
File and Code templates

Live Templates

Those templates a useful for repeatable action in your classes. Example: create a unit test stub function, inject language-specific constructions like loops, if/else, etc.

Live Templates config
Live Templates config

And now those helpers in action example.

Live Templates action
Live Templates action

Composer packages integration

Next, we will configure our composer packages integration with PhpStorm.

Why lint and analyze and code?

  • Lint helps to keep your code in one style. Useful for team projects, and good for self-discipline on your hobby projects.
  • Static code analysis helps catch bugs earlier in the development cycle and that’s one of the best things PhpStorm provides. Paired with stricter types, type-hints, and so on, your code would become cleaner and safer over time.

PHP_CodeSniffer setup

We installed this package and configured two commands in composer.json during the previous article so now we’ll go over how one can configure it in PhpStorm integration.

PHP_CodeSniffer setup
PHP_CodeSniffer setup

You now have an automatic php linter and fixed for your open files.

PHPStan & LaraStan setup

We installed this package and configured one command in composer.json during the previous article so now we’ll go over PhpStorm integration.

PHPStan & LaraStan setup
PHPStan & LaraStan setup

You now have improved static analysis for php files.

Code analysis for CI/CD

For CI/CD and local development, you can use the commands we’ve added in the previous article.

composer lint
composer lint-fix
composer analyze

Bonus: basic automation for code QA on commit

Recommended app — Husky https://typicode.github.io/husky/#/

Follow the installation guide on the site above, then add custom commands to your commit workflow (works both in console and PhpStorm).

basic automation for code QA on commit
basic automation for code QA on commit

That’s all for this series. Hope you enjoyed and learned something new.

Over time your productivity and quality of code will skyrocket when you use the right tools for the job.

--

--

Dmitry Khorev

Sharing my experience in software engineering (NestJS, Laravel, System Design, DevOps)