Test Failed
Pull Request — master (#37)
by Divine Niiquaye
13:18
created

AutowireTrait   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 39
c 6
b 0
f 0
dl 0
loc 102
rs 10
wmc 19

5 Methods

Rating   Name   Duplication   Size   Complexity  
A typed() 0 11 3
B triggerReturnType() 0 38 10
A autowire() 0 17 4
A getTypes() 0 3 1
A isTyped() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of DivineNii opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2021 DivineNii (https://divinenii.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Rade\DI\Definitions\Traits;
19
20
use PhpParser\Node\{Name, UnionType};
21
use Rade\DI\Resolver;
22
23
/**
24
 * This trait adds a autowiring functionality to the service definition.
25
 *
26
 * @author Divine Niiquaye Ibok <[email protected]>
27
 */
28
trait AutowireTrait
29
{
30
    /** @var array<int,string> */
31
    private array $types = [];
32
33
    private bool $autowired = false;
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function autowire(array $types = [])
39
    {
40
        if ([] === $types) {
41
            $types = Resolver::autowireService($this->getEntity(), false, $this->container ?? null);
0 ignored issues
show
Bug introduced by
It seems like getEntity() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
            $types = Resolver::autowireService($this->/** @scrutinizer ignore-call */ getEntity(), false, $this->container ?? null);
Loading history...
42
        }
43
44
        if (isset($this->container)) {
45
            foreach ($this->types as $typed) {
46
                $this->container->removeType($typed, $this->innerId);
47
            }
48
            $this->container->type($this->innerId, $types);
49
        }
50
51
        $this->autowired = true;
52
        $this->types = $types;
53
54
        return $this;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function typed(array $to)
61
    {
62
        if (isset($this->innerId)) {
63
            $this->container->type($this->innerId, $to);
64
        }
65
66
        foreach ($to as $typed) {
67
            $this->types[] = $typed;
68
        }
69
70
        return $this;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function isTyped(): bool
77
    {
78
        return !empty($this->types);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function getTypes(): array
85
    {
86
        return $this->types;
87
    }
88
89
    /**
90
     * Triggers a return type to definition's method.
91
     */
92
    public function triggerReturnType(\PhpParser\Builder\Method $defNode): void
93
    {
94
        $defTyped = [];
95
96
        foreach ($this->types as $offset => $typed) {
97
            if (\class_exists($typed)) {
98
                $defTyped[] = $typed;
99
100
                foreach (\array_slice($this->types, $offset + 1) as $interface) {
101
                    if (!\is_subclass_of($typed, $interface)) {
102
                        $defTyped[] = $interface;
103
                    }
104
                }
105
106
                break;
107
            }
108
        }
109
110
        if ([] === $defTyped) {
111
            $defTyped = $this->types;
112
113
            if (empty($defTyped)) {
114
                $this->types = Resolver::autowireService($this->getEntity(), true, $this->container ?? null);
115
116
                if (!empty($this->types)) {
117
                    $this->triggerReturnType($defNode);
118
                }
119
120
                return;
121
            }
122
        }
123
124
        $defTyped = \array_unique($defTyped); // Fix same type repeating.
125
126
        if (1 === count($defTyped) || \PHP_VERSION_ID < 80000) {
127
            $defNode->setReturnType(\current($defTyped));
128
        } else {
129
            $defNode->setReturnType(new UnionType(\array_map(fn ($type) => new Name($type), $defTyped)));
130
        }
131
    }
132
}
133