Total Complexity | 51 |
Total Lines | 189 |
Duplicated Lines | 20.11 % |
Changes | 0 |
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:
Complex classes like Handler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Handler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Handler |
||
12 | { |
||
13 | /** |
||
14 | * Method to convert a Siren document to a JSON string |
||
15 | * |
||
16 | * @param Document $document |
||
17 | * @return string |
||
18 | */ |
||
19 | public function toJson(Document $document) |
||
20 | { |
||
21 | $documentArray = $document->toArray(); |
||
22 | |||
23 | return json_encode($documentArray); |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * Method to convert a siren JSON string into a Siren document object |
||
28 | * |
||
29 | * @param string $json |
||
30 | * @return Document |
||
31 | */ |
||
32 | public function toDocument($json) |
||
33 | { |
||
34 | $data = json_decode($json, true); |
||
35 | |||
36 | $document = new Document(); |
||
37 | |||
38 | View Code Duplication | if (isset($data['class']) && is_array($data['class'])) { |
|
|
|||
39 | $document->setClass($data['class']); |
||
40 | } |
||
41 | |||
42 | View Code Duplication | if (isset($data['properties']) && is_array($data['properties'])) { |
|
43 | $document->setProperties($data['properties']); |
||
44 | } |
||
45 | |||
46 | if (isset($data['entities']) && is_array($data['entities'])) { |
||
47 | $entities = $this->getEntitiesFromDataArray($data['entities']); |
||
48 | $document->setEntities($entities); |
||
49 | } |
||
50 | |||
51 | if (isset($data['actions']) && is_array($data['actions'])) { |
||
52 | $actions = $this->getActionsFromDataArray($data['actions']); |
||
53 | $document->setActions($actions); |
||
54 | } |
||
55 | |||
56 | View Code Duplication | if (isset($data['links']) && is_array($data['links'])) { |
|
57 | $links = $this->getLinksFromDataArray($data['links']); |
||
58 | $document->setLinks($links); |
||
59 | } |
||
60 | |||
61 | return $document; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Helper method to convert an entities data array to an array of entity objects |
||
66 | * |
||
67 | * @param array $entitiesArray |
||
68 | * @return Entity[] |
||
69 | */ |
||
70 | protected function getEntitiesFromDataArray(array $entitiesArray) |
||
71 | { |
||
72 | $entities = array(); |
||
73 | foreach ($entitiesArray as $entityArray) { |
||
74 | $entity = new Entity(); |
||
75 | |||
76 | View Code Duplication | if (isset($entityArray['class']) && is_array($entityArray['class'])) { |
|
77 | $entity->setClass($entityArray['class']); |
||
78 | } |
||
79 | |||
80 | if (isset($entityArray['rel']) && is_array($entityArray['rel'])) { |
||
81 | $entity->setRel($entityArray['rel']); |
||
82 | } |
||
83 | |||
84 | if (isset($entityArray['href']) && is_string($entityArray['href'])) { |
||
85 | $entity->setHref($entityArray['href']); |
||
86 | } |
||
87 | |||
88 | View Code Duplication | if (isset($entityArray['properties']) && is_array($entityArray['properties'])) { |
|
89 | $entity->setProperties($entityArray['properties']); |
||
90 | } |
||
91 | |||
92 | View Code Duplication | if (isset($entityArray['links']) && is_array($entityArray['links'])) { |
|
93 | $links = $this->getLinksFromDataArray($entityArray['links']); |
||
94 | $entity->setLinks($links); |
||
95 | } |
||
96 | |||
97 | $entities[] = $entity; |
||
98 | } |
||
99 | |||
100 | return $entities; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Helper method to convert an action data array to an array of action objects |
||
105 | * |
||
106 | * @param array $actionsArray |
||
107 | * @return Action[] |
||
108 | */ |
||
109 | protected function getActionsFromDataArray(array $actionsArray) |
||
110 | { |
||
111 | $actions = array(); |
||
112 | foreach ($actionsArray as $actionArray) { |
||
113 | $action = new Action(); |
||
114 | |||
115 | View Code Duplication | if (isset($actionArray['name']) && is_string($actionArray['name'])) { |
|
116 | $action->setName($actionArray['name']); |
||
117 | } |
||
118 | |||
119 | if (isset($actionArray['title']) && is_string($actionArray['title'])) { |
||
120 | $action->setTitle($actionArray['title']); |
||
121 | } |
||
122 | |||
123 | View Code Duplication | if (isset($actionArray['method']) && is_string($actionArray['method'])) { |
|
124 | $action->setMethod($actionArray['method']); |
||
125 | } |
||
126 | |||
127 | View Code Duplication | if (isset($actionArray['href']) && is_string($actionArray['href'])) { |
|
128 | $action->setHref($actionArray['href']); |
||
129 | } |
||
130 | |||
131 | View Code Duplication | if (isset($actionArray['type']) && is_string($actionArray['type'])) { |
|
132 | $action->setType($actionArray['type']); |
||
133 | } |
||
134 | |||
135 | if (isset($actionArray['fields']) && is_array($actionArray['fields'])) { |
||
136 | $fields = $this->getFieldsFromDataArray($actionArray['fields']); |
||
137 | $action->setFields($fields); |
||
138 | } |
||
139 | |||
140 | $actions[] = $action; |
||
141 | } |
||
142 | |||
143 | return $actions; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Helper method to convert a fields data array to an array of field objects |
||
148 | * |
||
149 | * @param array $fieldsArray |
||
150 | * @return Field[] |
||
151 | */ |
||
152 | protected function getFieldsFromDataArray(array $fieldsArray) |
||
153 | { |
||
154 | $fields = array(); |
||
155 | foreach ($fieldsArray as $fieldArray) { |
||
156 | $field = new Field(); |
||
157 | |||
158 | View Code Duplication | if (isset($fieldArray['name']) && is_string($fieldArray['name'])) { |
|
159 | $field->setName($fieldArray['name']); |
||
160 | } |
||
161 | |||
162 | View Code Duplication | if (isset($fieldArray['type']) && is_string($fieldArray['type'])) { |
|
163 | $field->setType($fieldArray['type']); |
||
164 | } |
||
165 | |||
166 | if (isset($fieldArray['value'])) { |
||
167 | $field->setValue($fieldArray['value']); |
||
168 | } |
||
169 | |||
170 | $fields[] = $field; |
||
171 | } |
||
172 | |||
173 | return $fields; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Helper method to convert a link data array to an array of link objects |
||
178 | * |
||
179 | * @param array $linksArray |
||
180 | * @return Link[] |
||
181 | */ |
||
182 | protected function getLinksFromDataArray(array $linksArray) |
||
200 | } |
||
201 | } |
||
202 |
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.