phpstan-drupal
phpstan-drupal copied to clipboard
Drupal's EventDisapatcher is incompatible with Symfony; throws error
How is drupal-check installed?
drupal-check is installed as a dependency to my project
Environment:
- OS: MacOS
- PHP Version: 7.3
- Drupal core: 9.3.12
Describe the bug
When running DrupalCheck against an EventDisapatcher, we get a reported error. For instance in UserFloodControl.php in Drupal core:
/**
* {@inheritdoc}
*/
public function isAllowed($name, $threshold, $window = 3600, $identifier = NULL) {
if ($this->flood->isAllowed($name, $threshold, $window, $identifier)) {
return TRUE;
}
// Register flood control blocked login event.
$event_map['user.failed_login_ip'] = UserEvents::FLOOD_BLOCKED_IP;
$event_map['user.failed_login_user'] = UserEvents::FLOOD_BLOCKED_USER;
$event_map['user.http_login'] = UserEvents::FLOOD_BLOCKED_USER;
if (isset($event_map[$name])) {
if (empty($identifier)) {
$identifier = $this->requestStack->getCurrentRequest()->getClientIp();
}
$event = new UserFloodEvent($name, $threshold, $window, $identifier);
$this->eventDispatcher->dispatch($event, $event_map[$name]);
}
return FALSE;
}
This is due to the fact that Symfony deprecated the 2nd argument of the dispatch method. However, Drupal 9 (so far as I can tell) will not work without that argument.
Console output
> php vendor/bin/drupal-check -vvv web/core/modules/user/src/UserFloodControl.php
Performing deprecation checks
Analyzing path: /Users/rickard/Sites/tc22/web/core/modules/user/src/UserFloodControl.php
Current working directory: /Users/rickard/Sites/tc22
Using Drupal root: /Users/rickard/Sites/tc22/web
Using vendor root: /Users/rickard/Sites/tc22/vendor
Assumed running as global dependency
PHPStan path: /Users/rickard/Sites/tc22/vendor/phpstan/phpstan/phpstan.phar
PHPStan configuration path: /private/var/folders/kp/5qn2wy3163n8bqhx9cv1h25w0000gn/T/drupal_check_phpstan_1653576710.neon
PHPStan configuration:
parameters:
tipsOfTheDay: false
reportUnmatchedIgnoredErrors: false
excludePaths:
- */tests/Drupal/Tests/Listeners/Legacy/*
- */tests/fixtures/*.php
- */settings*.php
- */node_modules/*
ignoreErrors:
- '#\Drupal calls should be avoided in classes, use dependency injection instead#'
- '#Plugin definitions cannot be altered.#'
- '#Missing cache backend declaration for performance.#'
- '#Plugin manager has cache backend specified but does not declare cache tags.#'
- '#Unsafe usage of new static\(\)#'
drupal:
drupal_root: /Users/rickard/Sites/tc22/web
level: 2
bootstrapFiles:
- /Users/rickard/Sites/tc22/vendor/mglaman/drupal-check/error-bootstrap.php
includes:
- /Users/rickard/Sites/tc22/vendor/phpstan/phpstan-deprecation-rules/rules.neon
- /Users/rickard/Sites/tc22/vendor/mglaman/phpstan-drupal/extension.neon
Executing PHPStan
Result cache not used because only files were passed as analysed paths.
1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100% < 1 sec/< 1 sec 72.5 MiB
Result cache was not saved because only files were passed as analysed paths.
------ -----------------------------------------------------------------------
Line UserFloodControl.php
------ -----------------------------------------------------------------------
72 Method
Symfony\Contracts\EventDispatcher\EventDispatcherInterface::dispatch(
) invoked with 2 parameters, 1 required.
------ -----------------------------------------------------------------------
[ERROR] Found 1 error
Used memory: 72.5 MB
Finished executing PHPStan
Unlinking PHPStan configuration
Return PHPStan exit code
Thanks for using drupal-check!
Ideally, we'd be able to flag this in a phpstan.neon or similar in order to bypass the known error.
Looks like this can be fixed by following https://www.drupal.org/node/3159012
UPDATE: No, the code already follows that pattern.
I bypassed this with /** @phpstan-ignore-next-line */