EventClassDispatcher   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 42.67 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 5
Bugs 5 Features 0
Metric Value
wmc 6
c 5
b 5
f 0
lcom 1
cbo 4
dl 32
loc 75
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A notify() 17 18 3
A buildAndNotify() 15 16 2
A hasListeners() 0 7 1

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
namespace PEIP\Dispatcher;
4
5
/*
6
 * To change this template, choose Tools | Templates
7
 * and open the template in the editor.
8
 */
9
10
/*
11
 * Description of EventClassDispatcher
12
 *
13
 * @author timo
14
 */
15
use PEIP\Event\EventBuilder;
16
use PEIP\Util\ReflectionPool;
17
18
class EventClassDispatcher extends \PEIP\Dispatcher\ObjectMapDispatcher
19
{
20
    /**
21
     * Notifies all listeners of a given event-object.
22
     *
23
     * @param string                $name   name of the event
24
     * @param \PEIP\INF\Event\Event $object an event object
25
     *
26
     * @return bool|null
27
     */
28 View Code Duplication
    public function notify($name, $object)
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...
29
    {
30
        if ($object instanceof \PEIP\INF\Event\Event) {
31
            if (is_object($object->getContent())) {
32
                return self::doNotify(
33
                    $this->getListeners(
34
                        $name,
35
                        $object
36
                        ),
37
                        $object->getContent()
38
                    );
39
            } else {
40
                throw new \InvalidArgumentException('instance of \PEIP\INF\Event\Event must contain subject');
41
            }
42
        } else {
43
            throw new \InvalidArgumentException('object must be instance of \PEIP\INF\Event\Event');
44
        }
45
    }
46
47
       //put your code here
48
49
    /**
50
     * Creates an event-object with given object as content/subject and notifies
51
     * all registers listeners of the event.
52
     *
53
     * @param string $name       name of the event
54
     * @param object $object     the subject of the event
55
     * @param array  $headers    headers of the event-object as key/value pairs
56
     * @param string $eventClass event-class to create instances from
57
     *
58
     * @return
59
     */
60 View Code Duplication
    public function buildAndNotify($name, $object, array $headers = [], $eventClass = false, $type = false)
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...
61
    {
62
        if (!$this->hasListeners($name, ($object))) {
63
            return false;
64
        }
65
66
        return $this->notify(
67
                $name,
68
                EventBuilder::getInstance($eventClass)->build(
0 ignored issues
show
Bug introduced by
It seems like $eventClass defined by parameter $eventClass on line 60 can also be of type false; however, PEIP\Event\EventBuilder::getInstance() does only seem to accept string, 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...
69
                    $object,
70
                    $name,
71
                    $headers,
72
                    (string) $type
0 ignored issues
show
Unused Code introduced by
The call to EventBuilder::build() has too many arguments starting with (string) $type.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
73
                )
74
        );
75
    }
76
77
    /**
78
     * Checks wether an object has a listener for an event.
79
     *
80
     * @param string $name   the event-name
81
     * @param object $object object to check for listeners
82
     *
83
     * @return bool
84
     */
85
    public function hasListeners($name, $object)
86
    {
87
        return parent::hasListeners(
88
            $name,
89
            ReflectionPool::getInstance($object)
90
        );
91
    }
92
}
93