Dispatcher::notifyUntil()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace PEIP\Dispatcher;
4
5
namespace PEIP\Dispatcher;
6
7
/*
8
 * This file is part of the PEIP package.
9
 * (c) 2009-2016 Timo Michna <timomichna/yahoo.de>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
/*
16
 * Dispatcher
17
 * Basic dispatcher implementation
18
 *
19
 * @author Timo Michna <timomichna/yahoo.de>
20
 * @package PEIP
21
 * @subpackage dispatcher
22
 * @extends \PEIP\ABS\Dispatcher\Dispatcher
23
 * @implements \PEIP\INF\Dispatcher\Dispatcher
24
 */
25
26
use PEIP\Util\Test;
27
28
class Dispatcher extends \PEIP\ABS\Dispatcher\Dispatcher implements
29
        \PEIP\INF\Dispatcher\Dispatcher,
30
        \PEIP\INF\Base\Plugable
31
{
32
    protected $listeners = [];
33
34
    /**
35
     * Connects a listener.
36
     *
37
     * @param callable|PEIP\INF\Handler\Handler $listener
38
     *
39
     * @return void
40
     */
41
    public function connect($listener)
42
    {
43
        Test::ensureHandler($listener);
44
        $this->listeners[] = $listener;
45
    }
46
47
    /**
48
     * Disconnects a listener.
49
     *
50
     * @param callable|PEIP\INF\Handler\Handler $listener
51
     *
52
     * @return void
53
     */
54
    public function disconnect($listener)
55
    {
56
        Test::ensureHandler($listener);
57
        foreach ($this->listeners as $i => $callable) {
58
            if ($listener === $callable) {
59
                unset($this->listeners[$i]);
60
            }
61
        }
62
    }
63
64
    /**
65
     * Disconnects all listeners.
66
     *
67
     * @param callable|PEIP\INF\Handler\Handler $listener
0 ignored issues
show
Bug introduced by
There is no parameter named $listener. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
68
     *
69
     * @return void
70
     */
71
    public function disconnectAll()
72
    {
73
        $this->listeners = [];
74
    }
75
76
    /**
77
     * returns wether any listeners are registered.
78
     *
79
     * @return bool wether any listeners are registered
80
     */
81
    public function hasListeners()
82
    {
83
        return (bool) count($this->listeners);
84
    }
85
86
    /**
87
     * notifies all listeners on a subject.
88
     *
89
     * @param mixed $subject the subject
90
     *
91
     * @return void
92
     */
93 View Code Duplication
    public function notify($subject)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
    {
95
        $res = null;
96
        if ($this->hasListeners()) {
97
            $res = self::doNotify($this->getListeners(), $subject);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $res is correct as self::doNotify($this->getListeners(), $subject) (which targets PEIP\ABS\Dispatcher\Dispatcher::doNotify()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
98
        }
99
100
        return $res;
101
    }
102
103
    /**
104
     * notifies all listeners on a subject until one returns a boolean true value.
105
     *
106
     * @param mixed $subject the subject
107
     *
108
     * @return \PEIP\INF\Handler\Handler the listener which returned a boolean true value
109
     */
110
    public function notifyUntil($subject)
111
    {
112
        if ($this->hasListeners()) {
113
            $res = self::doNotifyUntil($this->getListeners(), $subject);
114
        }
115
116
        return $res;
0 ignored issues
show
Bug introduced by
The variable $res does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
117
    }
118
119
    /**
120
     * returns all listeners.
121
     *
122
     * @return array the listeners
123
     */
124
    public function getListeners()
125
    {
126
        return $this->listeners;
127
    }
128
}
129