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 ( 0c3dc8...b46d2a )
by Hong
03:03
created

SharedManagerTrait   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 2
cbo 1
dl 0
loc 123
rs 10

7 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 getOwnScopes() 0 19 2
A isAType() 0 4 2
B isSubType() 0 14 5
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 scopes avaible
117
        $allScopes = static::getScopes();
118
119
        // loop thru own scopes
120
        foreach ($this->scopes as $scope) {
121
            $result[$scope] = true;
122
            $this->isSubType($scope, $allScopes, $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
     * is $childType child type of one of the $typesToCheck.
145
     *
146
     * Returns the matched types
147
     *
148
     * @param  string $childType
149
     * @param  array $typesToCheck
150
     * @param  array &$result
151
     * @access protected
152
     */
153
    protected function isSubType(
154
        /*# string */ $childType,
155
        array $typesToCheck,
156
        array &$result
157
    )/*# : bool */ {
158
        foreach ($typesToCheck as $type) {
159
            if ($this->isAType($childType) &&
160
                $this->isAType($type) &&
161
                is_a($childType, $type, true)
162
            ) {
163
                $result[$type] = true;
164
            }
165
        }
166
    }
167
}
168