EventHandlerPass::process()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 51
Code Lines 32

Duplication

Lines 16
Ratio 31.37 %

Code Coverage

Tests 36
CRAP Score 6.036

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 16
loc 51
ccs 36
cts 40
cp 0.9
rs 8.6588
c 3
b 0
f 0
cc 6
eloc 32
nc 5
nop 1
crap 6.036

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * The software is based on the Axon Framework project which is
17
 * licensed under the Apache 2.0 license. For more information on the Axon Framework
18
 * see <http://www.axonframework.org/>.
19
 *
20
 * This software consists of voluntary contributions made by many individuals
21
 * and is licensed under the MIT license. For more information, see
22
 * <http://www.governor-framework.org/>.
23
 */
24
25
namespace Governor\Bundle\GovernorBundle\Command;
26
27
namespace Governor\Bundle\GovernorBundle\DependencyInjection\Compiler;
28
29
use Symfony\Component\DependencyInjection\Reference;
0 ignored issues
show
Coding Style introduced by
USE declarations must go after the first namespace declaration
Loading history...
30
use Symfony\Component\DependencyInjection\ContainerBuilder;
0 ignored issues
show
Coding Style introduced by
USE declarations must go after the first namespace declaration
Loading history...
31
use Governor\Framework\Annotations\EventHandler;
0 ignored issues
show
Coding Style introduced by
USE declarations must go after the first namespace declaration
Loading history...
32
use Governor\Framework\EventHandling\EventListenerInterface;
0 ignored issues
show
Coding Style introduced by
USE declarations must go after the first namespace declaration
Loading history...
33
use Governor\Framework\Common\Annotation\MethodMessageHandlerInspector;
0 ignored issues
show
Coding Style introduced by
USE declarations must go after the first namespace declaration
Loading history...
34
use Governor\Framework\EventHandling\Listeners\AnnotatedEventListener;
0 ignored issues
show
Coding Style introduced by
USE declarations must go after the first namespace declaration
Loading history...
35
36
/**
37
 * Compiler pass that subscribes all services tagged with <code>governor.event_handler</code>
38
 * to the event bus.
39
 *
40
 * @author    "David Kalosi" <[email protected]>
41
 * @license   <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a>
42
 */
43
class EventHandlerPass extends AbstractHandlerPass
44
{
45
46
    /**
47
     * @param ContainerBuilder $container
48
     */
49 11
    public function process(ContainerBuilder $container)
50
    {
51 11
        foreach ($container->findTaggedServiceIds('governor.event_handler') as $id => $attributes) {
52 11
            $busDefinition = $container->findDefinition(
0 ignored issues
show
Unused Code introduced by
$busDefinition is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
53 11
                sprintf(
54 11
                    "governor.event_bus.%s",
55 11
                    isset($attributes['event_bus']) ? $attributes['event_bus']
56
                        : 'default'
57 11
                )
58 11
            );
59
60 11
            $registry = $container->findDefinition(
61 11
                sprintf(
62 11
                    "governor.event_bus.registry.%s",
63 11
                    isset($attributes['event_bus']) ? $attributes['event_bus']
64
                        : 'default'
65 11
                )
66 11
            );
67
68 11
            $definition = $container->findDefinition($id);
69 11
            $reflectionClass = new \ReflectionClass($definition->getClass());
70
71
            // subscribe event listener instances directly
72 11
            if ($reflectionClass->implementsInterface(EventListenerInterface::class)) {
73
                $registry->addMethodCall('subscribe', [new Reference($id)]);
74
            }
75
76 11
            $inspector = new MethodMessageHandlerInspector(
77 11
                $container->get('governor.annotation_reader_factory'),
0 ignored issues
show
Documentation introduced by
$container->get('governo...tation_reader_factory') is of type object|null, but the function expects a object<Governor\Framewor...ReaderFactoryInterface>.

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...
78 11
                $reflectionClass,
79
                EventHandler::class
80 11
            );
81
82 11 View Code Duplication
            foreach ($inspector->getHandlerDefinitions() as $handlerDefinition) {
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...
83 11
                $handlerId = $this->getHandlerIdentifier('governor.event_handler');
84
85
                $container
86 11
                    ->register($handlerId, AnnotatedEventListener::class)
87 11
                    ->addArgument($handlerDefinition->getPayloadType())
88 11
                    ->addArgument($handlerDefinition->getMethod()->name)
89 11
                    ->addArgument(new Reference($id))
90 11
                    ->setPublic(true)
91 11
                    ->setLazy(true);
92
93 11
                $registry->addMethodCall(
94 11
                    'subscribe',
95 11
                    [new Reference($handlerId)]
96 11
                );
97 11
            }
98 11
        }
99 11
    }
100
101
}
102