Completed
Push — master ( 5b7dfb...3e297a )
by David
12s
created

Extension::__construct()   C

Complexity

Conditions 8
Paths 36

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 17
nc 36
nop 1
dl 0
loc 26
rs 5.3846
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace TheCodingMachine\Funky\Annotations;
5
6
use Doctrine\Common\Annotations\AnnotationException;
7
8
/**
9
 * @Annotation
10
 * @Target({"METHOD"})
11
 * @Attributes({
12
 *   @Attribute("name", type = "string"),
13
 *   @Attribute("nameFromType", type = "bool"),
14
 *   @Attribute("nameFromMethodName", type = "bool"),
15
 * })
16
 */
17
class Extension
18
{
19
    /**
20
     * @var bool
21
     */
22
    private $nameFromType = false;
23
    /**
24
     * @var bool
25
     */
26
    private $nameFromMethodName = false;
27
    /**
28
     * @var string|null
29
     */
30
    private $name;
31
32
    /**
33
     * @param mixed[] $attributes
34
     */
35
    public function __construct(array $attributes = [])
36
    {
37
        $count = 0;
38
        if (isset($attributes['nameFromType'])) {
39
            $this->nameFromType = $attributes['nameFromType'];
40
            if ($this->nameFromType) {
41
                $count++;
42
            }
43
        }
44
        if (isset($attributes['nameFromMethodName'])) {
45
            $this->nameFromMethodName = $attributes['nameFromMethodName'];
46
            if ($this->nameFromMethodName) {
47
                $count++;
48
            }
49
        }
50
        if (isset($attributes['name'])) {
51
            $this->name = $attributes['name'];
52
            $count++;
53
        }
54
55
        if ($count === 0) {
56
            $this->nameFromType = true;
57
        }
58
        if ($count > 1) {
59
            throw new AnnotationException('Extension should have only one property in the list "name", "nameFromType", '
60
                .'"nameFromMethodName".');
61
        }
62
    }
63
64
    public function getName(): ?string
65
    {
66
        return $this->name;
67
    }
68
    public function isFromType(): bool
69
    {
70
        return $this->nameFromType;
71
    }
72
    public function isFromMethodName(): bool
73
    {
74
        return $this->nameFromMethodName;
75
    }
76
}
77