Passed
Pull Request — master (#3)
by Valentin
03:18
created

DelegateTest::testSnapshotRenderByPass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace Spiral\Tests\Snapshotter;
4
5
use Mockery\MockInterface;
6
use Spiral\Debug\LogManager;
7
use Spiral\Snapshotter\DelegateSnapshot;
8
use Spiral\Snapshotter\HandlerInterface;
9
use Spiral\Tests\BaseTest;
10
use Symfony\Component\Debug\Debug;
11
12
class DelegateTest extends BaseTest
13
{
14
    /**
15
     * Test delegation contains snapshot render data.
16
     */
17
    public function testSnapshotRenderByPass()
18
    {
19
        /** @var HandlerInterface $handler */
20
        $handler = \Mockery::mock(HandlerInterface::class);
21
22
        $delegate = new DelegateSnapshot(
23
            new \Error('custom error'),
0 ignored issues
show
Documentation introduced by
new \Error('custom error') is of type object<Error>, but the function expects a object<Throwable>.

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...
24
            $this->logs->getLogger(LogManager::DEBUG_CHANNEL),
25
            $this->container,
26
            $handler
27
        );
28
29
        $this->assertContains('custom error', $delegate->render());
30
    }
31
32
    /**
33
     * Test snapshot error was logged.
34
     */
35
    public function testSnapshotReport()
36
    {
37
        /** @var HandlerInterface|MockInterface $handler */
38
        $handler = \Mockery::mock(HandlerInterface::class);
39
        $handler->shouldReceive('registerSnapshot');
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Spiral\Snapshotter\HandlerInterface.

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...
40
41
        $delegate = new DelegateSnapshot(
42
            new \Error('custom error'),
0 ignored issues
show
Documentation introduced by
new \Error('custom error') is of type object<Error>, but the function expects a object<Throwable>.

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...
43
            $this->logs->getLogger(LogManager::DEBUG_CHANNEL),
44
            $this->container,
45
            $handler
0 ignored issues
show
Bug introduced by
It seems like $handler defined by \Mockery::mock(\Spiral\S...andlerInterface::class) on line 38 can also be of type object<Mockery\MockInterface>; however, Spiral\Snapshotter\DelegateSnapshot::__construct() does only seem to accept object<Spiral\Snapshotter\HandlerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
46
        );
47
48
        $this->assertEmpty($this->files->getFiles(directory('runtime') . 'logs/'));
49
        $delegate->report();
50
        $this->assertNotEmpty($this->files->getFiles(directory('runtime') . 'logs/'));
51
    }
52
}