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

HttpRejectedPromise::then()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 12
loc 12
ccs 0
cts 11
cp 0
rs 9.4285
cc 3
eloc 7
nc 3
nop 2
crap 12
1
<?php
2
3
namespace Http\Client\Promise;
4
5
use Http\Client\Exception;
6
use Http\Promise\Promise;
7
8 View Code Duplication
final class HttpRejectedPromise 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...
9
{
10
    /**
11
     * @var \Exception
12
     */
13
    private $exception;
14
15
    /**
16
     * @param \Exception $exception
17
     */
18
    public function __construct(\Exception $exception)
19
    {
20
        $this->exception = $exception;
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function then(callable $onFulfilled = null, callable $onRejected = null)
27
    {
28
        if (null === $onRejected) {
29
            return $this;
30
        }
31
32
        try {
33
            return new HttpFulfilledPromise($onRejected($this->exception));
34
        } catch (Exception $e) {
35
            return new self($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...
36
        }
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getState()
43
    {
44
        return Promise::REJECTED;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function wait($unwrap = true)
51
    {
52
        if ($unwrap) {
53
            throw $this->exception;
54
        }
55
    }
56
}
57