Completed
Push — master ( d10f54...00ced4 )
by
unknown
05:32
created

ClassDefinition::getMethodsCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php declare(strict_types = 1);
2
/**
3
 * Created by PhpStorm.
4
 * User: root
5
 * Date: 02.08.16
6
 * Time: 0:46.
7
 */
8
namespace samsonframework\container\definition;
9
10
use samsonframework\container\definition\reference\ReferenceInterface;
11
use samsonframework\container\definition\scope\AbstractScope;
12
use samsonframework\container\definition\exception\MethodDefinitionAlreadyExistsException;
13
use samsonframework\container\definition\exception\PropertyDefinitionAlreadyExistsException;
14
use samsonframework\container\definition\exception\ScopeAlreadyExistsException;
15
use samsonframework\container\definition\exception\ScopeNotFoundException;
16
17
/**
18
 * Class ClassDefinition
19
 *
20
 * @author Ruslan Molodyko <[email protected]>
21
 */
22
class ClassDefinition extends AbstractDefinition implements ClassBuilderInterface
23
{
24
    /** @var string Class name with namespace */
25
    protected $className;
26
    /** @var string Class name space */
27
    protected $nameSpace;
28
    /** @var string Service name */
29
    protected $serviceName;
30
    /** @var array Class container scopes */
31
    protected $scopes = [];
32
    /** @var bool Is singleton */
33
    protected $isSingleton = false;
34
    /** @var bool Is class definition was analyzed */
35
    protected $isAnalyzed = false;
36
37
    /** @var MethodDefinition[] Methods collection */
38
    protected $methodsCollection = [];
39
    /** @var PropertyDefinition[] Property collection */
40
    protected $propertiesCollection = [];
41
42
    /** {@inheritdoc} */
43 8
    public function defineConstructor(): MethodBuilderInterface
44
    {
45
        /** Add constructor method manually */
46 8
        return $this->defineMethod('__construct');
47
    }
48
49
    /** {@inheritdoc} */
50 13
    public function defineMethod(string $methodName): MethodBuilderInterface
51
    {
52 13
        if (array_key_exists($methodName, $this->methodsCollection)) {
53 2
            throw new MethodDefinitionAlreadyExistsException();
54
        }
55
56 13
        $methodDefinition = new MethodDefinition($this, $methodName);
0 ignored issues
show
Unused Code introduced by
The call to MethodDefinition::__construct() has too many arguments starting with $methodName.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
57 13
        $methodDefinition->setMethodName($methodName);
58
59 13
        $this->methodsCollection[$methodName] = $methodDefinition;
60
61 13
        return $methodDefinition;
62
    }
63
64
    /** {@inheritdoc} */
65 9
    public function defineProperty(string $propertyName): PropertyBuilderInterface
66
    {
67 9
        if (array_key_exists($propertyName, $this->propertiesCollection)) {
68 1
            throw new PropertyDefinitionAlreadyExistsException();
69
        }
70
71 9
        $propertyDefinition = new PropertyDefinition($this);
72 9
        $propertyDefinition->setPropertyName($propertyName);
73
74 9
        $this->propertiesCollection[$propertyName] = $propertyDefinition;
75
76 9
        return $propertyDefinition;
77
    }
78
79
    /** {@inheritdoc} */
80 2
    public function defineIsPrototype(): ClassBuilderInterface
81
    {
82 2
        $this->isSingleton = false;
83
84 2
        return $this;
85
    }
86
87
    /** {@inheritdoc} */
88 2
    public function defineIsSingleton(): ClassBuilderInterface
89
    {
90 2
        $this->isSingleton = true;
91
92 2
        return $this;
93
    }
94
95
    /**
96
     * Get namespace
97
     *
98
     * @return string
99
     */
100 2
    public function getNameSpace(): string
101
    {
102 2
        return $this->nameSpace;
103
    }
104
105
    /**
106
     * @param string $nameSpace
107
     * @return ClassDefinition
108
     */
109 8
    public function setNameSpace(string $nameSpace): ClassDefinition
110
    {
111 8
        $this->nameSpace = $nameSpace;
112
113 8
        return $this;
114
    }
115
116
    /**
117
     * Add scope to definition
118
     *
119
     * @param AbstractScope $scope
120
     * @return ClassDefinition
121
     * @throws ScopeAlreadyExistsException
122
     */
123 4
    public function addScope(AbstractScope $scope): ClassDefinition
124
    {
125 4
        if ($this->hasScope($scope::getId())) {
126 1
            throw new ScopeAlreadyExistsException();
127
        }
128
129 4
        $this->scopes[$scope::getId()] = $scope;
130
131 4
        return $this;
132
    }
133
134
    /**
135
     * Remove scope from definition
136
     *
137
     * @param string $id
138
     * @return ClassDefinition
139
     * @throws ScopeNotFoundException
140
     */
141 2
    public function removeScope(string $id): ClassDefinition
142
    {
143 2
        if (!$this->hasScope($id)) {
144 1
            throw new ScopeNotFoundException();
145
        }
146
147 1
        unset($this->scopes[$id]);
148
149 1
        return $this;
150
    }
151
152
    /**
153
     * Check if scope exists in definition
154
     *
155
     * @param string $id
156
     * @return bool
157
     */
158 4
    public function hasScope(string $id): bool
159
    {
160 4
        return array_key_exists($id, $this->scopes);
161
    }
162
163
    /**
164
     * Get scope from definition
165
     *
166
     * @param string $id
167
     * @return mixed
168
     * @throws ScopeNotFoundException
169
     */
170 2
    public function getScope(string $id): AbstractScope
171
    {
172 2
        if (!$this->hasScope($id)) {
173 1
            throw new ScopeNotFoundException();
174
        }
175 1
        return $this->scopes[$id];
176
    }
177
178
    /**
179
     * Get all scopes
180
     *
181
     * @return AbstractScope[]
182
     */
183 1
    public function getScopes(): array
184
    {
185 1
        return $this->scopes;
186
    }
187
188
    /**
189
     * Get class name
190
     *
191
     * @return string
192
     */
193 8
    public function getClassName(): string
194
    {
195 8
        return $this->className;
196
    }
197
198
    /**
199
     * @param string $className
200
     * @return ClassDefinition
201
     */
202 28
    public function setClassName(string $className): ClassDefinition
203
    {
204 28
        $this->className = $className;
205
206 28
        return $this;
207
    }
208
209
    /**
210
     * @return string|null
211
     */
212 3
    public function getServiceName()
213
    {
214 3
        return $this->serviceName;
215
    }
216
217
    /**
218
     * @param string $serviceName
219
     * @return ClassDefinition
220
     */
221 2
    public function setServiceName(string $serviceName): ClassDefinition
222
    {
223 2
        $this->serviceName = $serviceName;
224
225 2
        return $this;
226
    }
227
228
    /**
229
     * @return boolean
230
     */
231 4
    public function isSingleton(): bool
232
    {
233 4
        return $this->isSingleton;
234
    }
235
236
    /**
237
     * @param boolean $isSingleton
238
     * @return ClassDefinition
239
     */
240 1
    public function setIsSingleton(bool $isSingleton): ClassDefinition
241
    {
242 1
        $this->isSingleton = $isSingleton;
243
244 1
        return $this;
245
    }
246
247
    /**
248
     * @return PropertyDefinition[]
249
     */
250 8
    public function getPropertiesCollection(): array
251
    {
252 8
        return $this->propertiesCollection;
253
    }
254
255
    /**
256
     * @return MethodDefinition[]
257
     */
258 8
    public function getMethodsCollection(): array
259
    {
260 8
        return $this->methodsCollection;
261
    }
262
263
    /**
264
     * @return boolean
265
     */
266 7
    public function isAnalyzed(): bool
267
    {
268 7
        return $this->isAnalyzed;
269
    }
270
271
    /**
272
     * @param boolean $isAnalyzed
273
     * @return ClassDefinition
274
     */
275 6
    public function setIsAnalyzed(bool $isAnalyzed): ClassDefinition
276
    {
277 6
        $this->isAnalyzed = $isAnalyzed;
278
279 6
        return $this;
280
    }
281
}
282