Guzzle3Mock   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 22
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getMock() 0 15 3
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