From aee002784ac1718e109bd2ff387c834d5bd255a9 Mon Sep 17 00:00:00 2001 From: Matt Glaman Date: Sat, 22 Aug 2020 12:07:04 -0500 Subject: [PATCH] Allow \Drupal calls in traits and tests Fixes #102 --- src/Rules/Drupal/GlobalDrupalDependencyInjectionRule.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Rules/Drupal/GlobalDrupalDependencyInjectionRule.php b/src/Rules/Drupal/GlobalDrupalDependencyInjectionRule.php index dad42143..ff8db12d 100644 --- a/src/Rules/Drupal/GlobalDrupalDependencyInjectionRule.php +++ b/src/Rules/Drupal/GlobalDrupalDependencyInjectionRule.php @@ -23,7 +23,8 @@ public function processNode(Node $node, Scope $scope): array if (!($node->class instanceof Node\Name\FullyQualified) || (string) $node->class !== 'Drupal') { return []; } - if (!$scope->isInClass() && !$scope->isInTrait()) { + // Do not raise if called inside of a trait. + if (!$scope->isInClass() || $scope->isInTrait()) { return []; } $scopeClassReflection = $scope->getClassReflection(); @@ -32,6 +33,8 @@ public function processNode(Node $node, Scope $scope): array } $whitelist = [ + // Ignore tests. + 'PHPUnit\Framework\TestCase', // Typed data objects cannot use dependency injection. 'Drupal\Core\TypedData\TypedDataInterface', // Render elements cannot use dependency injection.