Completed
Pull Request — master (#136)
by Fabien
03:05
created

ProfileClient   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 40%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 65
ccs 2
cts 5
cp 0.4
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 3
A sendAsyncRequest() 0 6 1
A sendRequest() 0 6 1
A collectRequestInformations() 0 10 2
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 HttpClient (or HttpAsyncClient) to gather target url and response status code.
12
 *
13
 * @author Fabien Bourigault <[email protected]>
14
 *
15
 * @internal
16
 */
17
class ProfileClient implements HttpClient, HttpAsyncClient
18
{
19
    /**
20
     * @var HttpClient|HttpAsyncClient
21
     */
22
    private $client;
23
24
    /**
25
     * @var Collector
26
     */
27
    private $collector;
28
29
    /**
30
     * @param HttpClient|HttpAsyncClient $client
31
     * @param Collector                  $collector
32
     */
33 4
    public function __construct($client, Collector $collector)
34
    {
35 4
        if (!$client instanceof HttpClient || !$client instanceof HttpAsyncClient) {
36
            throw new \RuntimeException(sprintf(
37
                '%s first argument must implement %s and %s. Consider using %s.',
38
                    __METHOD__,
39
                    HttpClient::class,
40
                    HttpAsyncClient::class,
41
                    FlexibleHttpClient::class
42
            ));
43
        }
44
        $this->client = $client;
45
        $this->collector = $collector;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function sendAsyncRequest(RequestInterface $request)
52
    {
53
        $this->collectRequestInformations($request);
54
55
        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...
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function sendRequest(RequestInterface $request)
62
    {
63
        $this->collectRequestInformations($request);
64
65
        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...
66
    }
67
68
    /**
69
     * @param RequestInterface $request
70
     */
71
    private function collectRequestInformations(RequestInterface $request)
72
    {
73
        if (!$stack = $this->collector->getCurrentStack()) {
74
            return;
75
        }
76
77
        $stack = $this->collector->getCurrentStack();
78
        $stack->setRequestTarget($request->getRequestTarget());
79
        $stack->setRequestMethod($request->getMethod());
80
    }
81
}
82