-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#62 Add Psalm support. #66
Conversation
Seems as if we also need https://github.com/fenetikm/autoload-drupal because otherwise it complains missing classes although they are there (and dev-master because it supports Composer 2). I've added this with last commit. In composer.json under extras add
Run
And finally try to commit code or run
|
Could also be we need to test https://github.com/mortenson/psalm-plugin-drupal |
https://github.com/fenetikm/autoload-drupal seems to be quite good but doesn't seem to load .module files so I'm seeing undefined constant error with my code. This one says it solves the .module and .theme loading https://github.com/mortenson/psalm-plugin-drupal |
For temporary fix it seems I'd need to create file something like autoload-extras-for-psalm.php
In psalm.xml add autoloader parameter:
|
Hm, can't get this to work. One more thing. We already have mglaman/phpstan-drupal installed and this seems promising: https://github.com/mglaman/phpstan-drupal/blob/main/drupal-autoloader.php |
I've taken the https://github.com/mglaman/phpstan-drupal/blob/main/drupal-autoloader.php and refactored it a bit. To me it seems to work nicely. |
Should be ok now for testing. See initial comment. |
b67cded
to
5a54029
Compare
I'm getting
This is the code:
I don't see it's actually a valid error. PhpStorm find the interface and if I try to add it then PhpStorm also yells that I can't add it again. So I'll disable this error:
|
Another one:
But we're not going to do something like t('Foobar')->____toString(). That would output string but with the placeholders. I'll create ignore for that specific error. All in all I'm adding these ignores to config:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, @hkirsman! I've been testing this feature on one of the customer's projects for some time. Seems to be working nicely in overall 🙂 👍 But since commit 5a9c35c I'm facing a few issues, please see my other two comments below.
Also, one thing that is not working is PSALM's Docblock suppression (/** @psalm-suppress <SomeError> */
) as PHPCS task throws Drupal.Commenting.InlineComment.DocBlock error whenever such docblock is used. In my project I've added a rule to phpcs.xml
file to ignore such errors completely (see my snippet below), but a better option would prolly be to ignore only @psalm-suppress
docblock comments. Not sure how, but I guess something similar has been done with @var
docblocks.
<!-- Mark inline DocBlock comments only as a warning as otherwise using @psalm-suppress annotation comments leads to PHPCS errors-->
<rule ref="Drupal.Commenting.InlineComment.DocBlock">
<severity>4</severity>
</rule>
UPDATE:
PSALM's Docblock suppression is now allowed and works since latest commit (361795e) 🎉
defaults: null | ||
allowed_types: ['string', 'null' ] | ||
threads: | ||
defaults: 6 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Running ./vendor/bin/grumphp run --tasks=psalm
gives "The pcntl & posix extensions must be loaded in order for Psalm to be able to use multiple processes."
Happens both locally (using MacOs BigSur) and on CircleCI (using eu.gcr.io/silta-images/php:7.4-fpm-v0.1
for PHP container). If I override the default value of threads
parameter back to 1
, then it works again.
UPDATE:
Locally, if I use custom lando tooling that utilises lando's appserver for running for GrumPHP commands, everything works just fine:
grumphp:
description: Runs GrumPHP commands
cmd:
- appserver: ./vendor/bin/grumphp
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works nicely now both on Lando and CircleCI :)
$arguments->addOptionalArgument('--report=%s', $config['report']); | ||
$arguments->addOptionalArgument('--no-cache', $config['no_cache']); | ||
$arguments->addOptionalArgument('--threads=%d', $config['threads']); | ||
$arguments->add('--find-unused-code'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When testing on one of client's projects, UnusedVariable
errors can't be ignored if using /** @psalm-suppress UnusedVariable */
annotation-comment before the variable., as --find-unused-code
seems to have higher priority. It can be changed by ignoring particular errors in psalm.xml file by adding
<issueHandlers>
<UnusedVariable errorLevel="info" />
</issueHandlers>
but prolly all new UnusedVariable
errors will be ignored from now on, which is not great, I guess.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not remove that variable or is false positive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like a false positive to me as the variable is used multiple times after defined. Also PHPStorm doesn't mark it as unused which happened in all other cases I fixed. What's different is that it's defined as a reference to another variable (see the example code below). Maybe PSALM doesn't like that for some reason.
$subform1 =& $form['field_content_row']['widget'][$key]['subform'];
if (!empty($subform1['field_block_reference']['widget'])) {
foreach (Element::children($subform1['field_block_reference']['widget']) as $key1) {
...
UPDATE
Managed to ignore the error by adding @psalm-suppress UnusedVariable
for the function which had that variable, at least better option then ignoring such errors completely 🙂 So seems that only inline usages of @psalm-suppress doesn't work.
61f093c
to
942a0f2
Compare
3effb7b
to
361795e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, all of the mentioned issues have been fixed ✅
NOTE: Set-up was tested on a real customer project and worked nicely ✅
Green light from me 🟢 And great work! 💪 💪 💪
You can try this out on Drupal 9 project:
composer require wunderio/code-quality:dev-feature/62-add-psalm-task --dev
or in Lando prefix with lando. Psalm requires PHP's mbstring and in my experience I was missing it locally but Lando had it.
Lock files can be tricky and every project is different. On my 9.2.6 installation I did this:
Copy the psalm.xml config to project root
cp vendor/wunderio/code-quality/config/psalm.xml ./psalm.xml
You might see issues with tests not finding dependencies. ERROR: MissingDependency - web/modules/custom/redirect_old_tua/tests/src/HomePageTest.php:13:28 - Drupal\Tests\BrowserTestBase depends on class or interface phpunit\framework\testcase that does not exist (see https://psalm.dev/157)
class HomePageTest extends BrowserTestBase {
For that install testing Drupals testing support:
As I don't have php locally I'm also using Landos php with this grumphp config:
grumphp:
git_hook_variables:
EXEC_GRUMPHP_COMMAND: 'lando php'
and re-init grumphp
Where X.X.X is your current core version.