Completed
Pull Request — master (#9)
by Trevor N.
02:02
created

AbstractDelegateContextualBuilder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 54
loc 54
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A build() 11 11 3

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
 * Incoming
4
 *
5
 * @author    Trevor Suarez (Rican7)
6
 * @copyright (c) Trevor Suarez
7
 * @link      https://github.com/Rican7/incoming
8
 * @license   MIT
9
 */
10
11
declare(strict_types=1);
12
13
namespace Incoming\Hydrator;
14
15
use Incoming\Structure\Map;
16
17
/**
18
 * An abstract, context enabled extension of the `AbstractDelegateBuilder`.
19
 *
20
 * @see AbstractDelegateBuilder
21
 */
22 View Code Duplication
abstract class AbstractDelegateContextualBuilder extends AbstractDelegateBuilder implements ContextualBuilder
1 ignored issue
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...
23
{
24
25
    /**
26
     * Properties
27
     */
28
29
    /**
30
     * Whether or not to provide a fallback empty context, when a `null` context
31
     * is otherwise provided, to make processes simpler by not having to rely on
32
     * null checks of the actual parameter before usage.
33
     *
34
     * @var bool
35
     */
36
    private $provide_fallback_context = false;
37
38
39
    /**
40
     * Methods
41
     */
42
43
    /**
44
     * Constructor
45
     *
46
     * @param bool $provide_fallback_context Whether or not to provide a
47
     *  fallback empty context, when a `null` context is otherwise provided, to
48
     *  make processes simpler by not having to rely on null checks of the
49
     *  actual parameter before usage.
50
     */
51 6
    protected function __construct(bool $provide_fallback_context = false)
52
    {
53 6
        $this->provide_fallback_context = $provide_fallback_context;
54 6
    }
55
56
    /**
57
     * {@inheritdoc}
58
     *
59
     * @param mixed $incoming The input data.
60
     * @param Map|null $context An optional generic key-value map, for providing
61
     *  contextual values during the build process.
62
     * @return mixed The built model.
63
     */
64 6
    public function build($incoming, Map $context = null)
65
    {
66 6
        $callable = $this->getDelegate();
67
68 6
        if (null === $context && $this->provide_fallback_context) {
69
            // Provide a non-null context so null checks aren't later required
70 3
            $context = new Map();
71
        }
72
73 6
        return $callable($incoming, $context);
74
    }
75
}
76