Completed
Push — develop ( e8d178...1ddd67 )
by
unknown
07:12
created

EventManager   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 72
Duplicated Lines 11.11 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 3
dl 8
loc 72
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setEventPrototype() 0 7 1
A trigger() 0 17 4
D getEvent() 8 33 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2016 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Core\EventManager;
12
13
use Zend\EventManager\Event;
14
use Zend\EventManager\EventInterface;
15
use Zend\EventManager\EventManager as ZfEventManager;
16
use Zend\EventManager\Exception;
17
18
/**
19
 * EventPrototype Aware EventManager implementation.
20
 *
21
 * @internal
22
 *      Will be obsolete with ZF3 as ZF3s' EventManager implementation is
23
 *      already event prototype aware.
24
 * 
25
 * @author Mathias Gelhausen <[email protected]>
26
 * @since 0.25
27
 */
28
class EventManager extends ZfEventManager implements EventProviderInterface
29
{
30
31
    /**
32
     * The event prototype.
33
     *
34
     * @var EventInterface
35
     */
36
    protected $eventPrototype;
37
38
    public function setEventPrototype(EventInterface $event)
39
    {
40
        $this->eventPrototype = $event;
41
        $this->setEventClass(get_class($event));
0 ignored issues
show
Deprecated Code introduced by
The method Zend\EventManager\EventManager::setEventClass() has been deprecated with message: This method is deprecated with 2.6.0, and will be removed in 3.0.0. See {@link https://github.com/zendframework/zend-eventmanager/blob/develop/doc/book/migration/removed.md} for details.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
42
43
        return $this;
44
    }
45
46
    public function getEvent($name = null, $target = null, $params = null)
47
    {
48
        if (!$this->eventPrototype) {
49
            $this->setEventPrototype(new Event());
50
        }
51
52
        $event = clone $this->eventPrototype;
53
54
        if (is_array($name)) {
55
            $params = $name;
56
            $name = null;
57
58
        } else if (is_array($target)) {
59
            $params = $target;
60
            $target = null;
61
        }
62
63 View Code Duplication
        if (!$name && isset($params['name'])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $name of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Duplication introduced by
This code seems to be duplicated across 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...
64
            $name = $params['name'];
65
            unset($params['name']);
66
        }
67
68 View Code Duplication
        if (!$target && isset($params['target'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
69
            $target = $params['target'];
70
            unset($params['target']);
71
        }
72
73
        $event->setName($name);
74
        if (null !== $target) { $event->setTarget($target); }
75
        if (null !== $params) { $event->setParams($params); }
0 ignored issues
show
Documentation introduced by
$params is of type array|object<Traversable>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76
77
        return $event;
78
    }
79
80
    public function trigger($event, $target = null, $argv = [], $callback = null)
81
    {
82
        if (!$event instanceOf EventInterface
83
            && !$target instanceOf EventInterface
84
            && !$argv instanceOf EventInterface
85
        ) {
86
            /*
87
             * Create the event from the prototype, and not
88
             * from eventClass as the parent implementation does.
89
             */
90
            $e = $this->getEvent($event, $target, $argv);
0 ignored issues
show
Bug introduced by
It seems like $target defined by parameter $target on line 80 can also be of type string; however, Core\EventManager\EventManager::getEvent() does only seem to accept array|object|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
91
92
            return parent::trigger($e, $callback);
0 ignored issues
show
Bug introduced by
It seems like $callback defined by parameter $callback on line 80 can also be of type callable; however, Zend\EventManager\EventManager::trigger() does only seem to accept string|object|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
93
        }
94
95
        return parent::trigger($event, $target, $argv, $callback);
96
    }
97
98
99
}