Completed
Push — master ( 17da57...0d4fd4 )
by Denis
02:07
created

RequestBuilder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 14.71 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 10
loc 68
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A build() 0 23 3
A onPreBuild() 0 4 1
A preBuild() 10 10 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 PhpJsonRpc\Client;
4
5
use PhpJsonRpc\Client\RequestBuilder\BuilderContainer;
6
use PhpJsonRpc\Common\Interceptor\Interceptor;
7
use PhpJsonRpc\Core\Invoke\AbstractInvoke;
8
use PhpJsonRpc\Core\Invoke\Invoke;
9
use PhpJsonRpc\Core\InvokeSpec;
10
11
class RequestBuilder
12
{
13
    /**
14
     * @var Interceptor
15
     */
16
    private $preBuild;
17
18
    /**
19
     * RequestBuilder constructor.
20
     */
21
    public function __construct()
22
    {
23
        $this->preBuild = Interceptor::createBase();
24
    }
25
26
    /**
27
     * @param InvokeSpec $call
28
     *
29
     * @return string
30
     */
31
    public function build(InvokeSpec $call): string
32
    {
33
        $response = [];
34
        $units    = $call->getUnits();
35
36
        foreach ($units as $invoke) {
37
            /** @var Invoke $invoke */
38
            $invoke = $this->preBuild($invoke);
39
40
            $response[] = [
41
                'jsonrpc' => '2.0',
42
                'method'  => $invoke->getRawMethod(),
43
                'params'  => $invoke->getRawParams(),
44
                'id'      => $invoke->getRawId()
45
            ];
46
        }
47
48
        if ($call->isSingleCall()) {
49
            return json_encode($response[0]);
50
        }
51
52
        return json_encode($response);
53
    }
54
55
    /**
56
     * @return Interceptor
57
     */
58
    public function onPreBuild(): Interceptor
59
    {
60
        return $this->preBuild;
61
    }
62
63
    /**
64
     * @param AbstractInvoke $invoke
65
     *
66
     * @return AbstractInvoke
67
     */
68 View Code Duplication
    private function preBuild(AbstractInvoke $invoke): AbstractInvoke
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...
69
    {
70
        $result = $this->preBuild->handle(new BuilderContainer($this, $invoke));
71
72
        if ($result instanceof BuilderContainer) {
73
            return $result->getInvoke();
74
        }
75
76
        throw new \RuntimeException();
77
    }
78
}
79