Skip to content

Commit 59633d4

Browse files
authored
Merge pull request #416 from mglaman/gh408
Allow \Drupal calls in StreamWrapperInterface
2 parents bd39ad6 + 0e5d4a1 commit 59633d4

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

src/Rules/Drupal/GlobalDrupalDependencyInjectionRule.php

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

35-
$classReflection = $scopeClassReflection->getNativeReflection();
3635
$allowed_list = [
3736
// Ignore tests.
3837
'PHPUnit\Framework\Test',
@@ -45,11 +44,14 @@ public function processNode(Node $node, Scope $scope): array
4544
// Entities don't use services for now
4645
// @see https://www.drupal.org/project/drupal/issues/2913224
4746
'Drupal\Core\Entity\EntityInterface',
47+
// Stream wrappers are only registered as a service for their tags
48+
// and cannot use dependency injection. Function calls like
49+
// file_exists, stat, etc. will construct the class directly.
50+
'Drupal\Core\StreamWrapper\StreamWrapperInterface',
4851
];
49-
$implemented_interfaces = $classReflection->getInterfaceNames();
5052

5153
foreach ($allowed_list as $item) {
52-
if (in_array($item, $implemented_interfaces, true)) {
54+
if ($scopeClassReflection->implementsInterface($item)) {
5355
return [];
5456
}
5557
}

tests/src/Rules/GlobalDrupalDependencyInjectionRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public function testRule(string $path, array $errorMessages): void
2222

2323
public function resultData(): \Generator
2424
{
25+
yield [
26+
__DIR__ . '/data/drupal-static.php',
27+
[],
28+
];
2529
yield [
2630
__DIR__ . '/../../fixtures/drupal/modules/phpstan_fixtures/src/UsesDeprecatedUrlFunction.php',
2731
[
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace DrupalStatic;
4+
5+
$class = new class () extends \Drupal\Core\StreamWrapper\LocalStream {
6+
7+
public function getDirectoryPath()
8+
{
9+
return \Drupal::root();
10+
}
11+
12+
public function getName()
13+
{
14+
}
15+
16+
public function getDescription()
17+
{
18+
}
19+
20+
public function getExternalUrl()
21+
{
22+
}
23+
};

0 commit comments

Comments
 (0)