Skip to content

Commit a9506d3

Browse files
committed
Fix phpcs and phpstan failures.
1 parent f00eed0 commit a9506d3

8 files changed

+79
-44
lines changed

src/DependencyInjection/DrupalExtension.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@
1111

1212
class DrupalExtension extends CompilerExtension
1313
{
14-
14+
/**
15+
* @var array
16+
*/
1517
protected $defaultConfig = [
1618
'modules' => [],
1719
'themes' => [],
1820
];
1921

22+
/**
23+
* @var string
24+
*/
2025
private $autoloaderPath;
2126

2227
/**
@@ -43,8 +48,19 @@ class DrupalExtension extends CompilerExtension
4348
*/
4449
protected $themeData = [];
4550

51+
/**
52+
* @var array
53+
*/
4654
private $modules = [];
55+
56+
/**
57+
* @var array
58+
*/
4759
private $themes = [];
60+
61+
/**
62+
* @var \PHPStan\Drupal\ExtensionDiscovery
63+
*/
4864
private $extensionDiscovery;
4965

5066
public function loadConfiguration(): void

src/DependencyInjection/RulesOverrideExtension.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ public function loadConfiguration(): void
1111
{
1212
$builder = $this->getContainerBuilder();
1313
foreach ($builder->getDefinitions() as $definition) {
14-
if ($definition->getFactory()->entity === RequireParentConstructCallRule::class) {
14+
$factory = $definition->getFactory();
15+
if ($factory === null) {
16+
continue;
17+
}
18+
if ($factory->entity === RequireParentConstructCallRule::class) {
1519
$definition->setFactory(EnhancedRequireParentConstructCallRule::class);
1620
}
1721
}
18-
1922
}
2023
}

src/Rules/Classes/EnhancedRequireParentConstructCallRule.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@
44

55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
7+
use PHPStan\ShouldNotHappenException;
78

8-
class EnhancedRequireParentConstructCallRule extends RequireParentConstructCallRule {
9+
class EnhancedRequireParentConstructCallRule extends RequireParentConstructCallRule
10+
{
911

1012
/**
1113
* @param Node $node
1214
* @param \PHPStan\Analyser\Scope $scope
1315
* @return string[]
14-
* @throws \PHPStan\ShouldNotHappenException
16+
* @throws ShouldNotHappenException
1517
*/
1618
public function processNode(Node $node, Scope $scope): array
1719
{
1820
assert($node instanceof Node\Stmt\ClassMethod);
1921

2022
if (!$scope->isInClass()) {
21-
throw new \PHPStan\ShouldNotHappenException();
23+
throw new ShouldNotHappenException();
2224
}
2325

2426
if ($scope->isInTrait()) {
@@ -30,9 +32,11 @@ public function processNode(Node $node, Scope $scope): array
3032
}
3133

3234
// Provides specific handling for Drupal instances where not calling the parent __construct is "okay."
35+
if ($scope->getClassReflection() === null) {
36+
throw new ShouldNotHappenException();
37+
}
3338
$classReflection = $scope->getClassReflection()->getNativeReflection();
34-
if (
35-
!$classReflection->isInterface()
39+
if (!$classReflection->isInterface()
3640
&& !$classReflection->isAnonymous()
3741
&& $classReflection->implementsInterface('Drupal\Component\Plugin\PluginManagerInterface')
3842
) {
@@ -41,6 +45,4 @@ public function processNode(Node $node, Scope $scope): array
4145

4246
return parent::processNode($node, $scope);
4347
}
44-
45-
4648
}

src/Rules/Classes/PluginManagerInspectionRule.php

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Rules\Rule;
8+
use PHPStan\ShouldNotHappenException;
89

910
class PluginManagerInspectionRule implements Rule
1011
{
@@ -38,14 +39,14 @@ public function processNode(Node $node, Scope $scope): array
3839

3940
foreach ($node->stmts as $stmt) {
4041
if ($stmt instanceof Node\Stmt\ClassMethod && $stmt->name->toString() === '__construct') {
41-
foreach ($stmt->stmts as $statement) {
42+
foreach ($stmt->stmts ?? [] as $statement) {
4243
if ($statement instanceof Node\Stmt\Expression) {
4344
$statement = $statement->expr;
4445
}
45-
if ($statement instanceof Node\Expr\MethodCall) {
46-
if ((string) $statement->name === 'alterInfo') {
47-
$hasAlterInfoSet = true;
48-
}
46+
if ($statement instanceof Node\Expr\MethodCall
47+
&& $statement->name instanceof Node\Identifier
48+
&& $statement->name->name === 'alterInfo') {
49+
$hasAlterInfoSet = true;
4950
}
5051
}
5152
}
@@ -58,25 +59,27 @@ public function processNode(Node $node, Scope $scope): array
5859
return $errors;
5960
}
6061

61-
private function isYamlDiscovery(Node\Stmt\Class_ $class): bool {
62+
private function isYamlDiscovery(Node\Stmt\Class_ $class): bool
63+
{
6264
foreach ($class->stmts as $stmt) {
6365
// YAML discovery plugin managers must override getDiscovery.
6466
if ($stmt instanceof Node\Stmt\ClassMethod && $stmt->name->toString() === 'getDiscovery') {
65-
foreach ($stmt->stmts as $methodStmt) {
66-
67+
foreach ($stmt->stmts ?? [] as $methodStmt) {
6768
if ($methodStmt instanceof Node\Stmt\If_) {
6869
foreach ($methodStmt->stmts as $ifStmt) {
6970
if ($ifStmt instanceof Node\Stmt\Expression) {
7071
$ifStmtExpr = $ifStmt->expr;
7172
if ($ifStmtExpr instanceof Node\Expr\Assign) {
7273
$ifStmtExprVar = $ifStmtExpr->var;
73-
if (
74-
$ifStmtExprVar instanceof Node\Expr\PropertyFetch
74+
if ($ifStmtExprVar instanceof Node\Expr\PropertyFetch
7575
&& $ifStmtExprVar->var instanceof Node\Expr\Variable
76+
&& $ifStmtExprVar->name instanceof Node\Identifier
7677
&& $ifStmtExprVar->name->name === 'discovery'
7778
) {
7879
$ifStmtExprExpr = $ifStmtExpr->expr;
79-
if ($ifStmtExprExpr instanceof Node\Expr\New_ && $ifStmtExprExpr->class->toString() === 'Drupal\Core\Plugin\Discovery\YamlDiscovery') {
80+
if ($ifStmtExprExpr instanceof Node\Expr\New_
81+
&& ($ifStmtExprExpr->class instanceof Node\Name)
82+
&& $ifStmtExprExpr->class->toString() === 'Drupal\Core\Plugin\Discovery\YamlDiscovery') {
8083
return true;
8184
}
8285
}
@@ -91,19 +94,24 @@ private function isYamlDiscovery(Node\Stmt\Class_ $class): bool {
9194
return false;
9295
}
9396

94-
private function inspectYamlPluginManager(Node\Stmt\Class_ $class): array {
97+
private function inspectYamlPluginManager(Node\Stmt\Class_ $class): array
98+
{
9599
$errors = [];
96100

97-
$fqn = (string) $class->namespacedName;
101+
$fqn = (string)$class->namespacedName;
98102
$reflection = new \ReflectionClass($fqn);
99103
$constructor = $reflection->getConstructor();
100104

105+
if ($constructor === null) {
106+
throw new ShouldNotHappenException();
107+
}
108+
101109
if ($constructor->class !== $fqn) {
102110
$errors[] = sprintf('%s must override __construct if using YAML plugins.', $fqn);
103111
} else {
104112
foreach ($class->stmts as $stmt) {
105113
if ($stmt instanceof Node\Stmt\ClassMethod && $stmt->name->toString() === '__construct') {
106-
foreach ($stmt->stmts as $constructorStmt) {
114+
foreach ($stmt->stmts ?? [] as $constructorStmt) {
107115
if ($constructorStmt instanceof Node\Stmt\Expression) {
108116
$constructorStmt = $constructorStmt->expr;
109117
}
@@ -113,7 +121,7 @@ private function inspectYamlPluginManager(Node\Stmt\Class_ $class): array {
113121
&& $constructorStmt->name instanceof Node\Identifier
114122
&& $constructorStmt->name->name === '__construct') {
115123
$errors[] = sprintf('YAML plugin managers should not invoke its parent constructor.');
116-
}
124+
}
117125
}
118126
}
119127
}

src/Rules/Drupal/Coder/DiscouragedFunctionsRule.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,8 @@ public function processNode(Node $node, Scope $scope): array
5353
];
5454

5555
if (in_array($name, $discouragedFunctions, true)) {
56-
var_dump($name);
5756
return [sprintf('Calls to function %s should not exist.', $name)];
5857
}
5958
return [];
60-
6159
}
62-
63-
6460
}

src/Rules/Drupal/GlobalDrupalDependencyInjectionRule.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
7+
use PHPStan\Reflection\MethodReflection;
78
use PHPStan\Rules\Rule;
9+
use PHPStan\ShouldNotHappenException;
810

911
class GlobalDrupalDependencyInjectionRule implements Rule
1012
{
@@ -24,6 +26,9 @@ public function processNode(Node $node, Scope $scope): array
2426
if (!$scope->isInClass() && !$scope->isInTrait()) {
2527
return [];
2628
}
29+
if ($scope->getClassReflection() === null) {
30+
throw new ShouldNotHappenException();
31+
}
2732

2833
$whitelist = [
2934
// Typed data objects cannot use dependency injection.
@@ -40,8 +45,8 @@ public function processNode(Node $node, Scope $scope): array
4045
}
4146
}
4247

43-
if ($scope->getFunctionName() === null) {
44-
throw new \PHPStan\ShouldNotHappenException();
48+
if ($scope->getFunctionName() === null || !($scope->getFunction() instanceof MethodReflection)) {
49+
throw new ShouldNotHappenException();
4550
}
4651
// Static methods have to invoke \Drupal.
4752
if ($scope->getFunction()->isStatic()) {
@@ -51,7 +56,5 @@ public function processNode(Node $node, Scope $scope): array
5156
return [
5257
'\Drupal calls should be avoided in classes, use dependency injection instead'
5358
];
54-
5559
}
56-
5760
}

src/Rules/Drupal/PluginManager/AbstractPluginManagerRule.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,4 @@ protected function isPluginManager(\ReflectionClass $classReflection): bool
1414
!$classReflection->isAnonymous() &&
1515
$classReflection->implementsInterface('Drupal\Component\Plugin\PluginManagerInterface');
1616
}
17-
1817
}

src/Rules/Drupal/PluginManager/PluginManagerSetsCacheBackendRule.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpParser\Node;
66
use PhpParser\Node\Stmt\ClassMethod;
77
use PHPStan\Analyser\Scope;
8+
use PHPStan\ShouldNotHappenException;
89

910
class PluginManagerSetsCacheBackendRule extends AbstractPluginManagerRule
1011
{
@@ -35,6 +36,10 @@ public function processNode(Node $node, Scope $scope): array
3536
return [];
3637
}
3738

39+
if ($scope->getClassReflection() === null) {
40+
throw new ShouldNotHappenException();
41+
}
42+
3843
$classReflection = $scope->getClassReflection()->getNativeReflection();
3944

4045
if (!$this->isPluginManager($classReflection)) {
@@ -45,26 +50,30 @@ public function processNode(Node $node, Scope $scope): array
4550
$hasCacheTags = false;
4651
$misnamedCacheTagWarnings = [];
4752

48-
foreach ($node->stmts as $statement) {
53+
foreach ($node->stmts ?? [] as $statement) {
4954
if ($statement instanceof Node\Stmt\Expression) {
5055
$statement = $statement->expr;
5156
}
52-
if (($statement instanceof Node\Expr\MethodCall) && (string)$statement->name === 'setCacheBackend') {
53-
$hasCacheBackendSet = true;
54-
57+
if (($statement instanceof Node\Expr\MethodCall) &&
58+
($statement->name instanceof Node\Identifier) &&
59+
$statement->name->name === 'setCacheBackend') {
5560
// setCacheBackend accepts a cache backend, the cache key, and optional (but suggested) cache tags.
5661
$setCacheBackendArgs = $statement->args;
5762

58-
$cacheKey = $setCacheBackendArgs[1]->value->value;
63+
$cacheKey = $setCacheBackendArgs[1]->value;
64+
if (!$cacheKey instanceof Node\Scalar\String_) {
65+
continue;
66+
}
67+
$hasCacheBackendSet = true;
68+
5969
if (isset($setCacheBackendArgs[2])) {
6070
/** @var \PhpParser\Node\Expr\Array_ $cacheTags */
6171
$cacheTags = $setCacheBackendArgs[2]->value;
62-
if (!empty($cacheTags->items)) {
72+
if (count($cacheTags->items) > 0) {
6373
$hasCacheTags = true;
6474
foreach ($cacheTags->items as $item) {
65-
if (
66-
($item->value instanceof Node\Scalar\String_) &&
67-
strpos($item->value->value, $cacheKey) === false) {
75+
if (($item->value instanceof Node\Scalar\String_) &&
76+
strpos($item->value->value, $cacheKey->value) === false) {
6877
$misnamedCacheTagWarnings[] = $item->value->value;
6978
}
7079
}
@@ -88,5 +97,4 @@ public function processNode(Node $node, Scope $scope): array
8897

8998
return $errors;
9099
}
91-
92100
}

0 commit comments

Comments
 (0)