QueryBusTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 28
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getQueryBus() 0 4 2
A shouldHandleQuery() 0 8 1
1
<?php
2
3
namespace PhpGitHooks\Module\Shared\Tests\Infrastructure;
4
5
use Bruli\EventBusBundle\QueryBus\QueryBus;
6
use Bruli\EventBusBundle\QueryBus\QueryInterface;
7
use PhpGitHooks\Module\Tests\Infrastructure\UnitTestCase\Mock;
8
use PhpGitHooks\Module\Tests\Infrastructure\UnitTestCase\SimilarTo;
9
10
trait QueryBusTrait
11
{
12
    /**
13
     * @var QueryBus
14
     */
15
    private $queryBus;
16
17
    /**
18
     * @return \Mockery\MockInterface|QueryBus
19
     */
20
    protected function getQueryBus()
21
    {
22
        return $this->queryBus = $this->queryBus ?: Mock::get(QueryBus::class);
23
    }
24
25
    /**
26
     * @param QueryInterface $query
27
     * @param mixed          $return
28
     */
29
    protected function shouldHandleQuery(QueryInterface $query, $return)
30
    {
31
        $this->getQueryBus()
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Bruli\EventBusBundle\QueryBus\QueryBus.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
32
            ->shouldReceive('handle')
33
            ->once()
34
            ->with((new SimilarTo())->compare($query))
35
            ->andReturn($return);
36
    }
37
}
38