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 JsonODataV1Writer 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 JsonODataV1Writer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class JsonODataV1Writer implements IODataWriter |
||
24 | { |
||
25 | /** |
||
26 | * Json output writer. |
||
27 | */ |
||
28 | protected $_writer; |
||
29 | |||
30 | protected $urlKey = ODataConstants::JSON_URI_STRING; |
||
31 | |||
32 | /** |
||
33 | * Constructs and initializes the Json output writer. |
||
34 | */ |
||
35 | public function __construct() |
||
39 | |||
40 | /** |
||
41 | * Determines if the given writer is capable of writing the response or not. |
||
42 | * |
||
43 | * @param Version $responseVersion the OData version of the response |
||
44 | * @param string $contentType the Content Type of the response |
||
45 | * |
||
46 | * @return bool true if the writer can handle the response, false otherwise |
||
47 | */ |
||
48 | View Code Duplication | public function canHandle(Version $responseVersion, $contentType) |
|
58 | |||
59 | /** |
||
60 | * Write the given OData model in a specific response format. |
||
61 | * |
||
62 | * @param ODataURL|ODataURLCollection|ODataPropertyContent|ODataFeed|ODataEntry $model Object of requested content |
||
63 | * |
||
64 | * @return JsonODataV1Writer |
||
65 | */ |
||
66 | View Code Duplication | public function write($model) |
|
95 | |||
96 | /** |
||
97 | * @param ODataURL $url the url to write |
||
98 | * |
||
99 | * @return JsonODataV1Writer |
||
100 | */ |
||
101 | public function writeUrl(ODataURL $url) |
||
109 | |||
110 | /** |
||
111 | * begin write OData links. |
||
112 | * |
||
113 | * @param ODataURLCollection $urls url collection to write |
||
114 | * |
||
115 | * @return JsonODataV1Writer |
||
116 | */ |
||
117 | public function writeUrlCollection(ODataURLCollection $urls) |
||
127 | |||
128 | /** |
||
129 | * Start writing a feed. |
||
130 | * |
||
131 | * @param ODataFeed $feed Feed to write |
||
132 | * |
||
133 | * @return JsonODataV1Writer |
||
134 | */ |
||
135 | protected function writeFeed(ODataFeed $feed) |
||
145 | |||
146 | /** |
||
147 | * @param ODataEntry $entry Entry to write |
||
148 | * |
||
149 | * @return JsonODataV1Writer |
||
150 | */ |
||
151 | protected function writeEntry(ODataEntry $entry) |
||
162 | |||
163 | /** |
||
164 | * Write metadata information for the entry. |
||
165 | * |
||
166 | * @param ODataEntry $entry Entry to write metadata for |
||
167 | * |
||
168 | * @return JsonODataV1Writer |
||
169 | */ |
||
170 | protected function writeEntryMetadata(ODataEntry $entry) |
||
249 | |||
250 | /** |
||
251 | * @param ODataLink $link Link to write |
||
252 | * |
||
253 | * @return JsonODataV1Writer |
||
254 | */ |
||
255 | protected function writeLink(ODataLink $link) |
||
280 | |||
281 | View Code Duplication | protected function writeExpandedLink(ODataLink $link) |
|
293 | |||
294 | /** |
||
295 | * Write the given collection of properties. |
||
296 | * (properties of an entity or complex type). |
||
297 | * |
||
298 | * @param ODataPropertyContent $properties Collection of properties |
||
299 | * |
||
300 | * @return JsonODataV1Writer |
||
301 | */ |
||
302 | protected function writeProperties(ODataPropertyContent $properties) |
||
321 | |||
322 | protected function writePropertyMeta(ODataProperty $property) |
||
326 | |||
327 | /** |
||
328 | * Begin write complex property. |
||
329 | * |
||
330 | * @param ODataProperty $property property to write |
||
331 | * |
||
332 | * @return JsonODataV1Writer |
||
333 | */ |
||
334 | protected function writeComplexProperty(ODataProperty $property) |
||
353 | |||
354 | /** |
||
355 | * Begin an item in a collection. |
||
356 | * |
||
357 | * @param ODataBagContent $bag bag property to write |
||
358 | * |
||
359 | * @return JsonODataV1Writer |
||
360 | */ |
||
361 | protected function writeBagContent(ODataBagContent $bag) |
||
392 | |||
393 | /** |
||
394 | * serialize exception. |
||
395 | * |
||
396 | * @param ODataException $exception Exception to serialize |
||
397 | * @param bool $serializeInnerException if set to true |
||
398 | * |
||
399 | * serialize the inner exception if $exception is an ODataException |
||
400 | * |
||
401 | * @return string |
||
402 | */ |
||
403 | public static function serializeException(ODataException $exception, $serializeInnerException) |
||
434 | |||
435 | /** |
||
436 | * Get the Json final output. |
||
437 | * |
||
438 | * @return string |
||
439 | */ |
||
440 | public function getOutput() |
||
444 | |||
445 | /** |
||
446 | * @param ProvidersWrapper $providers |
||
447 | * |
||
448 | * @return IODataWriter |
||
449 | */ |
||
450 | public function writeServiceDocument(ProvidersWrapper $providers) |
||
476 | } |
||
477 |
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.