GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ApiEvent::hasCallback()   B
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 5
nc 3
nop 2
crap 5
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 328
    public function __construct(array $objects)
48
    {
49 328
        $this->objects = $objects;
50 328
        $this->callbacks = new \SplObjectStorage();
51 328
        $this->requirements = new \SplObjectStorage();
52 328
    }
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 268
    public function getObjects($class = null)
72
    {
73 268
        if ($class === null) {
74 4
            return $this->objects;
75
        }
76
77 264
        $objects = [];
78
79 264
        foreach ($this->objects as $object) {
80 264
            if ($object instanceof $class) {
81 264
                $objects[] = $object;
82 132
            }
83 132
        }
84
85 264
        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 280
    public function getSources()
100
    {
101 280
        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 4
        }
121 8
    }
122
123
    /**
124
     * @param string $source
125
     *
126
     * @return bool
127
     */
128 52
    public function hasSource($source)
129
    {
130 52
        return in_array($source, $this->sources, true);
131
    }
132
133
    /**
134
     * @param string $source
135
     */
136 52
    public function addSource($source)
137
    {
138 52
        if (!$this->hasSource($source)) {
139 52
            $this->sources[] = $source;
140 26
        }
141 52
    }
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 280
    public function getLibraries()
164
    {
165 280
        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 268
    public function addLibraries(array $libraries)
181
    {
182 268
        foreach ($libraries as $library) {
183 60
            $this->addLibrary($library);
184 134
        }
185 268
    }
186
187
    /**
188
     * @param string $library
189
     *
190
     * @return bool
191
     */
192 84
    public function hasLibrary($library)
193
    {
194 84
        return in_array($library, $this->libraries, true);
195
    }
196
197
    /**
198
     * @param string $library
199
     */
200 84
    public function addLibrary($library)
201
    {
202 84
        if (!$this->hasLibrary($library)) {
203 84
            $this->libraries[] = $library;
204 42
        }
205 84
    }
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 264
    public function getCallbacks()
228
    {
229 264
        return $this->callbacks;
230
    }
231
232
    /**
233
     * @param string      $callback
234
     * @param object|null $object
235
     *
236
     * @return bool
237
     */
238 268
    public function hasCallback($callback, $object = null)
239
    {
240 268
        foreach ($this->callbacks as $rawObject) {
241 12
            if ($this->callbacks[$rawObject] === $callback && ($object === null || $object === $rawObject)) {
242 10
                return true;
243
            }
244 134
        }
245
246 268
        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]) && ($callback === null || $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 4
        }
282 4
    }
283
284
    /**
285
     * @param object $object
286
     * @param string $callback
287
     */
288 268
    public function addCallback($object, $callback)
289
    {
290 268
        if (!$this->hasCallback($callback, $object)) {
291 268
            $this->callbacks[$object] = $callback;
292 134
        }
293 268
    }
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 2
        }
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 ($object === null) {
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 264
    public function getRequirements()
333
    {
334 264
        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 4
        }
366 8
    }
367
368
    /**
369
     * @param object      $object
370
     * @param string|null $requirement
371
     *
372
     * @return bool
373
     */
374 276
    public function hasRequirement($object, $requirement = null)
375
    {
376 276
        return isset($this->requirements[$object])
377 276
            && ($requirement === null || in_array($requirement, $this->requirements[$object], true));
378
    }
379
380
    /**
381
     * @param object $object
382
     * @param string $requirement
383
     */
384 276
    public function addRequirement($object, $requirement)
385
    {
386 276
        if (!$this->hasRequirement($object)) {
387 276
            $this->requirements[$object] = [];
388 138
        }
389
390 276
        if (!$this->hasRequirement($object, $requirement)) {
391 276
            $this->requirements[$object] = array_merge(
392 276
                $this->requirements[$object],
393 276
                [$requirement]
394 138
            );
395 138
        }
396 276
    }
397
398
    /**
399
     * @param object      $object
400
     * @param string|null $requirement
401
     */
402 4
    public function removeRequirement($object, $requirement = null)
403
    {
404 4
        if ($requirement === null) {
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 2
            } else {
417 4
                $this->removeRequirement($object);
418
            }
419 2
        }
420 4
    }
421
}
422