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.
Completed
Push — master ( b02eb7...18e80c )
by Hong
03:04
created

CountableTrait::many()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 2
eloc 13
nc 1
nop 4
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Event
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Event\Traits;
16
17
use Phossa2\Event\Interfaces\EventInterface;
18
use Phossa2\Event\Interfaces\CountableInterface;
19
20
/**
21
 * CountableTrait
22
 *
23
 * Implmentation of CountableInterface
24
 *
25
 * @package Phossa2\Event
26
 * @author  Hong Zhang <[email protected]>
27
 * @see     CountableInterface
28
 * @version 2.0.0
29
 * @since   2.0.0 added
30
 */
31
trait CountableTrait
32
{
33
    /**
34
     * callable mapping
35
     *
36
     * @var    callable[];
37
     * @access protected
38
     */
39
    protected $callable_map = [];
40
41
    /**
42
     * {@inheritDoc}
43
     */
44
    public function many(
45
        /*# int */ $times,
46
        /*# string */ $eventName,
47
        callable $callable,
48
        /*# int */ $priority = 50
49
    ) {
50
        // wrap the callable
51
        $wrapper = function (EventInterface $event) use ($callable, $times) {
52
            static $cnt = 0;
53
            if ($cnt++ < $times) {
54
                call_user_func($callable, $event);
55
            }
56
        };
57
58
        // mapping callable
59
        $oid = $this->hashCallable($callable);
60
        $this->callable_map[$eventName][$oid] = $wrapper;
61
62
        // bind wrapper instead of the $callable
63
        $this->on($eventName, $wrapper, $priority);
0 ignored issues
show
Bug introduced by
It seems like on() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
64
65
        return $this;
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     */
71
    public function one(
72
        /*# string */ $eventName,
73
        callable $callable,
74
        /*# int */ $priority = 50
75
    ) {
76
        return $this->many(1, $eventName, $callable, $priority);
77
    }
78
79
    /**
80
     * Override `off()` in EventManager
81
     *
82
     * Added support for countable callable
83
     *
84
     * {@inheritDoc}
85
     */
86
    public function off(
87
        /*# string */ $eventName = '',
88
        callable $callable = null
89
    ) {
90
        if (null !== $callable) {
91
            $oid = $this->hashCallable($callable);
92
            if (isset($this->callable_map[$eventName][$oid])) {
93
                $callable = $this->callable_map[$eventName][$oid];
94
                unset($this->callable_map[$eventName][$oid]);
95
            }
96
        } else {
97
            unset($this->callable_map[$eventName]);
98
        }
99
        return parent::off($eventName, $callable);
100
    }
101
102
    /**
103
     * Returns a unique id for $callable with $eventName
104
     *
105
     * @param  callable $callable
106
     * @return string
107
     * @access protected
108
     */
109
    protected function hashCallable(callable $callable)/*# string */
110
    {
111
        return spl_object_hash((object) $callable);
112
    }
113
}
114