RejectedPromise   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 76
Duplicated Lines 19.74 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%
Metric Value
dl 15
loc 76
wmc 14
lcom 1
cbo 1
ccs 33
cts 33
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A then() 15 15 3
A done() 0 16 4
A otherwise() 0 8 2
A always() 0 8 1
A progress() 0 4 1
A cancel() 0 3 1

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 Thruster\Component\Promise;
4
5
use Thruster\Component\Promise\Exception\UnhandledRejectionException;
6
7
/**
8
 * Class RejectedPromise
9
 *
10
 * @package Thruster\Component\Promise
11
 * @author  Aurimas Niekis <[email protected]>
12
 */
13
class RejectedPromise implements ExtendedPromiseInterface, CancellablePromiseInterface
14
{
15
    private $reason;
16
17 231
    public function __construct($reason = null)
18
    {
19 231
        if ($reason instanceof PromiseInterface) {
20 1
            throw new \InvalidArgumentException(
21
                'You cannot create Thruster\Component\Promise\RejectedPromise with a promise.' .
22 1
                ' Use Thruster\Component\Promise\reject($promiseOrValue) instead.'
23
            );
24
        }
25
26 231
        $this->reason = $reason;
27 231
    }
28
29 146 View Code Duplication
    public function then(
0 ignored issues
show
Duplication introduced by
This method 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...
30
        callable $onFulfilled = null,
31
        callable $onRejected = null,
32
        callable $onProgress = null
33
    ) : PromiseInterface {
34 146
        if (null === $onRejected) {
35 27
            return $this;
36
        }
37
38
        try {
39 144
            return resolve($onRejected($this->reason));
40 11
        } catch (\Exception $exception) {
41 11
            return new RejectedPromise($exception);
42
        }
43
    }
44
45 101
    public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
46
    {
47 101
        if (null === $onRejected) {
48 31
            throw UnhandledRejectionException::resolve($this->reason);
49
        }
50
51 76
        $result = $onRejected($this->reason);
52
53 66
        if ($result instanceof self) {
54 14
            throw UnhandledRejectionException::resolve($result->reason);
55
        }
56
57 52
        if ($result instanceof ExtendedPromiseInterface) {
58 3
            $result->done();
59
        }
60 52
    }
61
62 4
    public function otherwise(callable $onRejected) : ExtendedPromiseInterface
63
    {
64 4
        if (!_checkTypehint($onRejected, $this->reason)) {
65 1
            return $this;
66
        }
67
68 3
        return $this->then(null, $onRejected);
69
    }
70
71
    public function always(callable $onFulfilledOrRejected) : ExtendedPromiseInterface
72
    {
73
        return $this->then(null, function ($reason) use ($onFulfilledOrRejected) {
74 5
            return resolve($onFulfilledOrRejected())->then(function () use ($reason) {
75 4
                return new RejectedPromise($reason);
76 5
            });
77 6
        });
78
    }
79
80 1
    public function progress(callable $onProgress) : ExtendedPromiseInterface
81
    {
82 1
        return $this;
83
    }
84
85 4
    public function cancel()
86
    {
87 4
    }
88
}
89