Completed
Pull Request — master (#136)
by Fabien
08:37
created

ProfileClient::sendAsyncRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Http\HttplugBundle\Collector;
4
5
use Http\Client\Common\FlexibleHttpClient;
6
use Http\Client\HttpAsyncClient;
7
use Http\Client\HttpClient;
8
use Psr\Http\Message\RequestInterface;
9
10
/**
11
 * The ProfileClient decorates any client that implement both HttpClient and HttpAsyncClient interfaces to gather target
12
 * url and response status code.
13
 *
14
 * @author Fabien Bourigault <[email protected]>
15
 *
16
 * @internal
17
 */
18
class ProfileClient implements HttpClient, HttpAsyncClient
19
{
20
    /**
21
     * @var HttpClient|HttpAsyncClient
22
     */
23
    private $client;
24
25
    /**
26
     * @var Collector
27
     */
28
    private $collector;
29
30
    /**
31
     * @param HttpClient|HttpAsyncClient $client The client to profile. Client must implement both HttpClient and
32
     *                                           HttpAsyncClient interfaces.
33
     * @param Collector                  $collector
34
     */
35
    public function __construct($client, Collector $collector)
36
    {
37
        if (!($client instanceof HttpClient && $client instanceof HttpAsyncClient)) {
38
            throw new \RuntimeException(sprintf(
39
                '%s first argument must implement %s and %s. Consider using %s.',
40
                    __METHOD__,
41
                    HttpClient::class,
42
                    HttpAsyncClient::class,
43
                    FlexibleHttpClient::class
44
            ));
45
        }
46
        $this->client = $client;
47
        $this->collector = $collector;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function sendAsyncRequest(RequestInterface $request)
54
    {
55
        $this->collectRequestInformations($request);
56
57
        return $this->client->sendAsyncRequest($request);
0 ignored issues
show
Bug introduced by
The method sendAsyncRequest does only exist in Http\Client\HttpAsyncClient, but not in Http\Client\HttpClient.

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...
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function sendRequest(RequestInterface $request)
64
    {
65
        $this->collectRequestInformations($request);
66
67
        return $this->client->sendRequest($request);
0 ignored issues
show
Bug introduced by
The method sendRequest does only exist in Http\Client\HttpClient, but not in Http\Client\HttpAsyncClient.

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...
68
    }
69
70
    /**
71
     * @param RequestInterface $request
72
     */
73
    private function collectRequestInformations(RequestInterface $request)
74
    {
75
        if (!$stack = $this->collector->getCurrentStack()) {
76
            return;
77
        }
78
79
        $stack = $this->collector->getCurrentStack();
80
        $stack->setRequestTarget($request->getRequestTarget());
81
        $stack->setRequestMethod($request->getMethod());
82
    }
83
}
84