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 |
||
| 17 | class ResourceObject extends ResourceIdentifierObject implements RecursiveResourceContainerInterface { |
||
|
|
|||
| 18 | use LinksManager; |
||
| 19 | |||
| 20 | /** @var AttributesObject */ |
||
| 21 | protected $attributes; |
||
| 22 | /** @var RelationshipsObject */ |
||
| 23 | protected $relationships; |
||
| 24 | /** @var array */ |
||
| 25 | protected static $defaults = [ |
||
| 26 | /** |
||
| 27 | * blocks 'type' as a keyword inside attributes or relationships |
||
| 28 | * the specification doesn't allow this as 'type' is already set at the root of a resource |
||
| 29 | * set to true if migrating to jsonapi and currently using 'type' as attribute or relationship |
||
| 30 | */ |
||
| 31 | 'enforceTypeFieldNamespace' => true, |
||
| 32 | ]; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * human api |
||
| 36 | */ |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @note if an `id` is set inside $attributes, it is removed from there |
||
| 40 | * and if $id is null, it is filled with that value |
||
| 41 | * it is common to find it inside, and not doing so will cause an exception |
||
| 42 | * |
||
| 43 | * @param array $attributes |
||
| 44 | * @param string $type optional |
||
| 45 | * @param string|int $id optional |
||
| 46 | * @param array $options optional {@see ResourceObject::$defaults} |
||
| 47 | * @return ResourceObject |
||
| 48 | */ |
||
| 49 | public static function fromArray(array $attributes, $type=null, $id=null, array $options=[]) { |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param object $attributes |
||
| 66 | * @param string $type optional |
||
| 67 | * @param string|int $id optional |
||
| 68 | * @param array $options optional {@see ResourceObject::$defaults} |
||
| 69 | * @return ResourceObject |
||
| 70 | */ |
||
| 71 | public static function fromObject($attributes, $type=null, $id=null, array $options=[]) { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * add key-value pairs to attributes |
||
| 79 | * |
||
| 80 | * @param string $key |
||
| 81 | * @param mixed $value |
||
| 82 | * @param array $options optional {@see ResourceObject::$defaults} |
||
| 83 | */ |
||
| 84 | public function add($key, $value, array $options=[]) { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param string $key |
||
| 98 | * @param mixed $relation ResourceInterface | ResourceInterface[] | CollectionDocument |
||
| 99 | * @param array $links optional |
||
| 100 | * @param array $meta optional |
||
| 101 | * @param array $options optional {@see ResourceObject::$defaults} |
||
| 102 | * @return RelationshipObject |
||
| 103 | */ |
||
| 104 | public function addRelationship($key, $relation, array $links=[], array $meta=[], array $options=[]) { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param string $href |
||
| 114 | * @param array $meta optional, if given a LinkObject is added, otherwise a link string is added |
||
| 115 | */ |
||
| 116 | public function setSelfLink($href, array $meta=[]) { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * spec api |
||
| 122 | */ |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param AttributesObject $attributesObject |
||
| 126 | * @param array $options optional {@see ResourceObject::$defaults} |
||
| 127 | */ |
||
| 128 | public function setAttributesObject(AttributesObject $attributesObject, array $options=[]) { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param string $key |
||
| 138 | * @param RelationshipObject $relationshipObject |
||
| 139 | * @param array $options optional {@see ResourceObject::$defaults} |
||
| 140 | * |
||
| 141 | * @throws DuplicateException if the resource is contained as a resource in the relationship |
||
| 142 | */ |
||
| 143 | public function addRelationshipObject($key, RelationshipObject $relationshipObject, array $options=[]) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param RelationshipsObject $relationshipsObject |
||
| 159 | */ |
||
| 160 | public function setRelationshipsObject(RelationshipsObject $relationshipsObject) { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * internal api |
||
| 170 | */ |
||
| 171 | |||
| 172 | /** |
||
| 173 | * whether the ResourceObject is empty except for the ResourceIdentifierObject |
||
| 174 | * |
||
| 175 | * this can be used to determine if a Relationship's resource could be added as included resource |
||
| 176 | * |
||
| 177 | * @internal |
||
| 178 | * |
||
| 179 | * @return boolean |
||
| 180 | */ |
||
| 181 | public function hasIdentifierPropertiesOnly() { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * ResourceInterface |
||
| 197 | */ |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @inheritDoc |
||
| 201 | */ |
||
| 202 | public function getResource($identifierOnly=false) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * ObjectInterface |
||
| 212 | */ |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @inheritDoc |
||
| 216 | */ |
||
| 217 | public function isEmpty() { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @inheritDoc |
||
| 236 | */ |
||
| 237 | View Code Duplication | public function toArray() { |
|
| 252 | |||
| 253 | /** |
||
| 254 | * RecursiveResourceContainerInterface |
||
| 255 | */ |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @inheritDoc |
||
| 259 | */ |
||
| 260 | public function getNestedContainedResourceObjects() { |
||
| 267 | } |
||
| 268 |