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 |
||
24 | class MeOrderCreateFromCartRequest extends AbstractApiRequest |
||
25 | { |
||
26 | const ID = 'id'; |
||
27 | const VERSION = 'version'; |
||
28 | |||
29 | protected $cartId; |
||
30 | protected $version; |
||
31 | |||
32 | protected $resultClass = '\Commercetools\Core\Model\Order\Order'; |
||
33 | |||
34 | /** |
||
35 | * @return mixed |
||
36 | */ |
||
37 | 3 | public function getCartId() |
|
41 | |||
42 | /** |
||
43 | * @param $cartId |
||
44 | * @return $this |
||
45 | */ |
||
46 | 3 | public function setCartId($cartId) |
|
52 | |||
53 | /** |
||
54 | * @return mixed |
||
55 | */ |
||
56 | 3 | public function getVersion() |
|
60 | |||
61 | /** |
||
62 | * @param $version |
||
63 | * @return $this |
||
64 | */ |
||
65 | 3 | public function setVersion($version) |
|
71 | |||
72 | /** |
||
73 | * @param string $cartId |
||
74 | * @param int $version |
||
75 | * @param Context $context |
||
76 | */ |
||
77 | 3 | public function __construct($cartId, $version, Context $context = null) |
|
82 | |||
83 | /** |
||
84 | * @param string $cartId |
||
85 | * @param int $version |
||
86 | * @param Context $context |
||
87 | * @return static |
||
88 | */ |
||
89 | 3 | public static function ofCartIdAndVersion($cartId, $version, Context $context = null) |
|
93 | |||
94 | /** |
||
95 | * @param ResponseInterface $response |
||
96 | * @return AbstractApiResponse |
||
97 | * @internal |
||
98 | */ |
||
99 | 3 | public function buildResponse(ResponseInterface $response) |
|
103 | |||
104 | /** |
||
105 | * @return HttpRequestInterface |
||
106 | * @internal |
||
107 | */ |
||
108 | 3 | View Code Duplication | public function httpRequest() |
117 | } |
||
118 |
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.