Skip to content

Commit 20e4bfc

Browse files
fix: Refactor component handling in ServiceMap to improve variable naming and streamline logic. (#28)
1 parent 82c53d0 commit 20e4bfc

File tree

5 files changed

+13
-32
lines changed

5 files changed

+13
-32
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Enh #25: Add support for PHPStan Extension Installer (@samuelrajan747)
66
- Enh #26: Add PHPStan extension installer instructions and improve `ServiceMap` configuration handling (@terabytesoftw)
77
- Bug #27: Enhance error handling in `ServiceMap` for invalid configuration structures and add corresponding test cases (@terabytesoftw)
8+
- Bug #28: Refactor component handling in `ServiceMap` to improve variable naming and streamline logic (@terabytesoftw)
89

910
## 0.2.2 June 04, 2025
1011

src/ServiceMap.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -270,24 +270,20 @@ private function processComponents(array $config): void
270270
if ($config !== []) {
271271
$components = $config['components'] ?? [];
272272

273-
foreach ($components as $id => $component) {
273+
foreach ($components as $id => $definition) {
274274
if (is_string($id) === false) {
275275
$this->throwErrorWhenIdIsNotString('Component', gettype($id));
276276
}
277277

278-
if (is_object($component)) {
279-
$this->components[$id] = get_class($component);
278+
if (is_object($definition)) {
279+
$this->components[$id] = get_class($definition);
280280

281281
continue;
282282
}
283283

284-
if (isset($component['class']) && is_string($component['class']) && $component['class'] !== '') {
285-
$this->components[$id] = $component['class'];
286-
287-
continue;
284+
if (isset($definition['class']) && is_string($definition['class']) && $definition['class'] !== '') {
285+
$this->components[$id] = $definition['class'];
288286
}
289-
290-
$this->throwErrorWhenUnsupportedDefinition($id);
291287
}
292288
}
293289
}

tests/ServiceMapTest.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ public function testItLoadsServicesAndComponents(): void
120120
$serviceMap->getComponentClassById('customInitializedComponent'),
121121
'ServiceMap should resolve component id \'customInitializedComponent\' to \'MyActiveRecord::class\'.',
122122
);
123+
$this->assertNull(
124+
$serviceMap->getComponentClassById('assetManager'),
125+
'ServiceMap should return \'null\' for \'assetManager\' component id as it is not a class but an array.',
126+
);
123127
}
124128

125129
public function testItAllowsWithoutEmptyConfigPath(): void
@@ -164,20 +168,6 @@ public function testThrowRuntimeExceptionWhenComponentsHasUnsupportedIdNotString
164168
new ServiceMap($fixturePath);
165169
}
166170

167-
/**
168-
* @throws ReflectionException if the service definition is invalid or can't be resolved.
169-
*/
170-
public function testThrowRuntimeExceptionWhenComponentsHasUnsupportedTypeArrayInvalidValue(): void
171-
{
172-
$ds = DIRECTORY_SEPARATOR;
173-
$fixturePath = __DIR__ . "{$ds}fixture{$ds}components-unsupported-type-array-invalid.php";
174-
175-
$this->expectException(RuntimeException::class);
176-
$this->expectExceptionMessage('Unsupported definition for \'unsupported-array-invalid\'.');
177-
178-
new ServiceMap($fixturePath);
179-
}
180-
181171
public function testThrowRuntimeExceptionWhenConfigPathFileDoesNotExist(): void
182172
{
183173
$this->expectException(InvalidArgumentException::class);

tests/fixture/components-unsupported-type-array-invalid.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/fixture/config.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
return [
88
'components' => [
9+
'assetManager' => [
10+
'basePath' => '@runtime/assets',
11+
],
912
'customComponent' => [
1013
'class' => MyActiveRecord::class,
1114
],

0 commit comments

Comments
 (0)