Completed
Pull Request — master (#5)
by Jean-Baptiste
01:43
created

Guzzle3Mock::getMock()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
namespace CanalTP\AbstractGuzzle\Mock;
4
5
use CanalTP\AbstractGuzzle\GuzzleFactory;
6
use Guzzle\Plugin\Mock\MockPlugin;
7
8
class Guzzle3Mock
9
{
10
    /**
11
     * @param array $responseCollection
12
     * @return \CanalTP\AbstractGuzzle\Guzzle
13
     */
14
    public function getMock(array $responseCollection)
15
    {
16
        $plugin = new MockPlugin();
17
        foreach ($responseCollection as $response) {
18
            $plugin->addResponse($response);
19
        }
20
21
        $client = GuzzleFactory::createGuzzle('');
22
        $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...
23
24
        return $client;
25
    }
26
}
27