LazyServiceTrait::getAllServices()   B
last analyzed

Complexity

Conditions 7
Paths 8

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 14
nc 8
nop 0
dl 0
loc 22
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace pjpawel\LightApi\Container\LazyService;
4
5
use Psr\Container\ContainerInterface;
6
use ReflectionClass;
7
use ReflectionNamedType;
8
9
/** @deprecated  */
10
trait LazyServiceTrait
11
{
12
13
    public ContainerInterface $container;
14
15
    /**
16
     * @inheritDoc
17
     */
18
    public function setContainer(ContainerInterface $container): void
19
    {
20
        $this->container = $container;
21
    }
22
23
    /**
24
     * @inheritDoc
25
     */
26
    public static function getAllServices(): array
27
    {
28
        /** @var array<string,string> $services */
29
        $services = [];
30
        if (get_parent_class(self::class) &&
31
            method_exists(get_parent_class(self::class), __FUNCTION__)) {
32
            $services = parent::getLazyServices();
33
        }
34
        $reflectionClass = new ReflectionClass(self::class);
35
        foreach ($reflectionClass->getMethods() as $method) {
36
            $reflectionAttributes = $method->getAttributes(AsLazyService::class);
37
            if (!isset($reflectionAttributes[0])) {
38
                continue;
39
            }
40
            // Allow to set in AsLazyService is of service
41
            $returnType = $method->getReturnType();
42
            if (!$returnType instanceof ReflectionNamedType || $returnType->isBuiltin()) {
43
                throw new \Exception(sprintf('%s must return class', $method->getName()));
44
            }
45
            $services[self::class . '::' . $method->getName()] = $returnType->getName();
46
        }
47
        return $services;
48
    }
49
50
}