Complex classes like IronicSerialiser 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 IronicSerialiser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class IronicSerialiser implements IObjectSerialiser |
||
31 | { |
||
32 | /** |
||
33 | * The service implementation. |
||
34 | * |
||
35 | * @var IService |
||
36 | */ |
||
37 | protected $service; |
||
38 | |||
39 | /** |
||
40 | * Request description instance describes OData request the |
||
41 | * the client has submitted and result of the request. |
||
42 | * |
||
43 | * @var RequestDescription |
||
44 | */ |
||
45 | protected $request; |
||
46 | |||
47 | /** |
||
48 | * Collection of complex type instances used for cycle detection. |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $complexTypeInstanceCollection; |
||
53 | |||
54 | /** |
||
55 | * Absolute service Uri. |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $absoluteServiceUri; |
||
60 | |||
61 | /** |
||
62 | * Absolute service Uri with slash. |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $absoluteServiceUriWithSlash; |
||
67 | |||
68 | /** |
||
69 | * Holds reference to segment stack being processed. |
||
70 | * |
||
71 | * @var SegmentStack |
||
72 | */ |
||
73 | protected $stack; |
||
74 | |||
75 | /** |
||
76 | * Lightweight stack tracking for recursive descent fill |
||
77 | */ |
||
78 | private $lightStack = []; |
||
79 | |||
80 | /** |
||
81 | * @param IService $service Reference to the data service instance |
||
82 | * @param RequestDescription $request Type instance describing the client submitted request |
||
83 | */ |
||
84 | public function __construct(IService $service, RequestDescription $request = null) |
||
93 | |||
94 | /** |
||
95 | * Write a top level entry resource. |
||
96 | * |
||
97 | * @param mixed $entryObject Reference to the entry object to be written |
||
98 | * |
||
99 | * @return ODataEntry |
||
100 | */ |
||
101 | public function writeTopLevelElement($entryObject) |
||
206 | |||
207 | /** |
||
208 | * Write top level feed element. |
||
209 | * |
||
210 | * @param array &$entryObjects Array of entry resources to be written |
||
211 | * |
||
212 | * @return ODataFeed |
||
213 | */ |
||
214 | public function writeTopLevelElements(&$entryObjects) |
||
245 | |||
246 | /** |
||
247 | * Write top level url element. |
||
248 | * |
||
249 | * @param mixed $entryObject The entry resource whose url to be written |
||
250 | * |
||
251 | * @return ODataURL |
||
252 | */ |
||
253 | public function writeUrlElement($entryObject) |
||
257 | |||
258 | /** |
||
259 | * Write top level url collection. |
||
260 | * |
||
261 | * @param array $entryObjects Array of entry resources |
||
262 | * whose url to be written |
||
263 | * |
||
264 | * @return ODataURLCollection |
||
265 | */ |
||
266 | public function writeUrlElements($entryObjects) |
||
270 | |||
271 | /** |
||
272 | * Write top level complex resource. |
||
273 | * |
||
274 | * @param mixed &$complexValue The complex object to be |
||
275 | * written |
||
276 | * @param string $propertyName The name of the |
||
277 | * complex property |
||
278 | * @param ResourceType &$resourceType Describes the type of |
||
279 | * complex object |
||
280 | * |
||
281 | * @return ODataPropertyContent |
||
282 | */ |
||
283 | public function writeTopLevelComplexObject(&$complexValue, $propertyName, ResourceType &$resourceType) |
||
287 | |||
288 | /** |
||
289 | * Write top level bag resource. |
||
290 | * |
||
291 | * @param mixed &$BagValue The bag object to be |
||
292 | * written |
||
293 | * @param string $propertyName The name of the |
||
294 | * bag property |
||
295 | * @param ResourceType &$resourceType Describes the type of |
||
296 | * bag object |
||
297 | * |
||
298 | * @return ODataPropertyContent |
||
299 | */ |
||
300 | public function writeTopLevelBagObject(&$BagValue, $propertyName, ResourceType &$resourceType) |
||
304 | |||
305 | /** |
||
306 | * Write top level primitive value. |
||
307 | * |
||
308 | * @param mixed &$primitiveValue The primitve value to be |
||
309 | * written |
||
310 | * @param ResourceProperty &$resourceProperty Resource property |
||
311 | * describing the |
||
312 | * primitive property |
||
313 | * to be written |
||
314 | * |
||
315 | * @return ODataPropertyContent |
||
316 | */ |
||
317 | public function writeTopLevelPrimitive(&$primitiveValue, ResourceProperty &$resourceProperty = null) |
||
321 | |||
322 | /** |
||
323 | * Gets reference to the request submitted by client. |
||
324 | * |
||
325 | * @return RequestDescription |
||
326 | */ |
||
327 | public function getRequest() |
||
333 | |||
334 | /** |
||
335 | * Sets reference to the request submitted by client. |
||
336 | * |
||
337 | * @param RequestDescription $request |
||
338 | */ |
||
339 | public function setRequest(RequestDescription $request) |
||
344 | |||
345 | /** |
||
346 | * Gets the data service instance. |
||
347 | * |
||
348 | * @return IService |
||
349 | */ |
||
350 | public function getService() |
||
354 | |||
355 | /** |
||
356 | * Gets the segment stack instance. |
||
357 | * |
||
358 | * @return SegmentStack |
||
359 | */ |
||
360 | public function getStack() |
||
364 | |||
365 | protected function getEntryInstanceKey($entityInstance, ResourceType $resourceType, $containerName) |
||
393 | |||
394 | /** |
||
395 | * @param $entryObject |
||
396 | * @param $type |
||
397 | * @param $relativeUri |
||
398 | * @param $resourceType |
||
399 | * @return array |
||
400 | */ |
||
401 | protected function writeMediaData($entryObject, $type, $relativeUri, ResourceType $resourceType) |
||
439 | |||
440 | /** |
||
441 | * Gets collection of projection nodes under the current node. |
||
442 | * |
||
443 | * @return ProjectionNode[]|ExpandedProjectionNode[]|null List of nodes |
||
444 | * describing projections for the current segment, If this method returns |
||
445 | * null it means no projections are to be applied and the entire resource |
||
446 | * for the current segment should be serialized, If it returns non-null |
||
447 | * only the properties described by the returned projection segments should |
||
448 | * be serialized |
||
449 | */ |
||
450 | protected function getProjectionNodes() |
||
459 | |||
460 | /** |
||
461 | * Find a 'ExpandedProjectionNode' instance in the projection tree |
||
462 | * which describes the current segment. |
||
463 | * |
||
464 | * @return ExpandedProjectionNode|null |
||
465 | */ |
||
466 | protected function getCurrentExpandedProjectionNode() |
||
495 | |||
496 | /** |
||
497 | * Check whether to expand a navigation property or not. |
||
498 | * |
||
499 | * @param string $navigationPropertyName Name of naviagtion property in question |
||
500 | * |
||
501 | * @return bool True if the given navigation should be |
||
502 | * explanded otherwise false |
||
503 | */ |
||
504 | protected function shouldExpandSegment($navigationPropertyName) |
||
516 | } |
||
517 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.