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 ObjectModelSerializerBase 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 ObjectModelSerializerBase, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class ObjectModelSerializerBase |
||
25 | { |
||
26 | /** |
||
27 | * The service implementation. |
||
28 | * |
||
29 | * @var IService |
||
30 | */ |
||
31 | protected $service; |
||
32 | |||
33 | /** |
||
34 | * Request description instance describes OData request the |
||
35 | * the client has submitted and result of the request. |
||
36 | * |
||
37 | * @var RequestDescription |
||
38 | */ |
||
39 | protected $request; |
||
40 | |||
41 | /** |
||
42 | * Collection of segment names |
||
43 | * Remark: Read 'ObjectModelSerializerNotes.txt' for more |
||
44 | * details about segment. |
||
45 | * |
||
46 | * @var string[] |
||
47 | */ |
||
48 | private $_segmentNames; |
||
49 | |||
50 | /** |
||
51 | * Collection of segment ResourceSetWrapper instances |
||
52 | * Remark: Read 'ObjectModelSerializerNotes.txt' for more |
||
53 | * details about segment. |
||
54 | * |
||
55 | * @var ResourceSetWrapper[] |
||
56 | */ |
||
57 | private $_segmentResourceSetWrappers; |
||
58 | |||
59 | /** |
||
60 | * Result counts for segments |
||
61 | * Remark: Read 'ObjectModelSerializerNotes.txt' for more |
||
62 | * details about segment. |
||
63 | * |
||
64 | * @var int[] |
||
65 | */ |
||
66 | private $_segmentResultCounts; |
||
67 | |||
68 | /** |
||
69 | * Collection of complex type instances used for cycle detection. |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $complexTypeInstanceCollection; |
||
74 | |||
75 | /** |
||
76 | * Absolute service Uri. |
||
77 | * |
||
78 | * @var string |
||
79 | */ |
||
80 | protected $absoluteServiceUri; |
||
81 | |||
82 | /** |
||
83 | * Absolute service Uri with slash. |
||
84 | * |
||
85 | * @var string |
||
86 | */ |
||
87 | protected $absoluteServiceUriWithSlash; |
||
88 | |||
89 | /** |
||
90 | * @param IService $service Reference to the data service instance. |
||
91 | * @param RequestDescription $request Type instance describing the client submitted request. |
||
92 | * |
||
93 | */ |
||
94 | protected function __construct(IService $service, RequestDescription $request) |
||
105 | |||
106 | /** |
||
107 | * Builds the key for the given entity instance. |
||
108 | * Note: The generated key can be directly used in the uri, |
||
109 | * this function will perform |
||
110 | * required escaping of characters, for example: |
||
111 | * Ships(ShipName='Antonio%20Moreno%20Taquer%C3%ADa',ShipID=123), |
||
112 | * Note to method caller: Don't do urlencoding on |
||
113 | * return value of this method as it already encoded. |
||
114 | * |
||
115 | * @param mixed $entityInstance Entity instance for which key value needs to be prepared. |
||
116 | * @param ResourceType $resourceType Resource type instance containing metadata about the instance. |
||
117 | * @param string $containerName Name of the entity set that the entity instance belongs to |
||
118 | * . |
||
119 | * |
||
120 | * @return string Key for the given resource, with values encoded for use in a URI |
||
121 | * . |
||
122 | */ |
||
123 | protected function getEntryInstanceKey($entityInstance, ResourceType $resourceType, $containerName) |
||
146 | |||
147 | /** |
||
148 | * Get the value of a given property from an instance. |
||
149 | * |
||
150 | * @param mixed $entity Instance of a type which contains this property. |
||
151 | * @param ResourceType $resourceType Resource type instance containing metadata about the instance. |
||
152 | * @param ResourceProperty $resourceProperty Resource property instance containing metadata about the property whose value to be retrieved. |
||
153 | * |
||
154 | * @return mixed The value of the given property. |
||
155 | * |
||
156 | * @throws ODataException If reflection exception occurred while trying to access the property. |
||
157 | * |
||
158 | */ |
||
159 | protected function getPropertyValue($entity, ResourceType $resourceType, ResourceProperty $resourceProperty) |
||
174 | |||
175 | /** |
||
176 | * Resource set wrapper for the resource being serialized. |
||
177 | * |
||
178 | * @return ResourceSetWrapper |
||
179 | */ |
||
180 | View Code Duplication | protected function getCurrentResourceSetWrapper() |
|
189 | |||
190 | /** |
||
191 | * Whether the current resource set is root resource set. |
||
192 | * |
||
193 | * @return boolean true if the current resource set root container else |
||
194 | * false. |
||
195 | */ |
||
196 | protected function isRootResourceSet() |
||
201 | |||
202 | /** |
||
203 | * Returns the etag for the given resource. |
||
204 | * |
||
205 | * @param mixed $entryObject Resource for which etag value |
||
206 | * needs to be returned |
||
207 | * @param ResourceType $resourceType Resource type of the $entryObject |
||
208 | * |
||
209 | * @return string|null ETag value for the given resource |
||
210 | * (with values encoded for use in a URI) |
||
211 | * if there are etag properties, NULL if there is no etag property. |
||
212 | */ |
||
213 | protected function getETagForEntry($entryObject, ResourceType $resourceType) |
||
243 | |||
244 | /** |
||
245 | * Pushes a segment for the root of the tree being written out |
||
246 | * Note: Refer 'ObjectModelSerializerNotes.txt' for more details about |
||
247 | * 'Segment Stack' and this method. |
||
248 | * Note: Calls to this method should be balanced with calls to popSegment. |
||
249 | * |
||
250 | * @return bool true if the segment was pushed, false otherwise. |
||
251 | */ |
||
252 | protected function pushSegmentForRoot() |
||
258 | |||
259 | /** |
||
260 | * Pushes a segment for the current navigation property being written out. |
||
261 | * Note: Refer 'ObjectModelSerializerNotes.txt' for more details about |
||
262 | * 'Segment Stack' and this method. |
||
263 | * Note: Calls to this method should be balanced with calls to popSegment. |
||
264 | * |
||
265 | * @param ResourceProperty &$resourceProperty The current navigation property |
||
266 | * being written out. |
||
267 | * |
||
268 | * @return bool true if a segment was pushed, false otherwise |
||
269 | * |
||
270 | * @throws InvalidOperationException If this function invoked with non-navigation |
||
271 | * property instance. |
||
272 | */ |
||
273 | View Code Duplication | protected function pushSegmentForNavigationProperty(ResourceProperty &$resourceProperty) |
|
293 | |||
294 | /** |
||
295 | * Gets collection of projection nodes under the current node. |
||
296 | * |
||
297 | * @return ProjectionNode[]|ExpandedProjectionNode[]|null List of nodes |
||
298 | * describing projections for the current segment, If this method returns |
||
299 | * null it means no projections are to be applied and the entire resource |
||
300 | * for the current segment should be serialized, If it returns non-null |
||
301 | * only the properties described by the returned projection segments should |
||
302 | * be serialized. |
||
303 | */ |
||
304 | protected function getProjectionNodes() |
||
315 | |||
316 | /** |
||
317 | * Find a 'ExpandedProjectionNode' instance in the projection tree |
||
318 | * which describes the current segment. |
||
319 | * |
||
320 | * @return ExpandedProjectionNode|null |
||
321 | */ |
||
322 | View Code Duplication | protected function getCurrentExpandedProjectionNode() |
|
353 | |||
354 | /** |
||
355 | * Check whether to expand a navigation property or not. |
||
356 | * |
||
357 | * @param string $navigationPropertyName Name of naviagtion property in question. |
||
358 | * |
||
359 | * @return boolean True if the given navigation should be |
||
360 | * explanded otherwise false. |
||
361 | */ |
||
362 | protected function shouldExpandSegment($navigationPropertyName) |
||
372 | |||
373 | /** |
||
374 | * Pushes information about the segment that is going to be serialized |
||
375 | * to the 'Segment Stack'. |
||
376 | * Note: Refer 'ObjectModelSerializerNotes.txt' for more details about |
||
377 | * 'Segment Stack' and this method. |
||
378 | * Note: Calls to this method should be balanced with calls to popSegment. |
||
379 | * |
||
380 | * @param string $segmentName Name of segment to push. |
||
381 | * @param ResourceSetWrapper &$resourceSetWrapper The resource set |
||
382 | * wrapper to push. |
||
383 | * |
||
384 | * @return bool true if the segment was push, false otherwise |
||
385 | */ |
||
386 | View Code Duplication | private function _pushSegment($segmentName, ResourceSetWrapper &$resourceSetWrapper) |
|
414 | |||
415 | /** |
||
416 | * Get next page link from the given entity instance. |
||
417 | * |
||
418 | * @param mixed &$lastObject Last object serialized to be |
||
419 | * used for generating $skiptoken. |
||
420 | * @param string $absoluteUri Absolute response URI. |
||
421 | * |
||
422 | * @return URI for the link for next page. |
||
423 | */ |
||
424 | protected function getNextLinkUri(&$lastObject, $absoluteUri) |
||
443 | |||
444 | /** |
||
445 | * Builds the string corresponding to query parameters for top level results |
||
446 | * (result set identified by the resource path) to be put in next page link. |
||
447 | * |
||
448 | * @return string|null string representing the query parameters in the URI |
||
449 | * query parameter format, NULL if there |
||
450 | * is no query parameters |
||
451 | * required for the next link of top level result set. |
||
452 | */ |
||
453 | protected function getNextPageLinkQueryParametersForRootResourceSet() |
||
488 | |||
489 | /** |
||
490 | * Builds the string corresponding to query parameters for current expanded |
||
491 | * results to be put in next page link. |
||
492 | * |
||
493 | * @return string|null string representing the $select and $expand parameters |
||
494 | * in the URI query parameter format, NULL if there is no |
||
495 | * query parameters ($expand and/select) required for the |
||
496 | * next link of expanded result set. |
||
497 | */ |
||
498 | protected function getNextPageLinkQueryParametersForExpandedResourceSet() |
||
540 | |||
541 | /** |
||
542 | * Wheter next link is needed for the current resource set (feed) |
||
543 | * being serialized. |
||
544 | * |
||
545 | * @param int $resultSetCount Number of entries in the current |
||
546 | * resource set. |
||
547 | * |
||
548 | * @return boolean true if the feed must have a next page link |
||
549 | */ |
||
550 | protected function needNextPageLink($resultSetCount) |
||
567 | |||
568 | /** |
||
569 | * Pops segment information from the 'Segment Stack' |
||
570 | * Note: Refer 'ObjectModelSerializerNotes.txt' for more details about |
||
571 | * 'Segment Stack' and this method. |
||
572 | * Note: Calls to this method should be balanced with previous |
||
573 | * calls to _pushSegment. |
||
574 | * |
||
575 | * @param boolean $needPop Is a pop required. Only true if last |
||
576 | * push was successful. |
||
577 | * |
||
578 | * @return void |
||
579 | * |
||
580 | * @throws InvalidOperationException If found un-balanced call with _pushSegment |
||
581 | */ |
||
582 | View Code Duplication | protected function popSegment($needPop) |
|
594 | |||
595 | /** |
||
596 | * Recursive metod to build $expand and $select paths for a specified node. |
||
597 | * |
||
598 | * @param string[] &$parentPathSegments Array of path |
||
599 | * segments which leads |
||
600 | * up to (including) |
||
601 | * the segment |
||
602 | * represented by |
||
603 | * $expandedProjectionNode. |
||
604 | * @param string[] &$selectionPaths The string which |
||
605 | * holds projection |
||
606 | * path segment |
||
607 | * seperated by comma, |
||
608 | * On return this argument |
||
609 | * will be updated with |
||
610 | * the selection path |
||
611 | * segments under |
||
612 | * this node. |
||
613 | * @param string[] &$expansionPaths The string which holds |
||
614 | * expansion path segment |
||
615 | * seperated by comma. |
||
616 | * On return this argument |
||
617 | * will be updated with |
||
618 | * the expand path |
||
619 | * segments under |
||
620 | * this node. |
||
621 | * @param ExpandedProjectionNode &$expandedProjectionNode The expanded node for |
||
622 | * which expansion |
||
623 | * and selection path |
||
624 | * to be build. |
||
625 | * @param boolean &$foundSelections On return, this |
||
626 | * argument will hold |
||
627 | * true if any selection |
||
628 | * defined under this node |
||
629 | * false otherwise. |
||
630 | * @param boolean &$foundExpansions On return, this |
||
631 | * argument will hold |
||
632 | * true if any expansion |
||
633 | * defined under this node |
||
634 | * false otherwise. |
||
635 | * |
||
636 | * @return void |
||
637 | */ |
||
638 | private function _buildSelectionAndExpansionPathsForNode(&$parentPathSegments, |
||
700 | |||
701 | /** |
||
702 | * Append the given path to $expand or $select path list. |
||
703 | * |
||
704 | * @param string &$path The $expand or $select path list to which to append the given path. |
||
705 | * @param string[] &$parentPathSegments The list of path up to the $segmentToAppend. |
||
706 | * @param string $segmentToAppend The last segment of the path. |
||
707 | * |
||
708 | * @return void |
||
709 | */ |
||
710 | private function _appendSelectionOrExpandPath(&$path, &$parentPathSegments, $segmentToAppend) |
||
722 | |||
723 | /** |
||
724 | * Assert that the given condition is true. |
||
725 | * |
||
726 | * @param boolean $condition Condition to be asserted. |
||
727 | * @param string $conditionAsString String containing message incase |
||
728 | * if assertion fails. |
||
729 | * |
||
730 | * @throws InvalidOperationException Incase if assertion failes. |
||
731 | * |
||
732 | * @return void |
||
733 | */ |
||
734 | protected function assert($condition, $conditionAsString) |
||
740 | } |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: