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 |
||
12 | abstract class BaseService implements ServiceInterface, ListableService |
||
13 | { |
||
14 | /** |
||
15 | * @var RequestHandler |
||
16 | */ |
||
17 | protected $request; |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $fields = []; |
||
23 | |||
24 | /** |
||
25 | * @var mixed |
||
26 | */ |
||
27 | protected $response; |
||
28 | |||
29 | /** |
||
30 | * @var array EntityInterface |
||
31 | */ |
||
32 | protected $entities = []; |
||
33 | |||
34 | /** |
||
35 | * LeadService constructor. |
||
36 | * @param RequestHandler $requestHandler |
||
37 | */ |
||
38 | 59 | public function __construct(RequestHandler $requestHandler) |
|
39 | { |
||
40 | 59 | $this->request = $requestHandler; |
|
41 | 59 | } |
|
42 | |||
43 | /** |
||
44 | * @return bool|mixed |
||
45 | */ |
||
46 | 36 | View Code Duplication | public function save() |
|
|||
47 | { |
||
48 | 36 | $this->composeFields(); |
|
49 | 36 | $this->request->performRequest($this->getLink(), $this->fields); |
|
50 | 36 | $this->response = $this->request->getResponse(); |
|
51 | |||
52 | 36 | if ($this->checkResponse()) { |
|
53 | 26 | return $this->getResponse(); |
|
54 | } |
||
55 | |||
56 | 10 | return false; |
|
57 | } |
||
58 | |||
59 | /** |
||
60 | * @return array|bool |
||
61 | */ |
||
62 | 9 | public function getList() |
|
63 | { |
||
64 | 9 | $link = $this->composeListLink($this->getLink()); |
|
65 | |||
66 | 9 | $this->request->performRequest($link, [], 'application/json', 'GET'); |
|
67 | 9 | $this->response = $this->request->getResponse(); |
|
68 | |||
69 | 9 | return $this->parseResponseToEntities(); |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * @param $link |
||
74 | * |
||
75 | * @return mixed |
||
76 | */ |
||
77 | protected function composeListLink($link) |
||
78 | { |
||
79 | return $link; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @return mixed |
||
84 | */ |
||
85 | 28 | public function getResponse() |
|
86 | { |
||
87 | 28 | return $this->response; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * @return array |
||
92 | */ |
||
93 | 22 | public function getEntities() |
|
94 | { |
||
95 | 22 | return $this->entities; |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * @return array|bool |
||
100 | */ |
||
101 | 45 | View Code Duplication | public function parseResponseToEntities() |
102 | { |
||
103 | 45 | if (!$this->checkResponse()) { |
|
104 | 10 | return false; |
|
105 | } |
||
106 | 35 | $this->entities = []; |
|
107 | |||
108 | 35 | foreach ($this->response['_embedded']['items'] as $item) { |
|
109 | 35 | $this->entities[] = $this->parseArrayToEntity($item); |
|
110 | 35 | } |
|
111 | |||
112 | 35 | return $this->entities; |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param $array |
||
117 | * @return EntityInterface |
||
118 | */ |
||
119 | abstract public function parseArrayToEntity($array); |
||
120 | |||
121 | /** |
||
122 | * @return bool |
||
123 | */ |
||
124 | 45 | protected function checkResponse() |
|
125 | { |
||
126 | 45 | if (isset($this->response['_embedded']['items']) && count($this->response['_embedded']['items'])) { |
|
127 | 35 | return true; |
|
128 | } |
||
129 | |||
130 | 10 | return false; |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * Fill fields for save request |
||
135 | */ |
||
136 | 36 | View Code Duplication | protected function composeFields() |
137 | { |
||
138 | 36 | $addFields = []; |
|
139 | 36 | $updateFields = []; |
|
140 | |||
141 | 36 | foreach ($this->entities as $entity) { |
|
142 | 36 | if ($entity->id) { |
|
143 | 10 | $updateFields[] = $entity->get(); |
|
144 | 10 | } else { |
|
145 | 26 | $addFields[] = $entity->get(); |
|
146 | } |
||
147 | 36 | } |
|
148 | |||
149 | 36 | if (count($addFields)) { |
|
150 | 26 | $this->fields['add'] = $addFields; |
|
151 | 26 | } |
|
152 | |||
153 | 36 | if (count($updateFields)) { |
|
154 | 10 | $this->fields['update'] = $updateFields; |
|
155 | 10 | } |
|
156 | 36 | } |
|
157 | |||
158 | /** |
||
159 | * @return string |
||
160 | */ |
||
161 | abstract protected function getLink(); |
||
162 | } |
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.