DelegateTest::testSnapshotReport()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
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
Unused Code introduced by
The call to Error::__construct() has too many arguments starting with 'custom error'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

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
Unused Code introduced by
The call to Error::__construct() has too many arguments starting with 'custom error'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

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
}