Skip to content

Commit

Permalink
Use TypeCombinator::union() to combine decorated service classes. (mg…
Browse files Browse the repository at this point in the history
…laman#620)

* Can not Union a UnionType.

* Refactor tests to only validate decorators are present.

* Use array unpacking notation.
  • Loading branch information
cmlara authored Nov 9, 2023
1 parent 0c7e48a commit b7039ae
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/Drupal/DrupalServiceDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use PHPStan\Type\TypeCombinator;

class DrupalServiceDefinition
{
Expand Down Expand Up @@ -122,7 +122,7 @@ public function getType(): Type
foreach ($decorating_services as $service_id => $service_definition) {
$combined_services[] = $service_definition->getType();
}
return new UnionType($combined_services);
return TypeCombinator::union(...$combined_services);
}
return new ObjectType($this->getClass() ?? $this->id);
}
Expand Down
22 changes: 13 additions & 9 deletions tests/src/ServiceMapFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use Drupal\Core\Logger\LoggerChannel;
use mglaman\PHPStanDrupal\Drupal\DrupalServiceDefinition;
use mglaman\PHPStanDrupal\Drupal\ServiceMap;
use PHPStan\Type\ObjectType;
use PHPStan\Type\UnionType;
use PHPUnit\Framework\TestCase;

final class ServiceMapFactoryTest extends TestCase
Expand All @@ -16,7 +14,9 @@ final class ServiceMapFactoryTest extends TestCase
* @dataProvider getServiceProvider
*
* @covers \mglaman\PHPStanDrupal\Drupal\DrupalServiceDefinition::__construct
* @covers \mglaman\PHPStanDrupal\Drupal\DrupalServiceDefinition::addDecorator
* @covers \mglaman\PHPStanDrupal\Drupal\DrupalServiceDefinition::getClass
* @covers \mglaman\PHPStanDrupal\Drupal\DrupalServiceDefinition::getDecorators
* @covers \mglaman\PHPStanDrupal\Drupal\DrupalServiceDefinition::isPublic
* @covers \mglaman\PHPStanDrupal\Drupal\DrupalServiceDefinition::getAlias
* @covers \mglaman\PHPStanDrupal\Drupal\DrupalServiceDefinition::getId
Expand Down Expand Up @@ -103,7 +103,11 @@ public function testFactory(string $id, callable $validator): void
'service_map.deocrating_base' => [
'decorates' => 'service_map.base_to_be_decorated',
'class' => 'Drupal\service_map\SecondBase',
]
],
'service_map.decorates_decorating_base' => [
'decorates' => 'service_map.deocrating_base',
'class' => 'Drupal\service_map\Override',
],
]);
$validator($service->getService($id));
}
Expand Down Expand Up @@ -225,12 +229,12 @@ function (DrupalServiceDefinition $service): void {
yield [
'service_map.base_to_be_decorated',
function (DrupalServiceDefinition $service): void {
$combined_class = [
new ObjectType('Drupal\service_map\Base'),
new ObjectType('Drupal\service_map\SecondBase')
];
$expected_class = new UnionType($combined_class);
self::assertEquals($expected_class, $service->getType());
$decorators = $service->getDecorators();
self::assertCount(1, $decorators);
self::assertArrayHasKey('service_map.deocrating_base', $decorators);
$child_decorators = $decorators['service_map.deocrating_base']->getDecorators();
self::assertCount(1, $child_decorators);
self::assertArrayHasKey('service_map.decorates_decorating_base', $child_decorators);
}
];
}
Expand Down

0 comments on commit b7039ae

Please sign in to comment.