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 |
||
| 28 | class GenericResource implements PuliResource |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var ResourceRepository |
||
| 32 | */ |
||
| 33 | private $repo; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | private $path; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | private $repoPath; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Creates a new resource. |
||
| 47 | * |
||
| 48 | * @param string|null $path The path of the resource. |
||
| 49 | 683 | */ |
|
| 50 | public function __construct($path = null) |
||
| 51 | 683 | { |
|
| 52 | 683 | $this->path = $path; |
|
| 53 | 683 | $this->repoPath = $path; |
|
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * {@inheritdoc} |
||
| 58 | 285 | */ |
|
| 59 | public function getPath() |
||
| 60 | 285 | { |
|
| 61 | return $this->path; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * {@inheritdoc} |
||
| 66 | 294 | */ |
|
| 67 | public function getName() |
||
| 68 | 294 | { |
|
| 69 | return $this->path ? basename($this->path) : null; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * {@inheritdoc} |
||
| 74 | 4 | */ |
|
| 75 | View Code Duplication | public function getChild($relPath) |
|
| 76 | 4 | { |
|
| 77 | 1 | if (!$this->getRepository()) { |
|
| 78 | throw ResourceNotFoundException::forPath($this->getRepositoryPath().'/'.$relPath); |
||
| 79 | } |
||
| 80 | 3 | ||
| 81 | return $this->getRepository()->get($this->getRepositoryPath().'/'.$relPath); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritdoc} |
||
| 86 | 3 | */ |
|
| 87 | public function hasChild($relPath) |
||
| 88 | 3 | { |
|
| 89 | 1 | if (!$this->getRepository()) { |
|
| 90 | return false; |
||
| 91 | } |
||
| 92 | 2 | ||
| 93 | return $this->getRepository()->contains($this->getRepositoryPath().'/'.$relPath); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritdoc} |
||
| 98 | 3 | */ |
|
| 99 | public function hasChildren() |
||
| 100 | 3 | { |
|
| 101 | 1 | if (!$this->getRepository()) { |
|
| 102 | return false; |
||
| 103 | } |
||
| 104 | 2 | ||
| 105 | return $this->getRepository()->hasChildren($this->getRepositoryPath()); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritdoc} |
||
| 110 | 6 | */ |
|
| 111 | View Code Duplication | public function listChildren() |
|
| 125 | |||
| 126 | /** |
||
| 127 | * {@inheritdoc} |
||
| 128 | */ |
||
| 129 | public function getStack() |
||
| 130 | { |
||
| 131 | if (!$this->getRepository()) { |
||
| 137 | |||
| 138 | 406 | /** |
|
| 139 | * {@inheritdoc} |
||
| 140 | 406 | */ |
|
| 141 | 352 | public function getMetadata() |
|
| 145 | |||
| 146 | /** |
||
| 147 | * {@inheritdoc} |
||
| 148 | */ |
||
| 149 | 21 | public function attachTo(ResourceRepository $repo, $path = null) |
|
| 158 | |||
| 159 | 212 | /** |
|
| 160 | * {@inheritdoc} |
||
| 161 | */ |
||
| 162 | public function detach() |
||
| 166 | |||
| 167 | 105 | /** |
|
| 168 | * {@inheritdoc} |
||
| 169 | */ |
||
| 170 | public function getRepository() |
||
| 174 | |||
| 175 | 263 | /** |
|
| 176 | * {@inheritdoc} |
||
| 177 | */ |
||
| 178 | public function getRepositoryPath() |
||
| 182 | |||
| 183 | 42 | /** |
|
| 184 | 42 | * {@inheritdoc} |
|
| 185 | */ |
||
| 186 | 42 | public function isAttached() |
|
| 190 | |||
| 191 | /** |
||
| 192 | 63 | * {@inheritdoc} |
|
| 193 | */ |
||
| 194 | 63 | public function createReference($path) |
|
| 201 | |||
| 202 | 20 | /** |
|
| 203 | * {@inheritdoc} |
||
| 204 | 20 | */ |
|
| 205 | public function isReference() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * {@inheritdoc} |
||
| 212 | 20 | */ |
|
| 213 | public function serialize() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * {@inheritdoc} |
||
| 224 | */ |
||
| 225 | public function unserialize($string) |
||
| 231 | 20 | ||
| 232 | /** |
||
| 233 | * Invoked before serializing a resource. |
||
| 234 | * |
||
| 235 | * Override this method if you want to serialize custom data in subclasses. |
||
| 236 | * |
||
| 237 | * @param array $data The data to serialize. Add custom data at the end of |
||
| 238 | * the array. |
||
| 239 | */ |
||
| 240 | protected function preSerialize(array &$data) |
||
| 245 | 20 | ||
| 246 | 20 | /** |
|
| 247 | * Invoked after unserializing a resource. |
||
| 248 | * |
||
| 249 | * Override this method if you want to unserialize custom data in |
||
| 250 | * subclasses. |
||
| 251 | * |
||
| 252 | * @param array $data The unserialized data. Pop your custom data from the |
||
| 253 | * end of the array before calling the parent method. |
||
| 254 | */ |
||
| 255 | protected function postUnserialize(array $data) |
||
| 260 | } |
||
| 261 |