Guzzle5Mock   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 28
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getMock() 0 21 3
1
<?php
2
3
namespace CanalTP\AbstractGuzzle\Mock;
4
5
use CanalTP\AbstractGuzzle\GuzzleFactory;
6
use GuzzleHttp\Message\Response;
7
use GuzzleHttp\Psr7\Response as psr7Response;
8
use GuzzleHttp\Stream\Stream;
9
use GuzzleHttp\Subscriber\Mock;
10
11
class Guzzle5Mock
12
{
13
    /**
14
     * @param Response[] $responseCollection
15
     * @return \CanalTP\AbstractGuzzle\Guzzle
16
     */
17
    public function getMock(array $responseCollection)
18
    {
19
        $version5ResponseCollection = [];
20
        $client = GuzzleFactory::createClient('');
21
22
        foreach ($responseCollection as $response) {
23
            if ($response instanceof Psr7Response) {
24
                $version5ResponseCollection[] = new Response(
25
                    $response->getStatusCode(),
26
                    $response->getHeaders(),
27
                    Stream::factory($response->getBody()->getContents())
28
                );
29
            } else {
30
                $version5ResponseCollection[] = new Response();
31
            }
32
        }
33
        $mock = new Mock($version5ResponseCollection);
34
        $client->getEmitter()->attach($mock);
0 ignored issues
show
Bug introduced by
The method getEmitter does only exist in CanalTP\AbstractGuzzle\Version\Guzzle5, 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...
35
36
        return $client;
37
    }
38
}
39