ApiEvent   F
last analyzed

Complexity

Total Complexity 64

Size/Duplication

Total Lines 405
Duplicated Lines 0 %

Coupling/Cohesion

Components 5
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 64
lcom 5
cbo 1
dl 0
loc 405
ccs 128
cts 128
cp 1
rs 3.28
c 0
b 0
f 0

34 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A hasObjects() 0 6 1
A getObjects() 0 16 4
A hasSources() 0 4 1
A getSources() 0 4 1
A setSources() 0 5 1
A addSources() 0 6 2
A hasSource() 0 4 1
A addSource() 0 6 2
A removeSource() 0 5 2
A hasLibraries() 0 4 1
A getLibraries() 0 4 1
A setLibraries() 0 5 1
A addLibraries() 0 6 2
A hasLibrary() 0 4 1
A addLibrary() 0 6 2
A removeLibrary() 0 5 2
A hasCallbacks() 0 4 1
A getCallbacks() 0 4 1
A hasCallback() 0 10 5
A hasCallbackObject() 0 4 3
A getCallback() 0 4 2
A getCallbackObject() 0 8 3
A addCallback() 0 6 2
A removeCallbackObject() 0 4 1
A removeCallback() 0 6 2
A hasRequirements() 0 10 2
A getRequirements() 0 4 1
A getRequirementsObject() 0 4 2
A setRequirements() 0 5 1
A addRequirements() 0 6 2
A hasRequirement() 0 5 3
A addRequirement() 0 13 3
A removeRequirement() 0 19 4

How to fix   Complexity   

Complex Class

Complex classes like ApiEvent often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ApiEvent, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * This file is part of the Ivory Google Map package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\GoogleMap\Helper\Event;
13
14
/**
15
 * @author GeLo <[email protected]>
16
 */
17
class ApiEvent extends AbstractEvent
18
{
19
    /**
20
     * @var object[]
21
     */
22
    private $objects;
23
24
    /**
25
     * @var string[]
26
     */
27
    private $sources = [];
28
29
    /**
30
     * @var string[]
31
     */
32
    private $libraries = [];
33
34
    /**
35
     * @var \SplObjectStorage
36
     */
37
    private $callbacks;
38
39
    /**
40
     * @var \SplObjectStorage
41
     */
42
    private $requirements;
43
44
    /**
45
     * @param object[] $objects
46
     */
47 68
    public function __construct(array $objects)
48
    {
49 68
        $this->objects = $objects;
50 68
        $this->callbacks = new \SplObjectStorage();
51 68
        $this->requirements = new \SplObjectStorage();
52 68
    }
53
54
    /**
55
     * @param string|null $class
56
     *
57
     * @return bool
58
     */
59 8
    public function hasObjects($class = null)
60
    {
61 8
        $objects = $this->getObjects($class);
62
63 8
        return !empty($objects);
64
    }
65
66
    /**
67
     * @param string|null $class
68
     *
69
     * @return object[]
70
     */
71 8
    public function getObjects($class = null)
72
    {
73 8
        if (null === $class) {
74 4
            return $this->objects;
75
        }
76
77 4
        $objects = [];
78
79 4
        foreach ($this->objects as $object) {
80 4
            if ($object instanceof $class) {
81 4
                $objects[] = $object;
82
            }
83
        }
84
85 4
        return $objects;
86
    }
87
88
    /**
89
     * @return bool
90
     */
91 20
    public function hasSources()
92
    {
93 20
        return !empty($this->sources);
94
    }
95
96
    /**
97
     * @return string[]
98
     */
99 20
    public function getSources()
100
    {
101 20
        return $this->sources;
102
    }
103
104
    /**
105
     * @param string[] $sources
106
     */
107 8
    public function setSources(array $sources)
108
    {
109 8
        $this->sources = [];
110 8
        $this->addSources($sources);
111 8
    }
112
113
    /**
114
     * @param string[] $sources
115
     */
116 8
    public function addSources(array $sources)
117
    {
118 8
        foreach ($sources as $source) {
119 8
            $this->addSource($source);
120
        }
121 8
    }
122
123
    /**
124
     * @param string $source
125
     *
126
     * @return bool
127
     */
128 16
    public function hasSource($source)
129
    {
130 16
        return in_array($source, $this->sources, true);
131
    }
132
133
    /**
134
     * @param string $source
135
     */
136 16
    public function addSource($source)
137
    {
138 16
        if (!$this->hasSource($source)) {
139 16
            $this->sources[] = $source;
140
        }
141 16
    }
142
143
    /**
144
     * @param string $source
145
     */
146 4
    public function removeSource($source)
147
    {
148 4
        unset($this->sources[array_search($source, $this->sources, true)]);
149 4
        $this->sources = empty($this->sources) ? [] : array_values($this->sources);
150 4
    }
151
152
    /**
153
     * @return bool
154
     */
155 20
    public function hasLibraries()
156
    {
157 20
        return !empty($this->libraries);
158
    }
159
160
    /**
161
     * @return string[]
162
     */
163 20
    public function getLibraries()
164
    {
165 20
        return $this->libraries;
166
    }
167
168
    /**
169
     * @param string[] $libraries
170
     */
171 8
    public function setLibraries(array $libraries)
172
    {
173 8
        $this->libraries = [];
174 8
        $this->addLibraries($libraries);
175 8
    }
176
177
    /**
178
     * @param string[] $libraries
179
     */
180 8
    public function addLibraries(array $libraries)
181
    {
182 8
        foreach ($libraries as $library) {
183 8
            $this->addLibrary($library);
184
        }
185 8
    }
186
187
    /**
188
     * @param string $library
189
     *
190
     * @return bool
191
     */
192 16
    public function hasLibrary($library)
193
    {
194 16
        return in_array($library, $this->libraries, true);
195
    }
196
197
    /**
198
     * @param string $library
199
     */
200 16
    public function addLibrary($library)
201
    {
202 16
        if (!$this->hasLibrary($library)) {
203 16
            $this->libraries[] = $library;
204
        }
205 16
    }
206
207
    /**
208
     * @param string $library
209
     */
210 4
    public function removeLibrary($library)
211
    {
212 4
        unset($this->libraries[array_search($library, $this->libraries, true)]);
213 4
        $this->libraries = empty($this->libraries) ? [] : array_values($this->libraries);
214 4
    }
215
216
    /**
217
     * @return bool
218
     */
219 12
    public function hasCallbacks()
220
    {
221 12
        return count($this->callbacks) > 0;
222
    }
223
224
    /**
225
     * @return \SplObjectStorage
226
     */
227 4
    public function getCallbacks()
228
    {
229 4
        return $this->callbacks;
230
    }
231
232
    /**
233
     * @param string      $callback
234
     * @param object|null $object
235
     *
236
     * @return bool
237
     */
238 8
    public function hasCallback($callback, $object = null)
239
    {
240 8
        foreach ($this->callbacks as $rawObject) {
241 8
            if ($this->callbacks[$rawObject] === $callback && (null === $object || $object === $rawObject)) {
242 8
                return true;
243
            }
244
        }
245
246 8
        return false;
247
    }
248
249
    /**
250
     * @param object      $object
251
     * @param string|null $callback
252
     *
253
     * @return bool
254
     */
255 8
    public function hasCallbackObject($object, $callback = null)
256
    {
257 8
        return isset($this->callbacks[$object]) && (null === $callback || $this->callbacks[$object] === $callback);
258
    }
259
260
    /**
261
     * @param $object
262
     *
263
     * @return string|null
264
     */
265 8
    public function getCallback($object)
266
    {
267 8
        return $this->hasCallbackObject($object) ? $this->callbacks[$object] : null;
268
    }
269
270
    /**
271
     * @param string $callback
272
     *
273
     * @return object
274
     */
275 8
    public function getCallbackObject($callback)
276
    {
277 8
        foreach ($this->callbacks as $object) {
278 8
            if ($this->callbacks[$object] === $callback) {
279 8
                return $object;
280
            }
281
        }
282 4
    }
283
284
    /**
285
     * @param object $object
286
     * @param string $callback
287
     */
288 8
    public function addCallback($object, $callback)
289
    {
290 8
        if (!$this->hasCallback($callback, $object)) {
291 8
            $this->callbacks[$object] = $callback;
292
        }
293 8
    }
294
295
    /**
296
     * @param object $object
297
     */
298 4
    public function removeCallbackObject($object)
299
    {
300 4
        unset($this->callbacks[$object]);
301 4
    }
302
303
    /**
304
     * @param string $callback
305
     */
306 4
    public function removeCallback($callback)
307
    {
308 4
        if ($this->hasCallback($callback)) {
309 4
            $this->removeCallbackObject($this->getCallbackObject($callback));
0 ignored issues
show
Bug introduced by
It seems like $this->getCallbackObject($callback) targeting Ivory\GoogleMap\Helper\E...nt::getCallbackObject() can also be of type null; however, Ivory\GoogleMap\Helper\E...:removeCallbackObject() does only seem to accept object, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
310
        }
311 4
    }
312
313
    /**
314
     * @param object|null $object
315
     *
316
     * @return bool
317
     */
318 20
    public function hasRequirements($object = null)
319
    {
320 20
        if (null === $object) {
321 20
            return count($this->requirements) > 0;
322
        }
323
324 16
        $requirements = $this->getRequirementsObject($object);
325
326 16
        return !empty($requirements);
327
    }
328
329
    /**
330
     * @return \SplObjectStorage
331
     */
332 4
    public function getRequirements()
333
    {
334 4
        return $this->requirements;
335
    }
336
337
    /**
338
     * @param object $object
339
     *
340
     * @return string[]
341
     */
342 16
    public function getRequirementsObject($object)
343
    {
344 16
        return $this->hasRequirement($object) ? $this->requirements[$object] : [];
345
    }
346
347
    /**
348
     * @param object   $object
349
     * @param string[] $requirements
350
     */
351 8
    public function setRequirements($object, array $requirements)
352
    {
353 8
        $this->requirements = new \SplObjectStorage();
354 8
        $this->addRequirements($object, $requirements);
355 8
    }
356
357
    /**
358
     * @param object   $object
359
     * @param string[] $requirements
360
     */
361 8
    public function addRequirements($object, array $requirements)
362
    {
363 8
        foreach ($requirements as $requirement) {
364 8
            $this->addRequirement($object, $requirement);
365
        }
366 8
    }
367
368
    /**
369
     * @param object      $object
370
     * @param string|null $requirement
371
     *
372
     * @return bool
373
     */
374 16
    public function hasRequirement($object, $requirement = null)
375
    {
376 16
        return isset($this->requirements[$object])
377 16
            && (null === $requirement || in_array($requirement, $this->requirements[$object], true));
378
    }
379
380
    /**
381
     * @param object $object
382
     * @param string $requirement
383
     */
384 16
    public function addRequirement($object, $requirement)
385
    {
386 16
        if (!$this->hasRequirement($object)) {
387 16
            $this->requirements[$object] = [];
388
        }
389
390 16
        if (!$this->hasRequirement($object, $requirement)) {
391 16
            $this->requirements[$object] = array_merge(
392 16
                $this->requirements[$object],
393 16
                [$requirement]
394
            );
395
        }
396 16
    }
397
398
    /**
399
     * @param object      $object
400
     * @param string|null $requirement
401
     */
402 4
    public function removeRequirement($object, $requirement = null)
403
    {
404 4
        if (null === $requirement) {
405 4
            unset($this->requirements[$object]);
406
407 4
            return;
408
        }
409
410 4
        if ($this->hasRequirement($object, $requirement)) {
411 4
            $requirements = $this->requirements[$object];
412 4
            unset($requirements[array_search($requirement, $requirements, true)]);
413
414 4
            if (!empty($requirements)) {
415 4
                $this->requirements[$object] = array_values($requirements);
416
            } else {
417 4
                $this->removeRequirement($object);
418
            }
419
        }
420 4
    }
421
}
422