In this article, we will also go over and configure anything exclusive to PhpStorm. We 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, as well as other external tools.
PhpStorm will provide insights into your code based on the PHP version. We will also enable [PSR-12] inspections, which are disabled by default in new installs.
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.
JavaScript and Node.js language settings
Actions on Save
Good to have — format only changed lines. Keeps your PRs smaller and more focused.
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 in this series. Right now we are going to enable inspections, which are disabled by default in new installs. PHP > Code style > PSR-12
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.
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.
And now those helpers in action example.
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.
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.
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
Follow the installation guide on the site above, then add custom commands to your commit workflow (works both in console and PhpStorm).
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.