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 ( b857c5...8bcec0 )
by Hong
02:52
created

EventCapableTrait::attachSelfToEventManager()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.2
cc 4
eloc 6
nc 3
nop 0
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\Event;
18
use Phossa2\Event\EventDispatcher;
19
use Phossa2\Event\Interfaces\ListenerInterface;
20
use Phossa2\Event\Interfaces\EventManagerInterface;
21
use Phossa2\Event\Interfaces\ListenerAwareInterface;
22
23
/**
24
 * Implementation of EventCapableInterface
25
 *
26
 * @package Phossa2\Event
27
 * @author  Hong Zhang <[email protected]>
28
 * @see     EventCapableInterface
29
 * @version 2.1.5
30
 * @since   2.0.0 added
31
 * @since   2.1.1 updated
32
 * @since   2.1.5 added attachSelfToEventManager()
33
 */
34
trait EventCapableTrait
35
{
36
    use EventPrototypeTrait;
37
38
    /**
39
     * event manager or dispatcher
40
     *
41
     * @var    EventManagerInterface
42
     * @access protected
43
     */
44
    protected $event_manager;
45
46
    /**
47
     * flag for attachListener
48
     *
49
     * @var    bool
50
     * @access protected
51
     * @since  2.1.5
52
     */
53
    protected $listener_attached = false;
54
55
    /**
56
     * {@inheritDoc}
57
     *
58
     * @since  2.1.5 moved attachListener to getEventManager
59
     */
60
    public function setEventManager(
61
        EventManagerInterface $eventManager
62
    ) {
63
        $this->event_manager = $eventManager;
64
        return $this;
65
    }
66
67
    /**
68
     * {@inheritDoc}
69
     *
70
     * @since  2.1.5 added attachSelfToEventManager()
71
     */
72
    public function getEventManager()/*# : EventManagerInterface */
73
    {
74
        // create the default slave
75
        if (is_null($this->event_manager)) {
76
            // add own classname as scope
77
            $this->setEventManager(new EventDispatcher(get_class($this)));
78
        }
79
80
        // attach self to the event manager if not yet
81
        $this->attachSelfToEventManager();
82
83
        return $this->event_manager;
84
    }
85
86
    /**
87
     * {@inheritDoc}
88
     */
89
    public function trigger(
90
        /*# string */ $eventName,
91
        array $parameters = []
92
    )/*# : bool */ {
93
        $evt = $this->newEvent($eventName, $this, $parameters);
94
        return $this->getEventManager()->trigger($evt);
95
    }
96
97
    /**
98
     * {@inheritDoc}
99
     */
100
    public function triggerEvent(
101
        /*# string */ $eventName,
102
        array $parameters = []
103
    )/*# : EventInterface */ {
104
        $evt = $this->newEvent($eventName, $this, $parameters);
105
        $this->getEventManager()->trigger($evt);
106
        return $evt;
107
    }
108
109
    /**
110
     * Attach $this to the event manager if not yet
111
     *
112
     * @access protected
113
     * @since  2.1.5
114
     */
115
    protected function attachSelfToEventManager()
116
    {
117
        if (!$this->listener_attached) {
118
            if ($this->event_manager instanceof ListenerAwareInterface &&
119
                $this instanceof ListenerInterface
120
            ) {
121
                $this->event_manager->attachListener($this);
1 ignored issue
show
Bug introduced by
Accessing event_manager on the interface Phossa2\Event\Interfaces\ListenerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
122
            }
123
            $this->listener_attached = true;
124
        }
125
    }
126
}
127