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 ParserTools 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 ParserTools, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class ParserTools |
||
19 | { |
||
20 | /** |
||
21 | * Parses the given $objectElement, if it contains embedded data. |
||
22 | * |
||
23 | * @param array $objectElement |
||
24 | * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher |
||
25 | * |
||
26 | * @return mixed |
||
27 | */ |
||
28 | public function parseObjectElement(array $objectElement, ParsingDispatcher $parsingDispatcher) |
||
39 | |||
40 | /** |
||
41 | * Returns if the given $objectElement has embedded object data or is only |
||
42 | * a reference. |
||
43 | * |
||
44 | * @param array $objectElement |
||
45 | * |
||
46 | * @return bool |
||
47 | */ |
||
48 | public function isEmbeddedObject(array $objectElement) |
||
59 | |||
60 | /** |
||
61 | * Parses a translatable list, like names or descriptions. |
||
62 | * |
||
63 | * @param array $listElement |
||
64 | * |
||
65 | * @return array |
||
66 | */ |
||
67 | public function parseTranslatableList(array $listElement) |
||
78 | |||
79 | /** |
||
80 | * Parses a boolean from $value. |
||
81 | * |
||
82 | * @param string|bool $value |
||
83 | * |
||
84 | * @return bool |
||
85 | * @throws \RuntimeException if the value can not be transformed to a boolean |
||
86 | */ |
||
87 | public function parseBooleanValue($value) |
||
101 | |||
102 | /** |
||
103 | * Parses the content types status from $contentTypeStatus. |
||
104 | * |
||
105 | * @param string $contentTypeStatus |
||
106 | * |
||
107 | * @return int |
||
108 | */ |
||
109 | public function parseStatus($contentTypeStatus) |
||
122 | |||
123 | /** |
||
124 | * Parses the default sort field from the given $defaultSortFieldString. |
||
125 | * |
||
126 | * @param string $defaultSortFieldString |
||
127 | * |
||
128 | * @return int |
||
129 | */ |
||
130 | View Code Duplication | public function parseDefaultSortField($defaultSortFieldString) |
|
161 | |||
162 | /** |
||
163 | * Parses the default sort order from the given $defaultSortOrderString. |
||
164 | * |
||
165 | * @param string $defaultSortOrderString |
||
166 | * |
||
167 | * @return int |
||
168 | */ |
||
169 | public function parseDefaultSortOrder($defaultSortOrderString) |
||
180 | |||
181 | /** |
||
182 | * Parses the input structure to Limitation object. |
||
183 | * |
||
184 | * @param array $limitation |
||
185 | * |
||
186 | * @return \eZ\Publish\API\Repository\Values\User\Limitation |
||
187 | */ |
||
188 | View Code Duplication | public function parseLimitation(array $limitation) |
|
213 | |||
214 | /** |
||
215 | * Instantiates Limitation object based on identifier. |
||
216 | * |
||
217 | * @param string $identifier |
||
218 | * |
||
219 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
220 | * |
||
221 | * @return \eZ\Publish\API\Repository\Values\User\Limitation |
||
222 | * |
||
223 | * @todo Use dependency injection system |
||
224 | */ |
||
225 | View Code Duplication | protected function getLimitationByIdentifier($identifier) |
|
271 | } |
||
272 |
This class constant has been deprecated.