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 |
||
13 | class TaskTypeService extends BaseService |
||
14 | { |
||
15 | /** |
||
16 | * @var TaskType[] |
||
17 | */ |
||
18 | protected $entities = []; |
||
19 | |||
20 | /** |
||
21 | * @param $array |
||
22 | * |
||
23 | * @return EntityInterface |
||
24 | */ |
||
25 | 3 | public function parseArrayToEntity($array) |
|
32 | |||
33 | /** |
||
34 | * @return string |
||
35 | */ |
||
36 | 3 | protected function getLink() |
|
40 | |||
41 | /** |
||
42 | * @param \linkprofit\AmoCRM\entities\EntityInterface $taskType |
||
43 | * |
||
44 | * @return mixed |
||
45 | */ |
||
46 | 3 | public function add(EntityInterface $taskType) |
|
47 | { |
||
48 | 3 | if ($taskType instanceof TaskType) { |
|
49 | 3 | $this->entities[] = $taskType; |
|
50 | 3 | } |
|
51 | 3 | } |
|
52 | |||
53 | /** |
||
54 | * @return bool|mixed |
||
55 | */ |
||
56 | 3 | View Code Duplication | public function save() |
68 | |||
69 | /** |
||
70 | * Fill fields for save request |
||
71 | */ |
||
72 | 3 | View Code Duplication | protected function composeFields() |
73 | { |
||
74 | 3 | $addFields = []; |
|
75 | 3 | $updateFields = []; |
|
76 | |||
77 | 3 | foreach ($this->entities as $entity) { |
|
78 | 3 | if ($entity->id) { |
|
79 | 1 | $updateFields[] = $entity->get(); |
|
80 | 1 | } else { |
|
81 | 2 | $addFields[] = $entity->get(); |
|
82 | } |
||
83 | 3 | } |
|
84 | |||
85 | 3 | $this->fields['ACTION'] = 'ALL_EDIT'; |
|
86 | |||
87 | 3 | $fields = array_merge($addFields, $updateFields); |
|
88 | |||
89 | 3 | if (count($fields)) { |
|
90 | 3 | $this->fields['task_types'] = $fields; |
|
91 | 3 | } |
|
92 | 3 | } |
|
93 | |||
94 | /** |
||
95 | * @return array|bool |
||
96 | */ |
||
97 | 3 | View Code Duplication | public function parseResponseToEntities() |
98 | { |
||
99 | 3 | if (!$this->checkResponse()) { |
|
100 | 1 | return false; |
|
101 | } |
||
102 | 2 | $this->entities = []; |
|
103 | |||
104 | 2 | foreach ($this->response['data'] as $item) { |
|
105 | 2 | $this->entities[] = $this->parseArrayToEntity($item); |
|
106 | 2 | } |
|
107 | |||
108 | 2 | return $this->entities; |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * @return bool |
||
113 | */ |
||
114 | 3 | protected function checkResponse() |
|
122 | } |
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.