Passed
Pull Request — master (#11)
by Pavel
11:58
created

JsonRpcRequest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 85
Duplicated Lines 10.59 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 65.71%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 9
loc 85
ccs 23
cts 35
cp 0.6571
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A jsonSerialize() 9 9 1
C fromStdClass() 0 28 7
A isNotification() 0 4 1
A getVersion() 0 4 1
A getMethod() 0 4 1
A getParameters() 0 4 1
A getId() 0 4 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 Bankiru\Api\JsonRpc\Specification;
4
5
use Bankiru\Api\JsonRpc\BankiruJsonRpcServerBundle;
6
use Bankiru\Api\JsonRpc\Exception\InvalidRequestException;
7
use Bankiru\Api\JsonRpc\Exception\JsonRpcException;
8
use ScayTrase\Api\JsonRpc\JsonRpcRequestInterface;
9
10
/** @internal */
11
final class JsonRpcRequest implements JsonRpcRequestInterface
12
{
13
    /** @var  string|null */
14
    private $id;
15
    /** @var  string */
16
    private $method;
17
    /** @var  mixed|\stdClass */
18
    private $parameters;
19
20
    /**
21
     * @param \stdClass $source
22
     *
23
     * @return JsonRpcRequestInterface
24
     * @throws JsonRpcException
25
     */
26 19
    public static function fromStdClass(\stdClass $source)
27
    {
28 19
        $request = new static;
29
30 19
        $missing = [];
31
32 19
        foreach (['method', 'jsonrpc'] as $field) {
33 19
            if (!isset($source->$field)) {
34 6
                $missing[] = $field;
35 6
            }
36 19
        }
37 19
        if (count($missing) > 0) {
38 6
            throw InvalidRequestException::missingFields($missing);
39
        }
40
41 13
        if (BankiruJsonRpcServerBundle::JSONRPC_VERSION !== $source->jsonrpc) {
42
            throw InvalidRequestException::invalidVersion(
43
                BankiruJsonRpcServerBundle::JSONRPC_VERSION,
44
                $source->jsonrpc
45
            );
46
        }
47
48 13
        $request->id         = isset($source->id) ? $source->id : null;
49 13
        $request->method     = $source->method;
50 13
        $request->parameters = isset($source->params) ? json_decode(json_encode($source->params), true) : null;
51
52 13
        return $request;
53
    }
54
55
    /** {@inheritdoc} */
56 13
    public function isNotification()
57
    {
58 13
        return null === $this->id;
59
    }
60
61
    /** {@inheritdoc} */
62 View Code Duplication
    public function jsonSerialize()
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...
63
    {
64
        return [
65
            self::VERSION_FIELD    => $this->getVersion(),
66
            self::METHOD_FIELD     => $this->getMethod(),
67
            self::PARAMETERS_FIELD => $this->getParameters(),
68
            self::ID_FIELD         => $this->getId(),
69
        ];
70
    }
71
72
    /** {@inheritdoc} */
73
    public function getVersion()
74
    {
75
        return BankiruJsonRpcServerBundle::JSONRPC_VERSION;
76
    }
77
78
    /** @return string */
79 13
    public function getMethod()
80
    {
81 13
        return $this->method;
82
    }
83
84
    /** {@inheritdoc} */
85 6
    public function getParameters()
86
    {
87 6
        return $this->parameters;
88
    }
89
90
    /** {@inheritdoc} */
91 13
    public function getId()
92
    {
93 13
        return $this->id;
94
    }
95
}
96