ObjectStorage::each()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
nc 3
nop 2
1
<?php
2
namespace PHPDaemon\Structures;
3
4
/**
5
 * ObjectStorage
6
 * @package PHPDaemon\Structures
7
 * @author  Vasily Zorin <[email protected]>
8
 */
9
class ObjectStorage extends \SplObjectStorage
10
{
11
    use \PHPDaemon\Traits\ClassWatchdog;
12
    use \PHPDaemon\Traits\StaticObjectWatchdog;
13
14
    /**
15
     * Call given method of all objects in storage
16
     * @param  string $method Method name
17
     * @param  mixed ...$args Arguments
18
     * @return integer Number of called objects
19
     */
20
    public function each($method, ...$args)
21
    {
22
        if ($this->count() === 0) {
23
            return 0;
24
        }
25
        $n = 0;
26
        foreach ($this as $obj) {
27
            $obj->$method(...$args);
28
            ++$n;
29
        }
30
        return $n;
31
    }
32
33
    /**
34
     * Remove all objects from this storage, which contained in another storage
35
     * @param  \SplObjectStorage $obj
0 ignored issues
show
Documentation introduced by
Should the type for parameter $obj not be \SplObjectStorage|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
36
     * @return void
37
     */
38
    public function removeAll($obj = null)
39
    {
40
        if ($obj === null) {
41
            $this->removeAllExcept(new \SplObjectStorage);
42
        }
43
        parent::removeAll($obj);
44
    }
45
46
    /**
47
     * Detaches first object and returns it
48
     * @return object
0 ignored issues
show
Documentation introduced by
Should the return type not be false|object?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
49
     */
50
    public function detachFirst()
51
    {
52
        $this->rewind();
53
        $o = $this->current();
54
        if (!$o) {
55
            return false;
56
        }
57
        $this->detach($o);
58
        return $o;
59
    }
60
61
    /**
62
     * Returns first object
63
     * @return object
64
     */
65
    public function getFirst()
66
    {
67
        $this->rewind();
68
        return $this->current();
69
    }
70
}
71