Completed
Branch guard_coverage (f2aaf0)
by Pablo
03:07
created

WriterInterfaceTrait::getWriter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
3
namespace PhpGitHooks\Module\Git\Tests\Infrastructure;
4
5
use PhpGitHooks\Module\Git\Model\WriterInterface;
6
use PhpGitHooks\Module\Tests\Infrastructure\UnitTestCase\Mock;
7
8
trait WriterInterfaceTrait
9
{
10
    /**
11
     * @var WriterInterface
12
     */
13
    private $writer;
14
15
    /**
16
     * @return \Mockery\MockInterface|WriterInterface
17
     */
18
    protected function getWriter()
19
    {
20
        return $this->writer = $this->writer ?: Mock::get(WriterInterface::class);
21
    }
22
23
    /**
24
     * @param string $data
25
     */
26
    protected function shouldWriteData($data)
27
    {
28
        $this->getWriter()
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in PhpGitHooks\Module\Git\Model\WriterInterface.

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...
29
            ->shouldReceive('write')
30
            ->once()
31
            ->with($data);
32
    }
33
}
34