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 |
||
20 | class JsonLightODataWriter extends JsonODataV2Writer |
||
21 | { |
||
22 | /** |
||
23 | * @var JsonLightMetadataLevel |
||
24 | */ |
||
25 | protected $metadataLevel; |
||
26 | |||
27 | /** |
||
28 | * The service base uri. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $baseUri; |
||
33 | |||
34 | /** |
||
35 | * @param string $absoluteServiceUri |
||
36 | */ |
||
37 | public function __construct(JsonLightMetadataLevel $metadataLevel, $absoluteServiceUri) |
||
50 | |||
51 | /** |
||
52 | * Determines if the given writer is capable of writing the response or not. |
||
53 | * |
||
54 | * @param Version $responseVersion the OData version of the response |
||
55 | * @param string $contentType the Content Type of the response |
||
56 | * |
||
57 | * @return bool true if the writer can handle the response, false otherwise |
||
58 | */ |
||
59 | View Code Duplication | public function canHandle(Version $responseVersion, $contentType) |
|
70 | |||
71 | /** |
||
72 | * Write the given OData model in a specific response format. |
||
73 | * |
||
74 | * @param ODataURL|ODataURLCollection|ODataPropertyContent|ODataFeed|ODataEntry $model Object of requested content |
||
75 | * |
||
76 | * @return JsonLightODataWriter |
||
77 | */ |
||
78 | public function write($model) |
||
79 | { |
||
80 | $this->_writer->startObjectScope(); |
||
81 | |||
82 | if ($model instanceof ODataURL) { |
||
83 | $this->writeTopLevelMeta('url'); |
||
84 | $this->writeURL($model); |
||
85 | } elseif ($model instanceof ODataURLCollection) { |
||
86 | $this->writeTopLevelMeta('urlCollection'); |
||
87 | $this->writeURLCollection($model); |
||
88 | } elseif ($model instanceof ODataPropertyContent) { |
||
89 | $this->writeTopLevelMeta($model->properties[0]->typeName); |
||
90 | $this->writeTopLevelProperty($model->properties[0]); |
||
91 | } elseif ($model instanceof ODataFeed) { |
||
92 | $this->writeTopLevelMeta($model->title); |
||
93 | $this->writeRowCount($model->rowCount); |
||
94 | $this->_writer |
||
95 | ->writeName($this->dataArrayName) |
||
96 | ->startArrayScope(); |
||
97 | $this->writeFeed($model); |
||
98 | $this->_writer->endScope(); |
||
99 | } elseif ($model instanceof ODataEntry) { |
||
100 | $this->writeTopLevelMeta($model->resourceSetName . '/@Element'); |
||
101 | $this->writeEntry($model); |
||
102 | } |
||
103 | |||
104 | $this->_writer->endScope(); |
||
105 | |||
106 | return $this; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param ODataProperty $property |
||
111 | * |
||
112 | * @return JsonLightODataWriter |
||
113 | */ |
||
114 | protected function writeTopLevelProperty(ODataProperty $property) |
||
135 | |||
136 | /** |
||
137 | * @param string $fragment |
||
138 | */ |
||
139 | protected function writeTopLevelMeta($fragment) |
||
149 | |||
150 | protected function writePropertyMeta(ODataProperty $property) |
||
175 | |||
176 | /** |
||
177 | * @param ODataEntry $entry Entry to write metadata for |
||
178 | * |
||
179 | * @return JsonLightODataWriter |
||
180 | */ |
||
181 | protected function writeEntryMetadata(ODataEntry $entry) |
||
200 | |||
201 | /** |
||
202 | * @param ODataLink $link Link to write |
||
203 | * |
||
204 | * @return JsonLightODataWriter |
||
205 | */ |
||
206 | protected function writeLink(ODataLink $link) |
||
227 | |||
228 | View Code Duplication | protected function writeExpandedLink(ODataLink $link) |
|
240 | |||
241 | /** |
||
242 | * Writes the next page link. |
||
243 | * |
||
244 | * @param ODataLink $nextPageLinkUri Uri for next page link |
||
245 | * |
||
246 | * @return JsonLightODataWriter |
||
247 | */ |
||
248 | protected function writeNextPageLink(ODataLink $nextPageLinkUri = null) |
||
249 | { |
||
250 | return; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Begin write complex property. |
||
255 | * |
||
256 | * @param ODataProperty $property property to write |
||
257 | * |
||
258 | * @return JsonLightODataWriter |
||
259 | */ |
||
260 | protected function writeComplexProperty(ODataProperty $property) |
||
261 | { |
||
262 | $this->_writer->startObjectScope(); |
||
263 | |||
264 | $this->writeComplexPropertyMeta($property) |
||
265 | ->writeProperties($property->value); |
||
266 | |||
267 | $this->_writer->endScope(); |
||
268 | |||
269 | return $this; |
||
270 | } |
||
271 | |||
272 | protected function writeComplexPropertyMeta(ODataProperty $property) |
||
282 | |||
283 | protected function writeBagContent(ODataBagContent $bag) |
||
284 | { |
||
285 | $this->_writer->startArrayScope(); |
||
303 | } |
||
304 |
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.