GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — 3.x ( 52ff30...48a2e9 )
by Jindřich
12s queued 11s
created

CacheDecoratorTest::testDecoratorRequireRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 28
rs 9.472
c 0
b 0
f 0
1
<?php
2
3
namespace Skaut\Skautis\Test\Unit;
4
5
use Mockery\MockInterface;
6
use PHPUnit\Framework\TestCase;
7
use Skaut\Skautis\User;
8
use Skaut\Skautis\Wsdl\Decorator\Cache\CacheDecorator;
9
use Skaut\Skautis\Wsdl\WebServiceInterface;
10
use Symfony\Component\Cache\Adapter\ArrayAdapter;
11
use Symfony\Component\Cache\Psr16Cache;
12
13
class CacheDecoratorTest extends TestCase
14
{
15
16
    protected function tearDown(): void
17
    {
18
        \Mockery::close();
19
    }
20
21
    public function testDecoratorRequireRequest()
22
    {
23
        $value = ['id' => 'response'];
24
        $args = ['asd', 'uv', User::ID_LOGIN => 'a'];
25
26
        /** @var WebServiceInterface|MockInterface $webService */
27
        $webService = \Mockery::mock(WebServiceInterface::class);
28
        $webService->shouldReceive('call')->with('funkceA', $args)->once()->andReturn($value);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Skaut\Skautis\Wsdl\WebServiceInterface.

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
30
        $cache = new Psr16Cache(new ArrayAdapter());
31
32
        //Prazdna cache, musi poslat request
33
        $decoratedServiceA = new CacheDecorator($webService, $cache, 30);
0 ignored issues
show
Bug introduced by
It seems like $webService defined by \Mockery::mock(\Skaut\Sk...erviceInterface::class) on line 27 can also be of type object<Mockery\MockInterface>; however, Skaut\Skautis\Wsdl\Decor...ecorator::__construct() does only seem to accept object<Skaut\Skautis\Wsdl\WebServiceInterface>, 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...
34
        $response = $decoratedServiceA->call('funkceA', $args);
35
        $this->assertEquals($value, $response);
36
37
38
        //Jina instance WS
39
        /** @var WebServiceInterface|MockInterface $decoratedServiceB */
40
        $webServiceB = \Mockery::mock(WebServiceInterface::class);
41
42
        //Cache naplnena z prechoziho requestu
43
        $decoratedServiceB = new CacheDecorator($webServiceB, $cache, 30);
0 ignored issues
show
Documentation introduced by
$webServiceB is of type object<Mockery\LegacyMockInterface>, but the function expects a object<Skaut\Skautis\Wsdl\WebServiceInterface>.

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...
44
        $response = $decoratedServiceB->call('funkceA', $args);
45
46
        //Vraci data z cache
47
        $this->assertEquals($value, $response);
48
    }
49
50
    public function testDecorator()
51
    {
52
        $value = ['id' => 'response'];
53
        $args = ['asd', 'uv'];
54
55
        /** @var WebServiceInterface|MockInterface $webService */
56
        $webService = \Mockery::mock(WebServiceInterface::class);
57
        $webService->shouldReceive('call')->with('funkceA', $args)->once()->andReturn($value);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Skaut\Skautis\Wsdl\WebServiceInterface.

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...
58
        $webService->shouldReceive('call')->with('funkceB', $args)->once()->andReturn($value);
59
60
        $cache = new Psr16Cache(new ArrayAdapter());
61
        $decoratedService = new CacheDecorator($webService, $cache, 30);
0 ignored issues
show
Bug introduced by
It seems like $webService defined by \Mockery::mock(\Skaut\Sk...erviceInterface::class) on line 56 can also be of type object<Mockery\MockInterface>; however, Skaut\Skautis\Wsdl\Decor...ecorator::__construct() does only seem to accept object<Skaut\Skautis\Wsdl\WebServiceInterface>, 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...
62
63
64
65
66
        // Stejne volani pouze 1x WebService
67
        $response = $decoratedService->call('funkceA', $args);
68
        $this->assertEquals($value, $response);
69
70
        // __call() stejny jako call()
71
        $response = $decoratedService->funkceA($args[0], $args[1]);
72
        $this->assertEquals($value, $response);
73
74
        // Stejne parametry jina funkce
75
        $response = $decoratedService->call('funkceB', $args);
76
        $this->assertEquals($value, $response);
77
78
79
        // Zmena obsahu parametru
80
        $args[0] = 'qwe';
81
        $webService->shouldReceive('call')->with('funkceA', $args)->once()->andReturn($value);
82
83
        $response = $decoratedService->call('funkceA', $args);
84
        $this->assertEquals($value, $response);
85
86
87
        // Odebrani parametru
88
        unset($args['1']);
89
        $webService->shouldReceive('call')->with('funkceA', $args)->once()->andReturn($value);
90
91
        $response = $decoratedService->call('funkceA', $args);
92
        $this->assertEquals($value, $response);
93
    }
94
}
95