Inject   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 99
rs 10
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getFullClassName() 0 11 3
A saveValue() 0 5 1
A setAttr() 0 3 1
A setTypes() 0 3 1
A __construct() 0 3 1
A setTarget() 0 3 1
A validate() 0 15 6
1
<?php
2
3
/**
4
 * Inject.php
5
 *
6
 * Jaxon attribute.
7
 * Specifies attributes to inject into a callable object.
8
 *
9
 * @package jaxon-attributes
10
 * @author Thierry Feuzeu <[email protected]>
11
 * @copyright 2024 Thierry Feuzeu <[email protected]>
12
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
13
 * @link https://github.com/jaxon-php/jaxon-core
14
 */
15
16
namespace Jaxon\Attributes\Attribute;
17
18
use Jaxon\App\Metadata\Metadata;
19
use Jaxon\Exception\SetupException;
20
use Attribute;
21
22
use function ltrim;
23
24
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY |
25
    Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
26
class Inject extends AbstractAttribute
27
{
28
    /**
29
     * @var int
30
     */
31
    protected $nTarget;
32
33
    /**
34
     * @var array
35
     */
36
    protected array $aTypes;
37
38
    /**
39
     * @param string|null $type
40
     * @param string|null $attr
41
     */
42
    public function __construct(protected string|null $type = null,
43
        protected string|null $attr = null)
44
    {}
45
46
    /**
47
     * @param int $nTarget
48
     *
49
     * @return void
50
     */
51
    public function setTarget(int $nTarget): void
52
    {
53
        $this->nTarget = $nTarget;
54
    }
55
56
    /**
57
     * @param string $sAttr
58
     *
59
     * @return void
60
     */
61
    public function setAttr(string $sAttr): void
62
    {
63
        $this->attr = $sAttr;
64
    }
65
66
    /**
67
     * @param array $aTypes
68
     *
69
     * @return void
70
     */
71
    public function setTypes(array $aTypes): void
72
    {
73
        $this->aTypes = $aTypes;
74
    }
75
76
    /**
77
     * @return void
78
     */
79
    public function validate(): void
80
    {
81
        if($this->nTarget === Attribute::TARGET_CLASS)
82
        {
83
            if(!$this->attr || !$this->type)
84
            {
85
                throw new SetupException('When applied to a class, the Inject attribute requires two arguments.');
86
            }
87
            return;
88
        }
89
        if($this->nTarget === Attribute::TARGET_METHOD)
90
        {
91
            if(!$this->attr)
92
            {
93
                throw new SetupException('When applied to a method, the Inject attribute requires the "attr" argument.');
94
            }
95
        }
96
    }
97
98
    /**
99
     * @return void
100
     */
101
    private function getFullClassName(): void
102
    {
103
        if(!$this->type)
104
        {
105
            // If no type is provided, take the attribute type.
106
            $this->type = $this->aTypes[$this->attr] ?? '';
107
            return;
108
        }
109
        if($this->type[0] === '\\')
110
        {
111
            $this->type = ltrim($this->type, '\\');
112
        }
113
    }
114
115
    /**
116
     * @inheritDoc
117
     */
118
    public function saveValue(Metadata $xMetadata, string $sMethod = '*'): void
119
    {
120
        $this->validate();
121
        $this->getFullClassName();
122
        $xMetadata->container($sMethod)->addValue($this->attr, $this->type);
0 ignored issues
show
Bug introduced by
It seems like $this->type can also be of type null; however, parameter $sClass of Jaxon\App\Metadata\Data\ContainerData::addValue() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

122
        $xMetadata->container($sMethod)->addValue($this->attr, /** @scrutinizer ignore-type */ $this->type);
Loading history...
Bug introduced by
It seems like $this->attr can also be of type null; however, parameter $sAttr of Jaxon\App\Metadata\Data\ContainerData::addValue() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

122
        $xMetadata->container($sMethod)->addValue(/** @scrutinizer ignore-type */ $this->attr, $this->type);
Loading history...
123
    }
124
}
125