Skip to content

Commit 7bbf81c

Browse files
committed
Check getInterfaceNames over implementsInterface
1 parent e6f5cab commit 7bbf81c

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

src/Rules/Drupal/GlobalDrupalDependencyInjectionRule.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public function processNode(Node $node, Scope $scope): array
3232
throw new ShouldNotHappenException();
3333
}
3434

35-
$whitelist = [
35+
$classReflection = $scopeClassReflection->getNativeReflection();
36+
$allowed_list = [
3637
// Ignore tests.
3738
'PHPUnit\Framework\Test',
3839
// Typed data objects cannot use dependency injection.
@@ -45,9 +46,10 @@ public function processNode(Node $node, Scope $scope): array
4546
// @see https://www.drupal.org/project/drupal/issues/2913224
4647
'Drupal\Core\Entity\EntityInterface',
4748
];
48-
$classReflection = $scopeClassReflection->getNativeReflection();
49-
foreach ($whitelist as $item) {
50-
if ($classReflection->implementsInterface($item)) {
49+
$implemented_interfaces = $classReflection->getInterfaceNames();
50+
51+
foreach ($allowed_list as $item) {
52+
if (in_array($item, $implemented_interfaces, true)) {
5153
return [];
5254
}
5355
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Drupal\Tests\module_with_tests\Kernel;
4+
5+
use Drupal;
6+
use Drupal\KernelTests\KernelTestBase;
7+
use Drupal\Tests\module_with_tests\Traits\ModuleWithTestsTrait;
8+
9+
/**
10+
* This is a dummy test class.
11+
*
12+
* @group module_with_tests
13+
*/
14+
class DrupalStaticCallTest extends KernelTestBase {
15+
16+
use ModuleWithTestsTrait;
17+
18+
/**
19+
* A dummy test.
20+
*/
21+
public function testModule() {
22+
$request = Drupal::hasRequest();
23+
$this->assertFalse($request);
24+
}
25+
26+
}

tests/src/DrupalIntegrationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function testExtensionTestSuiteAutoloading(): void
6060
__DIR__ . '/../fixtures/drupal/modules/module_with_tests/tests/src/Unit/ModuleWithTestsTest.php',
6161
__DIR__ . '/../fixtures/drupal/modules/module_with_tests/tests/src/Traits/ModuleWithTestsTrait.php',
6262
__DIR__ . '/../fixtures/drupal/modules/module_with_tests/tests/src/TestSite/ModuleWithTestsTestSite.php',
63+
__DIR__ . '/../fixtures/drupal/modules/module_with_tests/tests/src/Kernel/DrupalStaticCallTest.php',
6364
];
6465
foreach ($paths as $path) {
6566
$errors = $this->runAnalyze($path);

0 commit comments

Comments
 (0)