Completed
Pull Request — master (#116)
by Joel
04:48
created

HttpFulfilledPromise   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 1
dl 49
loc 49
ccs 0
cts 25
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A then() 12 12 3
A getState() 4 4 1
A wait() 6 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Http\Client\Promise;
4
5
use Http\Client\Exception;
6
use Http\Promise\Promise;
7
use Psr\Http\Message\ResponseInterface;
8
9 View Code Duplication
final class HttpFulfilledPromise implements Promise
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
10
{
11
    /**
12
     * @var ResponseInterface
13
     */
14
    private $response;
15
16
    /**
17
     * @param ResponseInterface $response
18
     */
19
    public function __construct(ResponseInterface $response)
20
    {
21
        $this->response = $response;
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function then(callable $onFulfilled = null, callable $onRejected = null)
28
    {
29
        if (null === $onFulfilled) {
30
            return $this;
31
        }
32
33
        try {
34
            return new self($onFulfilled($this->response));
35
        } catch (Exception $e) {
36
            return new HttpRejectedPromise($e);
0 ignored issues
show
Documentation introduced by
$e is of type object<Http\Client\Exception>, but the function expects a object<Exception>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
37
        }
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getState()
44
    {
45
        return Promise::FULFILLED;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function wait($unwrap = true)
52
    {
53
        if ($unwrap) {
54
            return $this->response;
55
        }
56
    }
57
}
58