BusTests   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 80
Duplicated Lines 50 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 40
loc 80
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testCreateWithInvalidMiddleware() 0 11 1
A testDispatch() 0 11 1
A testAddMiddlewareBefore() 20 20 1
A testAddMiddlewareAfter() 20 20 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
 * This file is part of the Cubiche package.
4
 *
5
 * Copyright (c) Cubiche
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Cubiche\Core\Bus\Tests\Units;
11
12
use Cubiche\Core\Bus\Bus;
13
use Cubiche\Core\Bus\Exception\InvalidMiddlewareException;
14
use Cubiche\Core\Bus\Middlewares\Locking\LockingMiddleware;
15
use Cubiche\Core\Bus\Tests\Fixtures\FooMessage;
16
17
/**
18
 * BusTests class.
19
 *
20
 * @author Ivannis Suárez Jerez <[email protected]>
21
 */
22
class BusTests extends TestCase
23
{
24
    /**
25
     * Test create with invalid middleware.
26
     */
27
    public function testCreateWithInvalidMiddleware()
28
    {
29
        $this
30
            ->given($middleware = new LockingMiddleware())
31
            ->then()
32
                ->exception(function () use ($middleware) {
33
                    new Bus([$middleware, new \StdClass()]);
0 ignored issues
show
Documentation introduced by
array($middleware, new \StdClass()) is of type array<integer,object<Cub...1":"object<stdClass>"}>, but the function expects a array<integer,object<Cub...s\MiddlewareInterface>>.

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...
34
                })
35
                ->isInstanceOf(InvalidMiddlewareException::class)
36
        ;
37
    }
38
39
    /**
40
     * Test dispatch method.
41
     */
42
    public function testDispatch()
43
    {
44
        $this
45
            ->given($middleware = new LockingMiddleware())
46
            ->and($bus = new Bus([12 => $middleware]))
47
            ->when($result = $bus->dispatch(new FooMessage()))
48
            ->then()
49
                ->variable($result)
50
                    ->isNull()
51
        ;
52
    }
53
54
    /**
55
     * Test addMiddlewareBefore method.
56
     */
57 View Code Duplication
    public function testAddMiddlewareBefore()
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...
58
    {
59
        $this
60
            ->given($middleware = new LockingMiddleware())
61
            ->and($bus = new Bus([12 => $middleware]))
62
            ->when($result = $bus->addMiddlewareBefore($middleware, $middleware))
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $bus->addMiddlewareBefor...iddleware, $middleware) (which targets Cubiche\Core\Bus\Bus::addMiddlewareBefore()) 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...
63
            ->then()
64
                ->variable($result)
65
                    ->isNull()
66
        ;
67
68
        $this
69
            ->given($middleware = new LockingMiddleware())
70
            ->and($bus = new Bus())
71
            ->then()
72
                ->exception(function () use ($bus, $middleware) {
73
                    $bus->addMiddlewareBefore($middleware, $middleware);
74
                })->isInstanceOf(\InvalidArgumentException::class)
75
        ;
76
    }
77
78
    /**
79
     * Test addMiddlewareAfter method.
80
     */
81 View Code Duplication
    public function testAddMiddlewareAfter()
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...
82
    {
83
        $this
84
            ->given($middleware = new LockingMiddleware())
85
            ->and($bus = new Bus([12 => $middleware]))
86
            ->when($result = $bus->addMiddlewareAfter($middleware, $middleware))
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $bus->addMiddlewareAfter...iddleware, $middleware) (which targets Cubiche\Core\Bus\Bus::addMiddlewareAfter()) 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...
87
            ->then()
88
                ->variable($result)
89
                    ->isNull()
90
        ;
91
92
        $this
93
            ->given($middleware = new LockingMiddleware())
94
            ->and($bus = new Bus())
95
            ->then()
96
                ->exception(function () use ($bus, $middleware) {
97
                    $bus->addMiddlewareAfter($middleware, $middleware);
98
                })->isInstanceOf(\InvalidArgumentException::class)
99
        ;
100
    }
101
}
102