Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use TypeCombinator::union() to combine decorated service classes. #620

Merged
merged 3 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading