AbstractAnnotation::__construct()   B
last analyzed

Complexity

Conditions 10
Paths 108

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 19
nc 108
nop 1
dl 0
loc 30
rs 7.6
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
namespace TheCodingMachine\Funky\Annotations;
5
6
use Doctrine\Common\Annotations\AnnotationException;
7
8
abstract class AbstractAnnotation
9
{
10
    /**
11
     * @var bool
12
     */
13
    private $nameFromType = false;
14
    /**
15
     * @var bool
16
     */
17
    private $nameFromMethodName = false;
18
    /**
19
     * @var string|null
20
     */
21
    private $name;
22
    /**
23
     * @var Tag[]
24
     */
25
    private $tags = [];
26
27
    /**
28
     * @param mixed[] $attributes
29
     */
30
    public function __construct(array $attributes = [])
31
    {
32
        $count = 0;
33
        if (isset($attributes['nameFromType'])) {
34
            $this->nameFromType = $attributes['nameFromType'];
35
            if ($this->nameFromType) {
36
                $count++;
37
            }
38
        }
39
        if (isset($attributes['nameFromMethodName'])) {
40
            $this->nameFromMethodName = $attributes['nameFromMethodName'];
41
            if ($this->nameFromMethodName) {
42
                $count++;
43
            }
44
        }
45
        if (isset($attributes['name'])) {
46
            $this->name = $attributes['name'];
47
            $count++;
48
        }
49
50
        if ($count === 0) {
51
            $this->nameFromType = true;
52
        }
53
        if ($count > 1) {
54
            throw new AnnotationException('There should be only one property in the list "name", "nameFromType", '
55
                .'"nameFromMethodName". You can add aliases if you need several names.');
56
        }
57
58
        if (isset($attributes['tags']) && \is_array($attributes['tags'])) {
59
            $this->setTags(...$attributes['tags']);
60
        }
61
    }
62
63
    /**
64
     * Helper method to ensure that the passed tags are of Tag type.
65
     *
66
     * @param Tag[] $tags
67
     */
68
    private function setTags(Tag ...$tags): void
69
    {
70
        $this->tags = $tags;
71
    }
72
    /**
73
     * Returns the list of tags for the bean instance. Returns an empty array when no tag was set.
74
     *
75
     * @return Tag[]
76
     */
77
    public function getTags(): array
78
    {
79
        return $this->tags;
80
    }
81
    public function getName(): ?string
82
    {
83
        return $this->name;
84
    }
85
    public function isFromType(): bool
86
    {
87
        return $this->nameFromType;
88
    }
89
    public function isFromMethodName(): bool
90
    {
91
        return $this->nameFromMethodName;
92
    }
93
}
94