Test Failed
Pull Request — master (#37)
by Divine Niiquaye
02:59
created

AutowireTrait::autowire()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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