Description
GrumpPHP actually supports psalm but not sure yet how much work needs to be done for implementing it Code Quality.
@ragnarkurmwunder said:
The Problem
The striving higher quality for less price to be paid.
My practice is psalm figures out a lot of bugs even before any test would catch them.
So effort to catch bugs ratio compared with writing tests, I'd say is 1:100.
The Solution
Install Psalm as
- part of the workflow –
composer require-dev vimeo/psalm
- Initialize
psalm – psalm --init
. By this Psalm figures out the highest level of errorLevel, and operates on that level. - Configure Psalm to scan proper folders, namely
web/modules/custom
, andweb/themes/custom
. - Integrate into our workflow. It could be
make scan
, part of commit scanners, or part in deployment flow. Or any combination of those. I personally use the first option massively to make the feedback loop as tight as possible. - Commit psalm.xml.
- As the codebase gets cleaner,
errorLevel
can be reduced.
Note, Psalm operates by tracking types. Therefore, adding types to all member variables, function and method arguments, and return data is important. In addition I suggest to use strict types. Php <8 have a bit limited type functionality, but doable. In some places need to add additional @var
, @param
, @return
docblocks.
Psalm documentation - https://psalm.dev/
Psalm is also able to set a baseline based on existing code, to prevent new bugs from leaking in.
https://psalm.dev/docs/running_psalm/dealing_with_code_issues/#using-a-baseline-file
Otherwise one might find it too intimidating to fix the vast amount items that Psalm may complain about.