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 |
||
24 | class ResourceDocument extends DataDocument implements ResourceInterface { |
||
25 | /** @var ResourceIdentifierObject|ResourceObject */ |
||
26 | protected $resource; |
||
27 | /** @var array */ |
||
28 | protected static $defaults = [ |
||
29 | /** |
||
30 | * add resources inside relationships to /included when adding resources to the collection |
||
31 | */ |
||
32 | 'includeContainedResources' => true, |
||
33 | ]; |
||
34 | |||
35 | /** |
||
36 | * @note $type and $id are optional to pass during construction |
||
37 | * however they are required for a valid ResourceDocument |
||
38 | * so use ->setPrimaryResource() if not passing them during construction |
||
39 | * |
||
40 | * @param string $type optional |
||
41 | * @param string|int $id optional |
||
42 | */ |
||
43 | public function __construct($type=null, $id=null) { |
||
48 | |||
49 | /** |
||
50 | * human api |
||
51 | */ |
||
52 | |||
53 | /** |
||
54 | * @param array $attributes |
||
55 | * @param string $type optional |
||
56 | * @param string|int $id optional |
||
57 | * @param array $options optional {@see ResourceDocument::$defaults} {@see ResourceObject::$defaults} |
||
58 | * @return ResourceDocument |
||
59 | */ |
||
60 | public static function fromArray(array $attributes, $type=null, $id=null, array $options=[]) { |
||
66 | |||
67 | /** |
||
68 | * @param object $attributes |
||
69 | * @param string $type optional |
||
70 | * @param string|int $id optional |
||
71 | * @param array $options optional {@see ResourceDocument::$defaults} |
||
72 | * @return ResourceDocument |
||
73 | */ |
||
74 | public static function fromObject($attributes, $type=null, $id=null, array $options=[]) { |
||
79 | |||
80 | /** |
||
81 | * add key-value pairs to the resource's attributes |
||
82 | * |
||
83 | * @param string $key |
||
84 | * @param mixed $value objects will be converted using `get_object_vars()` |
||
85 | * @param array $options optional {@see ResourceDocument::$defaults} |
||
86 | */ |
||
87 | public function add($key, $value, array $options=[]) { |
||
92 | |||
93 | /** |
||
94 | * add a relation to the resource |
||
95 | * |
||
96 | * adds included resources if found inside the relation, unless $options['includeContainedResources'] is set to false |
||
97 | * |
||
98 | * @param string $key |
||
99 | * @param mixed $relation ResourceInterface | ResourceInterface[] | CollectionDocument |
||
100 | * @param array $links optional |
||
101 | * @param array $meta optional |
||
102 | * @param array $options optional {@see ResourceDocument::$defaults} |
||
103 | */ |
||
104 | public function addRelationship($key, $relation, array $links=[], array $meta=[], array $options=[]) { |
||
115 | |||
116 | /** |
||
117 | * @param string $key |
||
118 | * @param string $href |
||
119 | * @param array $meta optional, if given a LinkObject is added, otherwise a link string is added |
||
120 | * @param string $level one of the Document::LEVEL_* constants, optional, defaults to Document::LEVEL_ROOT |
||
121 | */ |
||
122 | View Code Duplication | public function addLink($key, $href, array $meta=[], $level=Document::LEVEL_ROOT) { |
|
132 | |||
133 | /** |
||
134 | * set the self link on the resource |
||
135 | * |
||
136 | * @param string $href |
||
137 | * @param array $meta optional |
||
138 | */ |
||
139 | View Code Duplication | public function setSelfLink($href, array $meta=[], $level=Document::LEVEL_RESOURCE) { |
|
149 | |||
150 | /** |
||
151 | * @param string $key |
||
152 | * @param mixed $value |
||
153 | * @param string $level one of the Document::LEVEL_* constants, optional, defaults to Document::LEVEL_ROOT |
||
154 | */ |
||
155 | public function addMeta($key, $value, $level=Document::LEVEL_ROOT) { |
||
163 | |||
164 | /** |
||
165 | * wrapping ResourceObject spec api |
||
166 | */ |
||
167 | |||
168 | /** |
||
169 | * @param string $type |
||
170 | */ |
||
171 | public function setType($type) { |
||
174 | |||
175 | /** |
||
176 | * @param string|int $id will be casted to a string |
||
177 | */ |
||
178 | public function setId($id) { |
||
181 | |||
182 | /** |
||
183 | * @param AttributesObject $attributesObject |
||
184 | * @param array $options optional {@see ResourceObject::$defaults} |
||
185 | */ |
||
186 | public function setAttributesObject(AttributesObject $attributesObject, array $options=[]) { |
||
191 | |||
192 | /** |
||
193 | * add a RelationshipObject to the resource |
||
194 | * |
||
195 | * adds included resources if found inside the RelationshipObject, unless $options['includeContainedResources'] is set to false |
||
196 | * |
||
197 | * @param string $key |
||
198 | * @param RelationshipObject $relationshipObject |
||
199 | * @param array $options optional {@see ResourceDocument::$defaults} |
||
200 | */ |
||
201 | View Code Duplication | public function addRelationshipObject($key, RelationshipObject $relationshipObject, array $options=[]) { |
|
212 | |||
213 | /** |
||
214 | * set the RelationshipsObject to the resource |
||
215 | * |
||
216 | * adds included resources if found inside the RelationshipObjects inside the RelationshipsObject, unless $options['includeContainedResources'] is set to false |
||
217 | * |
||
218 | * @param RelationshipsObject $relationshipsObject |
||
219 | * @param array $options optional {@see ResourceDocument::$defaults} |
||
220 | */ |
||
221 | View Code Duplication | public function setRelationshipsObject(RelationshipsObject $relationshipsObject, array $options=[]) { |
|
232 | |||
233 | /** |
||
234 | * spec api |
||
235 | */ |
||
236 | |||
237 | /** |
||
238 | * overwrites the primary resource |
||
239 | * |
||
240 | * adds included resources if found inside the resource's relationships, unless $options['includeContainedResources'] is set to false |
||
241 | * |
||
242 | * @param ResourceInterface $resource |
||
243 | * @param array $options optional {@see ResourceDocument::$defaults} |
||
244 | * |
||
245 | * @throws InputException if the $resource is a ResourceDocument itself |
||
246 | */ |
||
247 | public function setPrimaryResource(ResourceInterface $resource, array $options=[]) { |
||
260 | |||
261 | /** |
||
262 | * internal api |
||
263 | */ |
||
264 | |||
265 | /** |
||
266 | * @internal |
||
267 | * |
||
268 | * @throws Exception |
||
269 | */ |
||
270 | private function ensureResourceObject() { |
||
275 | |||
276 | /** |
||
277 | * DocumentInterface |
||
278 | */ |
||
279 | |||
280 | /** |
||
281 | * @inheritDoc |
||
282 | */ |
||
283 | public function toArray() { |
||
293 | |||
294 | /** |
||
295 | * ResourceInterface |
||
296 | */ |
||
297 | |||
298 | /** |
||
299 | * @inheritDoc |
||
300 | */ |
||
301 | public function getResource($identifierOnly=false) { |
||
304 | } |
||
305 |