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

Guzzle3Mock   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

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

1 Method

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