Passed
Push — master ( 029172...056350 )
by Satoshi
02:16
created

Component::getAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace DependencyAnalyzer\Inspector\RuleViolationDetector;
5
6
use DependencyAnalyzer\Matcher\ClassNameMatcher;
7
8
class Component
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $name;
14
15
    /**
16
     * @var ClassNameMatcher
17
     */
18
    protected $matcher;
19
20
    /**
21
     * @var ClassNameMatcher
22
     */
23
    protected $dependerMatcher;
24
25
    /**
26
     * @var ClassNameMatcher
27
     */
28
    protected $dependeeMatcher;
29
30
    /**
31
     * @var array
32
     */
33
    protected $attributes = [];
34
35
    /**
36
     * @param string $name
37
     * @param ClassNameMatcher $pattern
38
     * @param ClassNameMatcher $dependerPatterns
39
     * @param ClassNameMatcher $dependeePatterns
40
     */
41
    public function __construct(string $name, ClassNameMatcher $pattern, ClassNameMatcher $dependerPatterns = null, ClassNameMatcher $dependeePatterns = null)
42
    {
43
        $this->name = $name;
44
        $this->matcher = $pattern;
45
        $this->dependerMatcher = $dependerPatterns;
46
        $this->dependeeMatcher = $dependeePatterns;
47
    }
48
49
    public function getName(): string
50
    {
51
        return $this->name;
52
    }
53
54
    public function getDefineMatcher(): ClassNameMatcher
55
    {
56
        return $this->matcher;
57
    }
58
59
    public function isBelongedTo(string $className): bool
60
    {
61
        return $this->matcher->isMatch($className);
62
    }
63
64
    public function verifyDepender(string $className): bool
65
    {
66
        if ($this->isBelongedTo($className)) {
67
            return true;
68
        } elseif (is_null($this->dependerMatcher)) {
69
            return true;
70
        }
71
72
        return $this->dependerMatcher->isMatch($className);
73
//        return $this->checkPatterns($className, $this->dependerPatterns);
74
    }
75
76
    public function verifyDependee(string $className): bool
77
    {
78
        if ($this->isBelongedTo($className)) {
79
            return true;
80
        } elseif (is_null($this->dependeeMatcher)) {
81
            return true;
82
        }
83
84
        return $this->dependeeMatcher->isMatch($className);
85
//        return $this->checkPatterns($className, $this->dependeePatterns);
86
    }
87
88
    /**
89
     * @param string $className
90
     * @param ClassNameMatcher[] $patterns
91
     * @return bool
92
     */
93
    protected function checkPatterns(string $className, array $patterns): bool
94
    {
95
        foreach ($patterns as $pattern) {
96
            if ($pattern->isMatch($className)) {
97
                return true;
98
            }
99
        }
100
101
        return false;
102
    }
103
104
    public function setAttribute(string $key, $name): void
105
    {
106
        $this->attributes[$key] = $name;
107
    }
108
109
    public function getAttribute(string $key)
110
    {
111
        return $this->attributes[$key] ?? null;
112
    }
113
114
    public function toArray()
115
    {
116
        $ret = [
117
            'define' => $this->matcher->toArray()
118
        ];
119
120
        if (!is_null($this->dependerMatcher)) {
121
            $ret['depender'] = $this->dependerMatcher->toArray();
122
        }
123
        if (!is_null($this->dependeeMatcher)) {
124
            $ret['dependee'] = $this->dependeeMatcher->toArray();
125
        }
126
127
        return $ret;
128
    }
129
}
130