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 |
||
| 12 | class ResourceIdentifierObject implements ObjectInterface, ResourceInterface { |
||
|
|
|||
| 13 | use AtMemberManager; |
||
| 14 | |||
| 15 | /** @var string */ |
||
| 16 | protected $type; |
||
| 17 | /** @var string */ |
||
| 18 | protected $id; |
||
| 19 | /** @var MetaObject */ |
||
| 20 | protected $meta; |
||
| 21 | /** @var Validator */ |
||
| 22 | protected $validator; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @note $type and $id are optional to pass during construction |
||
| 26 | * however they are required for a valid ResourceIdentifierObject |
||
| 27 | * so use ->setType() and ->setId() if not passing them during construction |
||
| 28 | * |
||
| 29 | * @param string $type optional |
||
| 30 | * @param string|int $id optional |
||
| 31 | */ |
||
| 32 | public function __construct($type=null, $id=null) { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * human api |
||
| 49 | */ |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param string $key |
||
| 53 | * @param mixed $value |
||
| 54 | */ |
||
| 55 | public function addMeta($key, $value) { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * spec api |
||
| 65 | */ |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param string $type |
||
| 69 | */ |
||
| 70 | public function setType($type) { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param string|int $id will be casted to a string |
||
| 76 | */ |
||
| 77 | public function setId($id) { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param MetaObject $metaObject |
||
| 83 | */ |
||
| 84 | public function setMetaObject(MetaObject $metaObject) { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * internal api |
||
| 90 | */ |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @internal |
||
| 94 | * |
||
| 95 | * @param ResourceObject $resourceObject |
||
| 96 | * @return ResourceIdentifierObject |
||
| 97 | */ |
||
| 98 | public static function fromResourceObject(ResourceObject $resourceObject) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @internal |
||
| 110 | * |
||
| 111 | * @param ResourceInterface $resource |
||
| 112 | * @return boolean |
||
| 113 | * |
||
| 114 | * @throws Exception if one or both are missing identification |
||
| 115 | */ |
||
| 116 | public function equals(ResourceInterface $resource) { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @internal |
||
| 126 | * |
||
| 127 | * @return boolean |
||
| 128 | */ |
||
| 129 | public function hasIdentification() { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * get a key to uniquely define this resource |
||
| 135 | * |
||
| 136 | * @internal |
||
| 137 | * |
||
| 138 | * @return string |
||
| 139 | * |
||
| 140 | * @throws Exception if type or id is not set yet |
||
| 141 | */ |
||
| 142 | public function getIdentificationKey() { |
||
| 149 | |||
| 150 | /** |
||
| 151 | * ObjectInterface |
||
| 152 | */ |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @inheritDoc |
||
| 156 | */ |
||
| 157 | View Code Duplication | public function isEmpty() { |
|
| 170 | |||
| 171 | /** |
||
| 172 | * @inheritDoc |
||
| 173 | */ |
||
| 174 | View Code Duplication | public function toArray() { |
|
| 189 | |||
| 190 | /** |
||
| 191 | * ResourceInterface |
||
| 192 | */ |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @inheritDoc |
||
| 196 | */ |
||
| 197 | public function getResource($identifierOnly=false) { |
||
| 200 | } |
||
| 201 |