Complex classes like ObjectProxyTrait 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 ObjectProxyTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
56 | trait ObjectProxyTrait |
||
57 | { |
||
58 | /** |
||
59 | * Use traits |
||
60 | */ |
||
61 | use IterableProxyTrait; |
||
62 | /** |
||
63 | * Object |
||
64 | * |
||
65 | * @var ObjectInterface |
||
66 | */ |
||
67 | protected $object = null; |
||
68 | |||
69 | /** |
||
70 | * Return the object repository locator |
||
71 | * |
||
72 | * @return RepositoryLocatorInterface Object repository locator |
||
73 | */ |
||
74 | public function getRepositoryLocator() |
||
83 | |||
84 | /** |
||
85 | * Return the object property data |
||
86 | * |
||
87 | * @param bool $serialize Serialize property objects |
||
88 | * @return array Object property data |
||
89 | */ |
||
90 | public function getPropertyData($serialize = true) |
||
94 | |||
95 | /** |
||
96 | * Return the enclosed remote object |
||
97 | * |
||
98 | * @return ObjectInterface Remote object |
||
99 | */ |
||
100 | 1 | protected function object() |
|
110 | |||
111 | /** |
||
112 | * Return the object payload |
||
113 | * |
||
114 | * @return string Object payload |
||
115 | */ |
||
116 | public function getPayload() |
||
120 | |||
121 | /** |
||
122 | * Set the payload |
||
123 | * |
||
124 | * @param string $payload Payload |
||
125 | * @return ObjectInterface Self reference |
||
126 | */ |
||
127 | public function setPayload($payload) |
||
131 | |||
132 | /** |
||
133 | * Return the object ID |
||
134 | * |
||
135 | * @return Id Object ID |
||
136 | */ |
||
137 | public function getId() |
||
141 | |||
142 | /** |
||
143 | * Return the object type |
||
144 | * |
||
145 | * @return Type Object type |
||
146 | */ |
||
147 | public function getObjectType() |
||
151 | |||
152 | /** |
||
153 | * Return the object revision |
||
154 | * |
||
155 | * @return Revision Object revision |
||
156 | */ |
||
157 | public function getRevision() |
||
161 | |||
162 | /** |
||
163 | * Return the latitude |
||
164 | * |
||
165 | * @return float Latitude |
||
166 | */ |
||
167 | public function getLatitude() |
||
171 | |||
172 | /** |
||
173 | * Set the latitude |
||
174 | * |
||
175 | * @param float $latitude Latitude |
||
176 | * @return ObjectInterface Self reference |
||
177 | */ |
||
178 | public function setLatitude($latitude) |
||
182 | |||
183 | /** |
||
184 | * Return the longitude |
||
185 | * |
||
186 | * @return float Longitude |
||
187 | */ |
||
188 | public function getLongitude() |
||
192 | |||
193 | /** |
||
194 | * Set the longitude |
||
195 | * |
||
196 | * @param float $longitude Longitude |
||
197 | * @return ObjectInterface Self reference |
||
198 | */ |
||
199 | public function setLongitude($longitude) |
||
203 | |||
204 | /** |
||
205 | * Return the elevation |
||
206 | * |
||
207 | * @return float Elevation |
||
208 | */ |
||
209 | public function getElevation() |
||
213 | |||
214 | /** |
||
215 | * Set the elevation |
||
216 | * |
||
217 | * @param float $elevation |
||
218 | * @return ObjectInterface Self reference |
||
219 | */ |
||
220 | public function setElevation($elevation) |
||
224 | |||
225 | |||
226 | /** |
||
227 | * Return the object draft mode |
||
228 | * |
||
229 | * @return boolean Object draft mode |
||
230 | */ |
||
231 | public function isDraft() |
||
235 | |||
236 | /** |
||
237 | * Return whether the object is in modified state |
||
238 | * |
||
239 | * @return boolean Modified state |
||
240 | */ |
||
241 | public function hasBeenModified() |
||
245 | |||
246 | /** |
||
247 | * Return whether the object is in mutated state |
||
248 | * |
||
249 | * @return boolean Mutated state |
||
250 | */ |
||
251 | public function hasBeenMutated() |
||
255 | |||
256 | /** |
||
257 | * Return the creation date & time |
||
258 | * |
||
259 | * @return \DateTimeInterface Creation date & time |
||
260 | */ |
||
261 | public function getCreated() |
||
265 | |||
266 | /** |
||
267 | * Return the deletion date & time |
||
268 | * |
||
269 | * @return \DateTimeInterface Deletion date & time |
||
270 | */ |
||
271 | public function getDeleted() |
||
275 | |||
276 | /** |
||
277 | * Return the modification date & time |
||
278 | * |
||
279 | * @return \DateTimeInterface Modification date & time |
||
280 | */ |
||
281 | public function getModified() |
||
285 | |||
286 | /** |
||
287 | * Return the publication date & time |
||
288 | * |
||
289 | * @return \DateTimeInterface Publication date & time |
||
290 | */ |
||
291 | public function getPublished() |
||
295 | |||
296 | /** |
||
297 | * Return the object title |
||
298 | * |
||
299 | * @return string Object title |
||
300 | */ |
||
301 | 1 | public function getTitle() |
|
305 | |||
306 | /** |
||
307 | * Set the title |
||
308 | * |
||
309 | * @param string $title Title |
||
310 | * @return ObjectInterface Self reference |
||
311 | */ |
||
312 | public function setTitle($title) |
||
316 | |||
317 | /** |
||
318 | * Return the object slug |
||
319 | * |
||
320 | * @return string Object slug |
||
321 | */ |
||
322 | public function getSlug() |
||
326 | |||
327 | /** |
||
328 | * Set the slug |
||
329 | * |
||
330 | * @param string $slug Slug |
||
331 | * @return ObjectInterface Self reference |
||
332 | */ |
||
333 | public function setSlug($slug) |
||
337 | |||
338 | |||
339 | /** |
||
340 | * Return the object description |
||
341 | * |
||
342 | * @return string Object description |
||
343 | */ |
||
344 | public function getDescription() |
||
348 | |||
349 | /** |
||
350 | * Set the description |
||
351 | * |
||
352 | * @param string $description Description |
||
353 | * @return ObjectInterface Self reference |
||
354 | */ |
||
355 | public function setDescription($description) |
||
359 | |||
360 | /** |
||
361 | * Return the object abstract |
||
362 | * |
||
363 | * @return string Object abstract |
||
364 | */ |
||
365 | public function getAbstract() |
||
369 | |||
370 | /** |
||
371 | * Set the abstract |
||
372 | * |
||
373 | * @param string $abstract Abstract |
||
374 | * @return ObjectInterface Self reference |
||
375 | */ |
||
376 | public function setAbstract($abstract) |
||
380 | |||
381 | /** |
||
382 | * Return all object keywords |
||
383 | * |
||
384 | * @return array Object keywords |
||
385 | */ |
||
386 | public function getKeywords() |
||
390 | |||
391 | /** |
||
392 | * Set the keywords |
||
393 | * |
||
394 | * @param array $keywords Keywords |
||
395 | * @return ObjectInterface Self reference |
||
396 | */ |
||
397 | public function setKeywords(array $keywords) |
||
401 | |||
402 | /** |
||
403 | * Return the license |
||
404 | * |
||
405 | * @return string License |
||
406 | */ |
||
407 | public function getLicense() |
||
411 | |||
412 | /** |
||
413 | * Set the license |
||
414 | * |
||
415 | * @param string $license License |
||
416 | * @return ObjectInterface Self reference |
||
417 | */ |
||
418 | public function setLicense($license) |
||
422 | |||
423 | /** |
||
424 | * Return the language |
||
425 | * |
||
426 | * @return string Language |
||
427 | */ |
||
428 | public function getLanguage() |
||
432 | |||
433 | /** |
||
434 | * Return the privacy |
||
435 | * |
||
436 | * @return string Privacy |
||
437 | */ |
||
438 | public function getPrivacy() |
||
442 | |||
443 | /** |
||
444 | * Set the privacy |
||
445 | * |
||
446 | * @param string $privacy Privacy |
||
447 | * @return ObjectInterface Self reference |
||
448 | */ |
||
449 | public function setPrivacy($privacy) |
||
453 | |||
454 | /** |
||
455 | * Return all object categories |
||
456 | * |
||
457 | * @return array Object categories |
||
458 | */ |
||
459 | public function getCategories() |
||
463 | |||
464 | /** |
||
465 | * Set the categories |
||
466 | * |
||
467 | * @param array $categories Categories |
||
468 | * @return ObjectInterface Self reference |
||
469 | */ |
||
470 | public function setCategories(array $categories) |
||
474 | |||
475 | /** |
||
476 | * Get a domain property value |
||
477 | * |
||
478 | * Multi-level properties might be traversed by property name locators separated with colons (":"). |
||
479 | * |
||
480 | * @param string $property Property name |
||
481 | * @return mixed Property value |
||
482 | */ |
||
483 | public function getDomain($property) |
||
487 | |||
488 | /** |
||
489 | * Set a domain property value |
||
490 | * |
||
491 | * @param string $property Property name |
||
492 | * @param mixed $value Property value |
||
493 | * @return ObjectInterface Self reference |
||
494 | */ |
||
495 | public function setDomain($property, $value) |
||
499 | |||
500 | /** |
||
501 | * Get a processing instruction |
||
502 | * |
||
503 | * @param string $procInst Processing instruction name |
||
504 | * @return mixed Processing instruction |
||
505 | */ |
||
506 | public function getProcessingInstruction($procInst) |
||
510 | |||
511 | /** |
||
512 | * Set a processing instruction |
||
513 | * |
||
514 | * @param string $procInst Processing instruction name |
||
515 | * @param mixed $value Processing instruction |
||
516 | * @return ObjectInterface Self reference |
||
517 | */ |
||
518 | public function setProcessingInstruction($procInst, $value) |
||
522 | |||
523 | /** |
||
524 | * Return the canonical object URL |
||
525 | * |
||
526 | * @return string |
||
527 | */ |
||
528 | public function getCanonicalUrl() |
||
532 | |||
533 | /** |
||
534 | * Return the absolute object URL |
||
535 | * |
||
536 | * @return string |
||
537 | */ |
||
538 | public function getAbsoluteUrl() |
||
542 | |||
543 | /** |
||
544 | * Generic caller |
||
545 | * |
||
546 | * @param string $name Method name |
||
547 | * @param array $arguments Method arguments |
||
548 | */ |
||
549 | public function __call($name, $arguments) |
||
561 | |||
562 | /** |
||
563 | * Use a specific object revision |
||
564 | * |
||
565 | * @param Revision $revision Revision to be used |
||
566 | * @return ObjectInterface Object |
||
567 | */ |
||
568 | public function useRevision(Revision $revision) |
||
572 | |||
573 | /** |
||
574 | * Persist the current object revision |
||
575 | * |
||
576 | * @return ObjectInterface Object |
||
577 | */ |
||
578 | public function persist() |
||
582 | |||
583 | /** |
||
584 | * Return whether the object is in published state |
||
585 | * |
||
586 | * @return boolean Published state |
||
587 | */ |
||
588 | public function isPublished() |
||
592 | |||
593 | /** |
||
594 | * Return whether the object has just been published |
||
595 | * |
||
596 | * @return boolean Object has just been published |
||
597 | */ |
||
598 | public function hasBeenPublished() |
||
602 | |||
603 | /** |
||
604 | * Return whether the object has been deleted |
||
605 | * |
||
606 | * @return boolean Object is deleted |
||
607 | */ |
||
608 | public function isDeleted() |
||
612 | |||
613 | /** |
||
614 | * Return whether the object has just been deleted |
||
615 | * |
||
616 | * @return boolean Object has just been deleted |
||
617 | */ |
||
618 | public function hasBeenDeleted() |
||
622 | |||
623 | /** |
||
624 | * Return whether the object has just been undeleted |
||
625 | * |
||
626 | * @return boolean Object has just been undeleted |
||
627 | */ |
||
628 | public function hasBeenUndeleted() |
||
632 | |||
633 | |||
634 | /** |
||
635 | * Publish the current object revision |
||
636 | * |
||
637 | * @return ObjectInterface Object |
||
638 | */ |
||
639 | public function publish() |
||
643 | |||
644 | /** |
||
645 | * Delete the object and all its revisions |
||
646 | * |
||
647 | * @return ObjectInterface Object |
||
648 | */ |
||
649 | public function delete() |
||
653 | |||
654 | /** |
||
655 | * Undelete the object and all its revisions |
||
656 | * |
||
657 | * @return ObjectInterface Object |
||
658 | */ |
||
659 | public function undelete() |
||
663 | |||
664 | /** |
||
665 | * Add an object relation |
||
666 | * |
||
667 | * @param string|RelationInterface $relation Serialized or instantiated object relation |
||
668 | * @param string|null $relationType Relation type |
||
669 | * @return ObjectInterface |
||
670 | */ |
||
671 | public function addRelation($relation, $relationType = null) |
||
675 | |||
676 | /** |
||
677 | * Delete an object relation |
||
678 | * |
||
679 | * @param RelationInterface $relation Object relation |
||
680 | * @return ObjectInterface |
||
681 | */ |
||
682 | public function deleteRelation(RelationInterface $relation) |
||
686 | |||
687 | /** |
||
688 | * Get all relations (optional: Of a particular type) |
||
689 | * |
||
690 | * @param string|null $relationType Optional: Relation type |
||
691 | * @return array Object relations |
||
692 | */ |
||
693 | public function getRelations($relationType = null) |
||
697 | |||
698 | /** |
||
699 | * Find and return particular relations |
||
700 | * |
||
701 | * @param array $criteria Relation criteria |
||
702 | * @return RelationInterface[] Relations |
||
703 | */ |
||
704 | public function findRelations(array $criteria) |
||
708 | |||
709 | /** |
||
710 | * Return the URL |
||
711 | * |
||
712 | * @return RepositoryLocatorInterface|ApparatUrl URL |
||
713 | */ |
||
714 | abstract public function getUrl(); |
||
715 | } |
||
716 |
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: