Completed
Pull Request — master (#136)
by Fabien
06:00
created

ProfileClient::sendAsyncRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Http\HttplugBundle\Collector;
4
5
use Http\Client\Common\EmulatedHttpAsyncClient;
6
use Http\Client\Common\EmulatedHttpClient;
7
use Http\Client\HttpAsyncClient;
8
use Http\Client\HttpClient;
9
use Psr\Http\Message\RequestInterface;
10
11
class ProfileClient implements HttpClient, HttpAsyncClient
12
{
13
    /**
14
     * @var HttpClient|HttpAsyncClient $client
15
     */
16
    private $client;
17
18
    /**
19
     * @var Collector
20
     */
21
    private $collector;
22
23
    /**
24
     * @param HttpClient|HttpAsyncClient $client
25
     * @param Collector                  $collector
26
     */
27 1
    public function __construct($client, Collector $collector)
28
    {
29 1
        if ($client instanceof HttpAsyncClient && $client instanceof HttpClient) {
30 1
            $this->client = $client;
31 1
        } elseif ($client instanceof HttpAsyncClient) {
32
            $this->client = new EmulatedHttpClient($client);
33
        } elseif ($client instanceof HttpClient) {
34
            $this->client = new EmulatedHttpAsyncClient($client);
35
        } else {
36
            throw new \RuntimeException(sprintf('Client must be an instance of %s or %s', HttpClient::class, HttpAsyncClient::class));
37
        }
38
        $this->collector = $collector;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function sendAsyncRequest(RequestInterface $request)
45
    {
46
        $this->collectRequestInformations($request);
47
        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...
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function sendRequest(RequestInterface $request)
54
    {
55
        $this->collectRequestInformations($request);
56
        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...
57
    }
58
59
    /**
60
     * @param RequestInterface $request
61
     */
62
    private function collectRequestInformations(RequestInterface $request)
63
    {
64
        if (!$stack = $this->collector->getCurrentStack()) {
65
            return;
66
        }
67
68
        $stack = $this->collector->getCurrentStack();
69
        $stack->setRequestTarget($request->getRequestTarget());
70
        $stack->setRequestMethod($request->getMethod());
71
    }
72
}
73