Passed
Push — master ( 697d7b...50775c )
by Satoshi
02:58
created

Component::getExtraPatterns()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 11
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\DependencyGraph\FullyQualifiedStructuralElementName;
7
use DependencyAnalyzer\DependencyGraph\FullyQualifiedStructuralElementName\Base as FQSEN;
8
use DependencyAnalyzer\DependencyGraph\StructuralElementPatternMatcher;
9
use DependencyAnalyzer\Exceptions\InvalidFullyQualifiedStructureElementNameException;
10
use DependencyAnalyzer\Exceptions\InvalidQualifiedNamePatternException;
11
12
class Component
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $name;
18
19
    /**
20
     * @var StructuralElementPatternMatcher
21
     */
22
    protected $matcher;
23
24
    /**
25
     * @var StructuralElementPatternMatcher
26
     */
27
    protected $publicMatcher;
28
29
    /**
30
     * @var StructuralElementPatternMatcher
31
     */
32
    protected $dependerMatcher;
33
34
    /**
35
     * @var StructuralElementPatternMatcher
36
     */
37
    protected $dependeeMatcher;
38
39
    /**
40
     * @var array
41
     */
42
    protected $attributes = [];
43
44
    /**
45
     * @var StructuralElementPatternMatcher[]
46
     */
47
    protected $extraPatterns = [];
48
49
    public function __construct(
50
        string $name,
51
        StructuralElementPatternMatcher $pattern,
52
        StructuralElementPatternMatcher $dependerPatterns = null,
53
        StructuralElementPatternMatcher $dependeePatterns = null,
54
        StructuralElementPatternMatcher $publicPattern = null,
55
        array $extraPatterns = null
56
    ) {
57
        $this->name = $name;
58
        $this->matcher = $pattern;
59
        $this->dependerMatcher = $dependerPatterns;
60
        $this->dependeeMatcher = $dependeePatterns;
61
        $this->publicMatcher = $publicPattern;
62
63
        if (!is_null($extraPatterns)) {
64
            try {
65
                foreach ($extraPatterns as $callee => $callerPattern) {
66
                    FullyQualifiedStructuralElementName::createFromString($callee);
67
                }
68
            } catch (InvalidFullyQualifiedStructureElementNameException $e) {
69
                throw new InvalidQualifiedNamePatternException($callee);
70
            }
71
            $this->extraPatterns = $extraPatterns;
72
        }
73
    }
74
75
    public function getName(): string
76
    {
77
        return $this->name;
78
    }
79
80
    public function getDefineMatcher(): StructuralElementPatternMatcher
81
    {
82
        return $this->matcher;
83
    }
84
85
    public function isBelongedTo(string $className): bool
86
    {
87
        return $this->matcher->isMatch($className);
88
    }
89
90
    public function verifyDepender(FQSEN $depender, FQSEN $dependee): bool
91
    {
92
        if ($this->isBelongedTo($depender->toString())) {
93
            return true;
94
        }
95
96
        if (!empty($extraPatterns = $this->getExtraPatterns($dependee))) {
97
            $ret = true;
98
            foreach ($extraPatterns as $extraPattern) {
99
                $ret = $ret && $extraPattern->isMatchWithFQSEN($depender);
100
            }
101
            return $ret;
102
        }
103
104
        return (
105
            (is_null($this->publicMatcher) ? true : $this->publicMatcher->isMatchWithFQSEN($dependee)) &&
106
            (is_null($this->dependerMatcher) ? true : $this->dependerMatcher->isMatchWithFQSEN($depender))
107
        );
108
    }
109
110
    public function verifyDependee(string $className): bool
111
    {
112
        if ($this->isBelongedTo($className)) {
113
            return true;
114
        } elseif (is_null($this->dependeeMatcher)) {
115
            return true;
116
        }
117
118
        return $this->dependeeMatcher->isMatch($className);
119
    }
120
121
    /**
122
     * @param string $className
123
     * @param StructuralElementPatternMatcher[] $patterns
124
     * @return bool
125
     */
126
    protected function checkPatterns(string $className, array $patterns): bool
127
    {
128
        foreach ($patterns as $pattern) {
129
            if ($pattern->isMatch($className)) {
130
                return true;
131
            }
132
        }
133
134
        return false;
135
    }
136
137
    /**
138
     * @param FQSEN $dependee
139
     * @return StructuralElementPatternMatcher[]
140
     */
141
    protected function getExtraPatterns(FQSEN $dependee): array
142
    {
143
        $ret = [];
144
        foreach ($this->extraPatterns as $callee => $callerPattern) {
145
            $calleeFQSEN = FullyQualifiedStructuralElementName::createFromString($callee);
146
            if ($calleeFQSEN->include($dependee)) {
147
                $ret[] = $this->extraPatterns[$callee];
148
            }
149
        }
150
151
        return $ret;
152
    }
153
154
    public function setAttribute(string $key, $name): void
155
    {
156
        $this->attributes[$key] = $name;
157
    }
158
159
    public function getAttribute(string $key)
160
    {
161
        return $this->attributes[$key] ?? null;
162
    }
163
164
    public function toArray()
165
    {
166
        $ret = [
167
            'define' => $this->matcher->toArray()
168
        ];
169
170
        if (!is_null($this->dependerMatcher)) {
171
            $ret['depender'] = $this->dependerMatcher->toArray();
172
        }
173
        if (!is_null($this->dependeeMatcher)) {
174
            $ret['dependee'] = $this->dependeeMatcher->toArray();
175
        }
176
        if (!is_null($this->publicMatcher)) {
177
            $ret['public'] = $this->publicMatcher->toArray();
178
        }
179
        if (!empty($this->extraPatterns)) {
180
            $ret['extra'] = [];
181
            foreach ($this->extraPatterns as $callee => $callerPattern) {
182
                $ret['extra'][$callee] = $callerPattern->toArray();
183
            }
184
        }
185
186
        return $ret;
187
    }
188
}
189