Skip to content

Commit 7d0d388

Browse files
authored
Merge branch 'main' into fix/inspector-type-extension-mixed-null-phpstan-2147
2 parents 01a4229 + 0d050e1 commit 7d0d388

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

src/Rules/Drupal/GlobalDrupalDependencyInjectionRule.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ public function processNode(Node $node, Scope $scope): array
4040
'PHPUnit\Framework\Test',
4141
// Typed data objects cannot use dependency injection.
4242
'Drupal\Core\TypedData\TypedDataInterface',
43-
// Render elements cannot use dependency injection.
44-
'Drupal\Core\Render\Element\ElementInterface',
45-
'Drupal\Core\Render\Element\FormElementInterface',
46-
'Drupal\config_translation\FormElement\ElementInterface',
4743
// Entities don't use services for now
4844
// @see https://www.drupal.org/project/drupal/issues/2913224
4945
'Drupal\Core\Entity\EntityInterface',

tests/src/Rules/GlobalDrupalDependencyInjectionRuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ public static function resultData(): \Generator
7070
[],
7171
];
7272

73+
yield [
74+
__DIR__ . '/data/bug-828.php',
75+
[
76+
[
77+
'\Drupal calls should be avoided in classes, use dependency injection instead',
78+
35,
79+
],
80+
],
81+
];
82+
7383
}
7484

7585

tests/src/Rules/data/bug-828.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Bug828;
4+
5+
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
6+
use Drupal\Core\Render\Element\RenderElementBase;
7+
use Symfony\Component\DependencyInjection\ContainerInterface;
8+
9+
// A render element using ContainerFactoryPluginInterface for DI.
10+
// Should NOT trigger the rule since it uses proper DI via create().
11+
class RenderElementWithDi extends RenderElementBase implements ContainerFactoryPluginInterface {
12+
13+
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
14+
parent::__construct($configuration, $plugin_id, $plugin_definition);
15+
}
16+
17+
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
18+
return new static($configuration, $plugin_id, $plugin_definition);
19+
}
20+
21+
public function getInfo() {
22+
return [];
23+
}
24+
25+
}
26+
27+
// A render element using \Drupal directly. Should trigger the rule.
28+
class RenderElementWithDrupalCall extends RenderElementBase {
29+
30+
public function getInfo() {
31+
return [];
32+
}
33+
34+
public function someMethod() {
35+
\Drupal::service('some.service');
36+
}
37+
38+
}

0 commit comments

Comments
 (0)