FakeEndpointBuilder   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 30.56 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 4
dl 11
loc 36
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getBuildersList() 0 7 1
A build() 11 11 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 hiapi\tests\unit\Endpoints;
4
5
use Closure;
6
use hiapi\endpoints\EndpointBuilderInterface;
7
use hiapi\endpoints\EndpointConfiguration;
8
use hiapi\endpoints\Module\Builder\OrderedBuildersCallTrait;
9
use hiapi\endpoints\Module\Builder\ReflectionBasedEndpointBuilderTrait;
10
use hiapi\endpoints\Module\InOutControl\ExamplesAwareBuilderInterface;
11
use hiapi\endpoints\Module\InOutControl\ExamplesAwareBuilderTrait;
12
use hiapi\endpoints\Module\InOutControl\InOutControlBuilderInterface;
13
use hiapi\endpoints\Module\InOutControl\InOutControlBuilderTrait;
14
use hiapi\tests\unit\Endpoints\support\FakeEndpoint;
15
use hiapi\tests\unit\Endpoints\support\InputStub;
16
use hiapi\tests\unit\Endpoints\support\ReturnStub;
17
use PHPUnit\Framework\TestCase;
18
19
/**
20
 * @group endpoints
21
 */
22
class EndpointBuilderTest extends TestCase
23
{
24
    protected $builder;
25
26
    protected function setUp()
27
    {
28
        parent::setUp();
29
30
        $this->builder = new FakeEndpointBuilder();
31
    }
32
33
    public function testBuilderCanProduceEndpoint()
34
    {
35
        $builder = $this->builder
36
            ->take(InputStub::class)
37
            ->return(ReturnStub::class);
38
39
        $endpoint = $builder->build();
40
41
        $this->assertSame(InputStub::class, $endpoint->getConfig()['take']);
42
        $this->assertSame(ReturnStub::class, $endpoint->getConfig()['return']);
43
    }
44
}
45
46
/**
47
 * Class TestABC
48
 *
49
 * @author Dmytro Naumenko <[email protected]>
50
 */
51
class FakeEndpointBuilder implements
52
    EndpointBuilderInterface,
53
    ExamplesAwareBuilderInterface,
54
    InOutControlBuilderInterface
55
{
56
    use ExamplesAwareBuilderTrait;
57
    use InOutControlBuilderTrait {
58
        buildInOutParameters as buildInOutControl;
59
    }
60
61
    /**
62
     * @return Closure[]
63
     */
64
    protected function getBuildersList(): array
65
    {
66
        return [
67
            Closure::fromCallable([$this, 'buildExamples']),
68
            Closure::fromCallable([$this, 'buildInOutControl']),
69
        ];
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75 View Code Duplication
    public function build()
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...
76
    {
77
        $builders = $this->getBuildersList();
78
79
        $config = new EndpointConfiguration();
80
        foreach ($builders as $builder) {
81
            $builder($config);
82
        }
83
84
        return FakeEndpoint::fromConfig($config);
85
    }
86
};
87