Skip to content

Commit 691acb4

Browse files
authored
Merge pull request #14 from muglug/improve-type-coverage
Improve type coverage
2 parents 9c94cea + 280500f commit 691acb4

File tree

8 files changed

+41
-23
lines changed

8 files changed

+41
-23
lines changed

src/DependencyInjection/DrupalExtension.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class DrupalExtension extends CompilerExtension
2020
];
2121

2222
/**
23-
* @var string
23+
* @var ?string
2424
*/
2525
private $autoloaderPath;
2626

@@ -62,18 +62,21 @@ public function loadConfiguration(): void
6262
throw new \InvalidArgumentException('Cannot determine the realpath of the autoloader.');
6363
}
6464
$project_root = dirname($realpath, 2);
65+
$drupalRoot = null;
6566
if (is_dir($project_root . '/core')) {
66-
$this->drupalRoot = $project_root;
67+
$drupalRoot = $project_root;
6768
}
6869
foreach (['web', 'docroot'] as $possible_docroot) {
6970
if (is_dir("$project_root/$possible_docroot/core")) {
70-
$this->drupalRoot = "$project_root/$possible_docroot";
71+
$drupalRoot = "$project_root/$possible_docroot";
7172
}
7273
}
73-
if ($this->drupalRoot === null) {
74+
if ($drupalRoot === null) {
7475
throw new \InvalidArgumentException('Unable to determine the Drupal root');
7576
}
7677

78+
$this->drupalRoot = $drupalRoot;
79+
7780
$builder = $this->getContainerBuilder();
7881
$builder->parameters['drupalRoot'] = $this->drupalRoot;
7982

@@ -99,7 +102,7 @@ public function loadConfiguration(): void
99102
$extensionDiscovery = new ExtensionDiscovery($this->drupalRoot);
100103
$extensionDiscovery->setProfileDirectories([]);
101104
$profiles = $extensionDiscovery->scan('profile');
102-
$profile_directories = array_map(function ($profile) {
105+
$profile_directories = array_map(function (\PHPStan\Drupal\Extension $profile) : string {
103106
return $profile->getPath();
104107
}, $profiles);
105108
$extensionDiscovery->setProfileDirectories($profile_directories);
@@ -139,7 +142,7 @@ public function loadConfiguration(): void
139142
// Prevent \Nette\DI\ContainerBuilder::completeStatement from array_walk_recursive into the arguments
140143
// and thinking these are real services for PHPStan's container.
141144
if (isset($serviceDefinition['arguments']) && is_array($serviceDefinition['arguments'])) {
142-
array_walk($serviceDefinition['arguments'], function (&$argument) {
145+
array_walk($serviceDefinition['arguments'], function (string &$argument) : void {
143146
$argument = str_replace('@', '', $argument);
144147
});
145148
}

src/Drupal/Bootstrap.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Bootstrap
1616
];
1717

1818
/**
19-
* @var string
19+
* @var ?string
2020
*/
2121
private $autoloaderPath;
2222

@@ -55,7 +55,7 @@ class Bootstrap
5555
private $themes = [];
5656

5757
/**
58-
* @var \PHPStan\Drupal\ExtensionDiscovery
58+
* @var ?\PHPStan\Drupal\ExtensionDiscovery
5959
*/
6060
private $extensionDiscovery;
6161

@@ -81,19 +81,21 @@ public function register(): void
8181
if (is_dir($project_root . '/core')) {
8282
$this->drupalRoot = $project_root;
8383
}
84+
$drupalRoot = null;
8485
foreach (['web', 'docroot'] as $possible_docroot) {
8586
if (is_dir("$project_root/$possible_docroot/core")) {
86-
$this->drupalRoot = "$project_root/$possible_docroot";
87+
$drupalRoot = "$project_root/$possible_docroot";
8788
}
8889
}
89-
if ($this->drupalRoot === null) {
90+
if ($drupalRoot === null) {
9091
throw new \InvalidArgumentException('Unable to determine the Drupal root');
9192
}
93+
$this->drupalRoot = $drupalRoot;
9294

9395
$this->extensionDiscovery = new ExtensionDiscovery($this->drupalRoot);
9496
$this->extensionDiscovery->setProfileDirectories([]);
9597
$profiles = $this->extensionDiscovery->scan('profile');
96-
$profile_directories = array_map(function ($profile) {
98+
$profile_directories = array_map(function (\PHPStan\Drupal\Extension $profile) : string {
9799
return $profile->getPath();
98100
}, $profiles);
99101
$this->extensionDiscovery->setProfileDirectories($profile_directories);

src/Drupal/Extension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Extension
3737
*
3838
* Note that SplFileInfo is a PHP resource and resources cannot be serialized.
3939
*
40-
* @var \SplFileInfo
40+
* @var ?\SplFileInfo
4141
*/
4242
protected $splFileInfo;
4343

@@ -51,12 +51,12 @@ class Extension
5151
/**
5252
* @var string
5353
*/
54-
public $subpath;
54+
public $subpath = '';
5555

5656
/**
5757
* @var string
5858
*/
59-
public $origin;
59+
public $origin = '';
6060

6161
/**
6262
* Constructs a new Extension object.

src/Drupal/ExtensionDiscovery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ protected function filterByProfileDirectories(array $all_files)
197197
return $all_files;
198198
}
199199

200-
$all_files = array_filter($all_files, function ($file) {
200+
$all_files = array_filter($all_files, function (\PHPStan\Drupal\Extension $file) : bool {
201201
if (strpos($file->subpath, 'profiles') !== 0) {
202202
// This extension doesn't belong to a profile, ignore it.
203203
return true;

src/Drupal/ServiceMap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class ServiceMap
1313
*/
1414
public function __construct(array $drupalServices)
1515
{
16+
$this->services = [];
17+
1618
foreach ($drupalServices as $serviceId => $serviceDefinition) {
1719
// @todo support factories
1820
if (!isset($serviceDefinition['class'])) {

src/Rules/Classes/EnhancedRequireParentConstructCallRule.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ public function processNode(Node $node, Scope $scope): array
3131
return [];
3232
}
3333

34+
$scopeClassReflection = $scope->getClassReflection();
35+
3436
// Provides specific handling for Drupal instances where not calling the parent __construct is "okay."
35-
if ($scope->getClassReflection() === null) {
37+
if ($scopeClassReflection === null) {
3638
throw new ShouldNotHappenException();
3739
}
38-
$classReflection = $scope->getClassReflection()->getNativeReflection();
40+
$classReflection = $scopeClassReflection->getNativeReflection();
3941
if (!$classReflection->isInterface()
4042
&& !$classReflection->isAnonymous()
4143
&& $classReflection->implementsInterface('Drupal\Component\Plugin\PluginManagerInterface')

src/Rules/Drupal/GlobalDrupalDependencyInjectionRule.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public function processNode(Node $node, Scope $scope): array
2626
if (!$scope->isInClass() && !$scope->isInTrait()) {
2727
return [];
2828
}
29-
if ($scope->getClassReflection() === null) {
29+
$scopeClassReflection = $scope->getClassReflection();
30+
if ($scopeClassReflection === null) {
3031
throw new ShouldNotHappenException();
3132
}
3233

@@ -38,18 +39,23 @@ public function processNode(Node $node, Scope $scope): array
3839
'Drupal\Core\Render\Element\FormElementInterface',
3940
'Drupal\config_translation\FormElement\ElementInterface',
4041
];
41-
$classReflection = $scope->getClassReflection()->getNativeReflection();
42+
$classReflection = $scopeClassReflection->getNativeReflection();
4243
foreach ($whitelist as $item) {
4344
if ($classReflection->implementsInterface($item)) {
4445
return [];
4546
}
4647
}
4748

48-
if ($scope->getFunctionName() === null || !($scope->getFunction() instanceof MethodReflection)) {
49+
if ($scope->getFunctionName() === null) {
50+
throw new ShouldNotHappenException();
51+
}
52+
53+
$scopeFunction = $scope->getFunction();
54+
if (!($scopeFunction instanceof MethodReflection)) {
4955
throw new ShouldNotHappenException();
5056
}
5157
// Static methods have to invoke \Drupal.
52-
if ($scope->getFunction()->isStatic()) {
58+
if ($scopeFunction->isStatic()) {
5359
return [];
5460
}
5561

src/Rules/Drupal/PluginManager/PluginManagerSetsCacheBackendRule.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ public function processNode(Node $node, Scope $scope): array
3636
return [];
3737
}
3838

39-
if ($scope->getClassReflection() === null) {
39+
$scopeClassReflection = $scope->getClassReflection();
40+
41+
if ($scopeClassReflection === null) {
4042
throw new ShouldNotHappenException();
4143
}
4244

43-
$classReflection = $scope->getClassReflection()->getNativeReflection();
45+
$classReflection = $scopeClassReflection->getNativeReflection();
4446

4547
if (!$this->isPluginManager($classReflection)) {
4648
return [];
@@ -71,6 +73,7 @@ public function processNode(Node $node, Scope $scope): array
7173
$cacheTags = $setCacheBackendArgs[2]->value;
7274
if (count($cacheTags->items) > 0) {
7375
$hasCacheTags = true;
76+
/** @var \PhpParser\Node\Expr\ArrayItem $item */
7477
foreach ($cacheTags->items as $item) {
7578
if (($item->value instanceof Node\Scalar\String_) &&
7679
strpos($item->value->value, $cacheKey->value) === false) {

0 commit comments

Comments
 (0)