FulfilledPromise   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 68
Duplicated Lines 22.06 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%
Metric Value
dl 15
loc 68
wmc 12
lcom 1
cbo 2
ccs 29
cts 29
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A then() 15 15 3
A done() 0 12 3
A otherwise() 0 4 1
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
/**
6
 * Class FulfilledPromise
7
 *
8
 * @package Thruster\Component\Promise
9
 * @author  Aurimas Niekis <[email protected]>
10
 */
11
class FulfilledPromise implements ExtendedPromiseInterface, CancellablePromiseInterface
12
{
13
    private $value;
14
15 339
    public function __construct($value = null)
16
    {
17 339
        if ($value instanceof PromiseInterface) {
18 1
            throw new \InvalidArgumentException(
19
                'You cannot create Thruster\Component\Promise\FulfilledPromise with a promise.' .
20 1
                ' Use Thruster\Component\Promise\resolve($promiseOrValue) instead.'
21
            );
22
        }
23
24 339
        $this->value = $value;
25 339
    }
26
27 213 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...
28
        callable $onFulfilled = null,
29
        callable $onRejected = null,
30
        callable $onProgress = null
31
    ) : PromiseInterface {
32 213
        if (null === $onFulfilled) {
33 26
            return $this;
34
        }
35
36
        try {
37 194
            return resolve($onFulfilled($this->value));
38 18
        } catch (\Exception $exception) {
39 18
            return new RejectedPromise($exception);
40
        }
41
    }
42
43 136
    public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
44
    {
45 136
        if (null === $onFulfilled) {
46 8
            return;
47
        }
48
49 128
        $result = $onFulfilled($this->value);
50
51 121
        if ($result instanceof ExtendedPromiseInterface) {
52 7
            $result->done();
53
        }
54 114
    }
55
56 1
    public function otherwise(callable $onRejected) : ExtendedPromiseInterface
57
    {
58 1
        return $this;
59
    }
60
61
    public function always(callable $onFulfilledOrRejected) : ExtendedPromiseInterface
62
    {
63
        return $this->then(function ($value) use ($onFulfilledOrRejected) {
64 5
            return resolve($onFulfilledOrRejected())->then(function () use ($value) {
65 4
                return $value;
66 5
            });
67 6
        });
68
    }
69
70 1
    public function progress(callable $onProgress) : ExtendedPromiseInterface
71
    {
72 1
        return $this;
73
    }
74
75 4
    public function cancel()
76
    {
77 4
    }
78
}
79