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 |
||
| 27 | class GenericResource implements PuliResource |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var ResourceRepository |
||
| 31 | */ |
||
| 32 | private $repo; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $path; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private $repoPath; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Creates a new resource. |
||
| 46 | * |
||
| 47 | * @param string|null $path The path of the resource. |
||
| 48 | */ |
||
| 49 | 683 | public function __construct($path = null) |
|
| 54 | |||
| 55 | /** |
||
| 56 | * {@inheritdoc} |
||
| 57 | */ |
||
| 58 | 285 | public function getPath() |
|
| 62 | |||
| 63 | /** |
||
| 64 | * {@inheritdoc} |
||
| 65 | */ |
||
| 66 | 294 | public function getName() |
|
| 70 | |||
| 71 | /** |
||
| 72 | * {@inheritdoc} |
||
| 73 | */ |
||
| 74 | 4 | View Code Duplication | public function getChild($relPath) |
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | */ |
||
| 86 | 3 | public function hasChild($relPath) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * {@inheritdoc} |
||
| 97 | */ |
||
| 98 | 3 | public function hasChildren() |
|
| 106 | |||
| 107 | /** |
||
| 108 | * {@inheritdoc} |
||
| 109 | */ |
||
| 110 | 6 | View Code Duplication | public function listChildren() |
| 124 | |||
| 125 | /** |
||
| 126 | * {@inheritdoc} |
||
| 127 | */ |
||
| 128 | public function getStack() |
||
| 129 | { |
||
| 130 | if (!$this->getRepository()) { |
||
| 131 | throw ResourceNotFoundException::forPath($this->getRepositoryPath()); |
||
| 132 | } |
||
| 133 | |||
| 134 | return $this->getRepository()->getStack($this->getRepositoryPath()); |
||
| 135 | } |
||
| 136 | 406 | ||
| 137 | /** |
||
| 138 | 406 | * {@inheritdoc} |
|
| 139 | */ |
||
| 140 | 406 | public function getMetadata() |
|
| 141 | 352 | { |
|
| 142 | 352 | return new ResourceMetadata(); |
|
| 143 | 352 | } |
|
| 144 | 406 | ||
| 145 | /** |
||
| 146 | * {@inheritdoc} |
||
| 147 | */ |
||
| 148 | public function attachTo(ResourceRepository $repo, $path = null) |
||
| 149 | 21 | { |
|
| 150 | $this->repo = $repo; |
||
| 151 | 21 | ||
| 152 | 21 | if (null !== $path) { |
|
| 153 | $this->path = $path; |
||
| 154 | $this->repoPath = $path; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | 212 | ||
| 158 | /** |
||
| 159 | 212 | * {@inheritdoc} |
|
| 160 | */ |
||
| 161 | public function detach() |
||
| 162 | { |
||
| 163 | $this->repo = null; |
||
| 164 | } |
||
| 165 | 105 | ||
| 166 | /** |
||
| 167 | 105 | * {@inheritdoc} |
|
| 168 | */ |
||
| 169 | public function getRepository() |
||
| 170 | { |
||
| 171 | return $this->repo; |
||
| 172 | } |
||
| 173 | 263 | ||
| 174 | /** |
||
| 175 | 263 | * {@inheritdoc} |
|
| 176 | */ |
||
| 177 | public function getRepositoryPath() |
||
| 178 | { |
||
| 179 | return $this->repoPath; |
||
| 180 | } |
||
| 181 | 42 | ||
| 182 | /** |
||
| 183 | 42 | * {@inheritdoc} |
|
| 184 | 42 | */ |
|
| 185 | public function isAttached() |
||
| 186 | 42 | { |
|
| 187 | return null !== $this->repo; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * {@inheritdoc} |
||
| 192 | 63 | */ |
|
| 193 | public function createReference($path) |
||
| 194 | 63 | { |
|
| 195 | $ref = clone $this; |
||
| 196 | $ref->path = $path; |
||
| 197 | |||
| 198 | return $ref; |
||
| 199 | } |
||
| 200 | 20 | ||
| 201 | /** |
||
| 202 | 20 | * {@inheritdoc} |
|
| 203 | */ |
||
| 204 | 20 | public function isReference() |
|
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritdoc} |
||
| 211 | */ |
||
| 212 | 20 | public function serialize() |
|
| 220 | |||
| 221 | /** |
||
| 222 | * {@inheritdoc} |
||
| 223 | */ |
||
| 224 | public function unserialize($string) |
||
| 225 | { |
||
| 226 | $data = unserialize($string); |
||
| 227 | 20 | ||
| 228 | $this->postUnserialize($data); |
||
| 230 | 20 | ||
| 231 | 20 | /** |
|
| 232 | * Invoked before serializing a resource. |
||
| 233 | * |
||
| 234 | * Override this method if you want to serialize custom data in subclasses. |
||
| 235 | * |
||
| 236 | * @param array $data The data to serialize. Add custom data at the end of |
||
| 237 | * the array. |
||
| 238 | */ |
||
| 239 | protected function preSerialize(array &$data) |
||
| 244 | 20 | ||
| 245 | 20 | /** |
|
| 246 | 20 | * Invoked after unserializing a resource. |
|
| 247 | * |
||
| 248 | * Override this method if you want to unserialize custom data in |
||
| 249 | * subclasses. |
||
| 250 | * |
||
| 251 | * @param array $data The unserialized data. Pop your custom data from the |
||
| 252 | * end of the array before calling the parent method. |
||
| 253 | */ |
||
| 254 | protected function postUnserialize(array $data) |
||
| 259 | } |
||
| 260 |