Skip to content

Commit 8ebb84b

Browse files
authored
Merge pull request #219 from mglaman/fix-head
Fix remaining test and analysis errors
2 parents e473354 + 91646fa commit 8ebb84b

25 files changed

+214
-482
lines changed

phpstan.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ parameters:
55
level: 8
66
checkGenericClassInNonGenericObjectType: false
77
checkMissingIterableValueType: false
8+
reportUnmatchedIgnoredErrors: false
89
paths:
910
- src
1011
- drupal-phpunit-hack.php
1112
- drupal-autoloader.php
1213
- tests/src
14+
excludePaths:
15+
- tests/src/Type/data/entity-type-manager.php

phpunit.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
beStrictAboutCoversAnnotation="true"
77
beStrictAboutOutputDuringTests="true"
88
beStrictAboutTodoAnnotatedTests="true"
9+
beStrictAboutChangesToGlobalState="true"
910
verbose="false">
1011
<php>
1112
<ini name="error_reporting" value="32767"/>
1213
<ini name="memory_limit" value="-1"/>
14+
<!-- Drupal requires Symfony PHPUnit Bridge, silence its warnings. -->
15+
<env name="SYMFONY_DEPRECATIONS_HELPER" value="disabled"/>
1316
</php>
1417
<testsuites>
1518
<testsuite name="default">
16-
<directory suffix="Test.php">tests</directory>
17-
<exclude>tests/fixtures/</exclude>
19+
<directory suffix="Test.php">tests/src</directory>
1820
</testsuite>
1921
</testsuites>
2022
<logging>

src/Drupal/DrupalAutoloader.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,7 @@ class: Drupal\jsonapi\Routing\JsonApiParamEnhancer
188188
}
189189

190190
$service_map = $container->getByType(ServiceMap::class);
191-
assert($service_map instanceof ServiceMap);
192-
// @todo this is a hack that needs investigation.
193-
// We cannot manipulate the service container and add parameters, so we take the existing
194-
// service and modify it's properties so that its reference is updated within the container.
195-
//
196-
// During debug this works, but other times it fails.
197191
$service_map->setDrupalServices($this->serviceMap);
198-
// So, to work around whatever is happening we force it into globals.
199-
$GLOBALS['drupalServiceMap'] = $service_map->getServices();
200192
}
201193

202194
protected function loadLegacyIncludes(): void

src/Drupal/ServiceMap.php

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,54 @@
22

33
namespace mglaman\PHPStanDrupal\Drupal;
44

5-
use PHPStan\ShouldNotHappenException;
6-
75
class ServiceMap
86
{
9-
/** @var \mglaman\PHPStanDrupal\Drupal\DrupalServiceDefinition[] */
10-
private $services = [];
7+
/** @var DrupalServiceDefinition[] */
8+
private static $services = [];
119

1210
public function getService(string $id): ?DrupalServiceDefinition
1311
{
14-
// @see notes in DrupalAutoloader.
15-
// This is all a work around due to inability to set container parameters.
16-
if (count($this->services) === 0) {
17-
$this->services = $GLOBALS['drupalServiceMap'] ?? [];
18-
if (count($this->services) === 0) {
19-
throw new ShouldNotHappenException('No Drupal service map was registered.');
20-
}
21-
}
22-
return $this->services[$id] ?? null;
12+
return self::$services[$id] ?? null;
2313
}
2414

2515
/**
26-
* @return \mglaman\PHPStanDrupal\Drupal\DrupalServiceDefinition[]
16+
* @return DrupalServiceDefinition[]
2717
*/
2818
public function getServices(): array
2919
{
30-
return $this->services;
20+
return self::$services;
3121
}
3222

3323
public function setDrupalServices(array $drupalServices): void
3424
{
35-
$this->services = [];
25+
self::$services = [];
3626

3727
foreach ($drupalServices as $serviceId => $serviceDefinition) {
3828
// @todo support factories
3929
if (!isset($serviceDefinition['class'])) {
4030
if (isset($serviceDefinition['alias'], $drupalServices[$serviceDefinition['alias']])) {
41-
$serviceDefinition['class'] = $drupalServices[$serviceDefinition['alias']]['class'];
31+
$aliasedService = $drupalServices[$serviceDefinition['alias']];
32+
33+
if (isset($aliasedService['class'])) {
34+
$serviceDefinition['class'] = $drupalServices[$serviceDefinition['alias']]['class'];
35+
} elseif (class_exists($serviceDefinition['alias'])) {
36+
$serviceDefinition['class'] = $serviceDefinition['alias'];
37+
}
38+
} elseif (class_exists($serviceId)) {
39+
$serviceDefinition['class'] = $serviceId;
4240
} else {
4341
continue;
4442
}
4543
}
46-
$this->services[$serviceId] = new DrupalServiceDefinition(
44+
self::$services[$serviceId] = new DrupalServiceDefinition(
4745
(string) $serviceId,
4846
$serviceDefinition['class'],
4947
$serviceDefinition['public'] ?? true,
5048
$serviceDefinition['alias'] ?? null
5149
);
5250
$deprecated = $serviceDefinition['deprecated'] ?? null;
5351
if ($deprecated) {
54-
$this->services[$serviceId]->setDeprecated(true, $deprecated);
52+
self::$services[$serviceId]->setDeprecated(true, $deprecated);
5553
}
5654
}
5755
}

src/Rules/Drupal/LoadIncludes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public function processNode(Node $node, Scope $scope): array
8686

8787
$module_name = $module_arg->value->value;
8888
if (!isset($modules[$module_name])) {
89+
// @todo return error that module is missing.
8990
return [];
9091
}
9192
$type_prefix = $name_arg->value->value;

src/Rules/Drupal/ModuleLoadInclude.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public function processNode(Node $node, Scope $scope): array
7272

7373
$module_name = $module_arg->value->value;
7474
if (!isset($modules[$module_name])) {
75+
// @todo return error that the module does not exist.
7576
return [];
7677
}
7778
$type_prefix = $name_arg->value->value;

src/Type/EntityTypeManagerGetStorageDynamicReturnTypeExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public function getTypeFromMethodCall(
7676
if (isset($this->entityTypeStorageMapping[$entityTypeId])) {
7777
return new ObjectType($this->entityTypeStorageMapping[$entityTypeId]);
7878
}
79+
// @todo get entity type class reflection and return proper storage for entity type
80+
// example: config storage, sqlcontententitystorage, etc.
7981
return $returnType;
8082
}
8183
}

tests/fixtures/drupal/modules/module_load_include_fixture/module_load_include_fixture.module

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ function test_locale_translation_inc_included() {
77
locale_translation_build_projects();
88
module_load_include('translation.inc', 'locale');
99
locale_translation_build_sources();
10+
module_load_include('translationzzzz.inc', 'locale');
1011
}

tests/fixtures/drupal/profiles/.gitkeep

Whitespace-only changes.

tests/fixtures/drupal/themes/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)