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 RelationshipObject 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 RelationshipObject, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class RelationshipObject implements ObjectInterface, PaginableInterface, RecursiveResourceContainerInterface { |
||
|
|||
18 | use AtMemberManager, LinksManager; |
||
19 | |||
20 | const TO_ONE = 'one'; |
||
21 | const TO_MANY = 'many'; |
||
22 | |||
23 | /** @var MetaObject */ |
||
24 | protected $meta; |
||
25 | /** @var ResourceInterface */ |
||
26 | protected $resource; |
||
27 | /** @var string one of the RelationshipObject::TO_* constants */ |
||
28 | protected $type; |
||
29 | /** @var ResourceInterface[] */ |
||
30 | protected $resources = []; |
||
31 | |||
32 | /** |
||
33 | * @param string $type one of the RelationshipObject::TO_* constants |
||
34 | * |
||
35 | * @throws InputException if $type is unknown |
||
36 | */ |
||
37 | public function __construct($type) { |
||
44 | |||
45 | /** |
||
46 | * human api |
||
47 | */ |
||
48 | |||
49 | /** |
||
50 | * create a RelationshipObject from mixed input |
||
51 | * |
||
52 | * @param mixed $relation ResourceInterface | ResourceInterface[] | CollectionDocument |
||
53 | * @param array $links optional |
||
54 | * @param array $meta optional |
||
55 | * @return RelationshipObject |
||
56 | * |
||
57 | * @throws InputException if $relation is not one of the supported formats |
||
58 | */ |
||
59 | public static function fromAnything($relation, array $links=[], array $meta=[]) { |
||
79 | |||
80 | /** |
||
81 | * @param ResourceInterface $resource |
||
82 | * @param array $links optional |
||
83 | * @param array $meta optional |
||
84 | * @param string $type optional, one of the RelationshipObject::TO_* constants, defaults to RelationshipObject::TO_ONE |
||
85 | * @return RelationshipObject |
||
86 | */ |
||
87 | public static function fromResource(ResourceInterface $resource, array $links=[], array $meta=[], $type=RelationshipObject::TO_ONE) { |
||
106 | |||
107 | /** |
||
108 | * @param CollectionDocument $collectionDocument |
||
109 | * @param array $links optional |
||
110 | * @param array $meta optional |
||
111 | * @return RelationshipObject |
||
112 | */ |
||
113 | public static function fromCollectionDocument(CollectionDocument $collectionDocument, array $links=[], array $meta=[]) { |
||
129 | |||
130 | /** |
||
131 | * @param string $href |
||
132 | * @param array $meta optional, if given a LinkObject is added, otherwise a link string is added |
||
133 | */ |
||
134 | public function setSelfLink($href, array $meta=[]) { |
||
137 | |||
138 | /** |
||
139 | * @param string $href |
||
140 | * @param array $meta optional, if given a LinkObject is added, otherwise a link string is added |
||
141 | */ |
||
142 | public function setRelatedLink($href, array $meta=[]) { |
||
145 | |||
146 | /** |
||
147 | * @inheritDoc |
||
148 | * |
||
149 | * @throws InputException if used on a to-one relationship |
||
150 | */ |
||
151 | public function setPaginationLinks($previousHref=null, $nextHref=null, $firstHref=null, $lastHref=null) { |
||
169 | |||
170 | /** |
||
171 | * @param string $key |
||
172 | * @param mixed $value |
||
173 | */ |
||
174 | public function addMeta($key, $value) { |
||
181 | |||
182 | /** |
||
183 | * spec api |
||
184 | */ |
||
185 | |||
186 | /** |
||
187 | * set the resource on a to-one relationship |
||
188 | * |
||
189 | * @param ResourceInterface $resource |
||
190 | * |
||
191 | * @throws InputException if used on a to-many relationship, use {@see ->addResource()} instead |
||
192 | */ |
||
193 | public function setResource(ResourceInterface $resource) { |
||
200 | |||
201 | /** |
||
202 | * add a resource to a to-many relationship |
||
203 | * |
||
204 | * @param ResourceInterface $resource |
||
205 | * |
||
206 | * @throws InputException if used on a to-one relationship, use {@see ->setResource()} instead |
||
207 | */ |
||
208 | public function addResource(ResourceInterface $resource) { |
||
215 | |||
216 | /** |
||
217 | * @param MetaObject $metaObject |
||
218 | */ |
||
219 | public function setMetaObject(MetaObject $metaObject) { |
||
222 | |||
223 | /** |
||
224 | * internal api |
||
225 | */ |
||
226 | |||
227 | /** |
||
228 | * whether or not the $otherResource is (one of) the resource(s) inside the relationship |
||
229 | * |
||
230 | * @internal |
||
231 | * |
||
232 | * @param ResourceInterface $otherResource |
||
233 | * @return boolean |
||
234 | */ |
||
235 | public function hasResource(ResourceInterface $otherResource) { |
||
252 | |||
253 | /** |
||
254 | * ObjectInterface |
||
255 | */ |
||
256 | |||
257 | /** |
||
258 | * @inheritDoc |
||
259 | */ |
||
260 | public function isEmpty() { |
||
279 | |||
280 | /** |
||
281 | * @inheritDoc |
||
282 | */ |
||
283 | public function toArray() { |
||
307 | |||
308 | /** |
||
309 | * RecursiveResourceContainerInterface |
||
310 | */ |
||
311 | |||
312 | /** |
||
313 | * @inheritDoc |
||
314 | */ |
||
315 | public function getNestedContainedResourceObjects() { |
||
341 | } |
||
342 |