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 |
||
10 | final class RichJsonRpcRequest implements RpcRequestInterface, JsonRpcRequestInterface |
||
11 | { |
||
12 | /** @var JsonRpcRequestInterface */ |
||
13 | private $request; |
||
14 | /** @var ParameterBag */ |
||
15 | private $attributes; |
||
16 | |||
17 | /** |
||
18 | * RichJsonRpcRequest constructor. |
||
19 | * |
||
20 | * @param JsonRpcRequestInterface $request |
||
21 | * @param ParameterBag $attributes |
||
22 | */ |
||
23 | 13 | public function __construct(JsonRpcRequestInterface $request, ParameterBag $attributes = null) |
|
24 | { |
||
25 | 13 | $this->request = $request; |
|
26 | 13 | $this->attributes = $attributes ?: new ParameterBag(); |
|
27 | 13 | } |
|
28 | |||
29 | /** {@inheritdoc} */ |
||
30 | 13 | public function isNotification() |
|
34 | |||
35 | /** {@inheritdoc} */ |
||
36 | 13 | public function getAttributes() |
|
40 | |||
41 | /** {@inheritdoc} */ |
||
42 | View Code Duplication | public function jsonSerialize() |
|
51 | |||
52 | /** {@inheritdoc} */ |
||
53 | public function getVersion() |
||
57 | |||
58 | /** {@inheritdoc} */ |
||
59 | 13 | public function getMethod() |
|
63 | |||
64 | /** {@inheritdoc} */ |
||
65 | 7 | public function getParameters() |
|
69 | |||
70 | /** {@inheritdoc} */ |
||
71 | 13 | public function getId() |
|
75 | } |
||
76 |
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.