Guzzle3Mock::getMock()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
1
<?php
2
3
namespace CanalTP\AbstractGuzzle\Mock;
4
5
use CanalTP\AbstractGuzzle\GuzzleFactory;
6
use Guzzle\Http\Message\Response;
7
use Guzzle\Plugin\Mock\MockPlugin;
8
use GuzzleHttp\Psr7\Response as Psr7Response;
9
10
class Guzzle3Mock
11
{
12
    /**
13
     * @param array $responseCollection
14
     * @return \CanalTP\AbstractGuzzle\Guzzle
15
     */
16
    public function getMock(array $responseCollection)
17
    {
18
        $plugin = new MockPlugin();
19
        foreach ($responseCollection as $response) {
20
            if ($response instanceof Psr7Response) {
21
                $response = new Response($response->getStatusCode(), $response->getHeaders(), $response->getBody());
22
            }
23
            $plugin->addResponse($response);
24
        }
25
26
        $client = GuzzleFactory::createClient('');
27
        $client->addSubscriber($plugin);
0 ignored issues
show
Bug introduced by
The method addSubscriber does only exist in CanalTP\AbstractGuzzle\Version\Guzzle3, but not in CanalTP\AbstractGuzzle\V...tGuzzle\Version\Guzzle6.

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...
28
29
        return $client;
30
    }
31
}
32