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 ( 48d25d...efdc1d )
by Hong
02:54
created

SharedManagerTrait   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 17
c 3
b 0
f 0
lcom 2
cbo 1
dl 0
loc 140
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A onEvent() 0 12 2
A offEvent() 0 11 2
A onGlobalEvent() 0 8 1
A offGlobalEvent() 0 7 1
A isAType() 0 4 2
A getOwnScopes() 0 19 2
A getAllTypes() 0 10 3
A matchParentType() 0 13 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\Shared\Shareable\ShareableTrait;
18
use Phossa2\Event\Interfaces\EventManagerInterface;
19
20
/**
21
 * SharedManagerTrait
22
 *
23
 * Support for shared managers using ShareableTrait
24
 *
25
 * ```php
26
 * // one event manager instance
27
 * $event_dispatcher = new EventDispatcher();
28
 *
29
 * // global event manager, default scope is ''
30
 * $globalManager = EventDispatcher::getShareable();
31
 *
32
 * // shared manager for a scope, say 'MVC'
33
 * $MvcManager = EventDispatcher::getShareable('MVC');
34
 *
35
 * // class/interface level shared manager
36
 * $classManager = EventDispatcher::getShareable('Phossa\\Config\\Config');
37
 * ```
38
 *
39
 * @package Phossa2\Event
40
 * @author  Hong Zhang <[email protected]>
41
 * @see     SharedManagerInterface
42
 * @version 2.0.0
43
 * @since   2.0.0 added
44
 */
45
trait SharedManagerTrait
46
{
47
    use ShareableTrait;
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    public static function onEvent(
53
        $scope,
54
        /*# string */ $eventName,
55
        callable $callable,
56
        /*# int */ $priority = 50
57
    ) {
58
        foreach ((array) $scope as $sc) {
59
            /* @var $em EventManagerInterface */
60
            $em = static::getShareable($sc);
61
            $em->on($eventName, $callable, $priority);
62
        }
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68
    public static function offEvent(
69
        $scope,
70
        /*# string */ $eventName,
71
        callable $callable = null
72
    ) {
73
        foreach ((array) $scope as $sc) {
74
            /* @var $em EventManagerInterface */
75
            $em = static::getShareable($sc);
76
            $em->off($eventName, $callable);
77
        }
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83
    public static function onGlobalEvent(
84
        /*# string */ $eventName,
85
        callable $callable,
86
        /*# int */ $priority = 50
87
    ) {
88
        // scope '' means GLOBAL
89
        static::onEvent('', $eventName, $callable, $priority);
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95
    public static function offGlobalEvent(
96
        /*# string */ $eventName,
97
        callable $callable = null
98
    ) {
99
        // scope '' means GLOBAL
100
        static::offEvent('', $eventName, $callable);
101
    }
102
103
    /**
104
     * Override `getOwnScopes()` in ShareableTrait
105
     *
106
     * For $this's own scopes, looking for any parent class or interface in
107
     * all the available scopes.
108
     *
109
     * {@inheritDoc}
110
     */
111
    protected function getOwnScopes()/*# : array */
112
    {
113
        // result
114
        $result = [];
115
116
        // all types avaible
117
        $allTypes = $this->getAllTypes(static::getScopes());
118
119
        // loop thru own scopes
120
        foreach ($this->scopes as $scope) {
121
            $result[$scope] = true;
122
            $this->matchParentType($scope, $allTypes, $result);
123
        }
124
125
        // alway add global scope
126
        $result[''] = true;
127
128
        return array_keys($result);
129
    }
130
131
    /**
132
     * Is $type a classname or interface name ?
133
     *
134
     * @param  string $type
135
     * @return bool
136
     * @access protected
137
     */
138
    protected function isAType(/*# string */ $type)/*# : bool */
139
    {
140
        return class_exists($type) || interface_exists($type);
141
    }
142
143
    /**
144
     * Get all types (class or interface) from the given scopes
145
     *
146
     * @param  array $scopes
147
     * @return array
148
     * @access protected
149
     */
150
    protected function getAllTypes(array $scopes)/*# : array */
151
    {
152
        $result = [];
153
        foreach ($scopes as $scope) {
154
            if ($this->isAType($scope)) {
155
                $result[] = $scope;
156
            }
157
        }
158
        return $result;
159
    }
160
161
    /**
162
     * is $childType child type of one of the $typesToCheck.
163
     *
164
     * Returns the matched types
165
     *
166
     * @param  string $childType
167
     * @param  array $typesToCheck
168
     * @param  array &$result
169
     * @access protected
170
     */
171
    protected function matchParentType(
172
        /*# string */ $childType,
173
        array $typesToCheck,
174
        array &$result
175
    )/*# : bool */ {
176
        foreach ($typesToCheck as $type) {
177
            if ($this->isAType($childType) &&
178
                is_a($childType, $type, true)
179
            ) {
180
                $result[$type] = true;
181
            }
182
        }
183
    }
184
}
185