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 | */ |
||
| 50 | 1020 | public function __construct($path = null) |
|
| 55 | |||
| 56 | /** |
||
| 57 | * {@inheritdoc} |
||
| 58 | */ |
||
| 59 | 712 | public function getPath() |
|
| 63 | |||
| 64 | /** |
||
| 65 | * {@inheritdoc} |
||
| 66 | */ |
||
| 67 | 438 | public function getName() |
|
| 71 | |||
| 72 | /** |
||
| 73 | * {@inheritdoc} |
||
| 74 | */ |
||
| 75 | 3 | View Code Duplication | public function getChild($relPath) |
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritdoc} |
||
| 86 | */ |
||
| 87 | 3 | public function hasChild($relPath) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritdoc} |
||
| 98 | */ |
||
| 99 | 3 | public function hasChildren() |
|
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritdoc} |
||
| 110 | */ |
||
| 111 | 8 | View Code Duplication | public function listChildren() |
| 112 | { |
||
| 113 | 8 | $children = new ArrayResourceCollection(); |
|
| 114 | |||
| 115 | 8 | if (!$this->getRepository()) { |
|
| 116 | 1 | return $children; |
|
| 117 | } |
||
| 118 | |||
| 119 | 7 | foreach ($this->getRepository()->listChildren($this->getRepositoryPath()) as $child) { |
|
| 120 | 2 | $children[$child->getName()] = $child; |
|
| 121 | } |
||
| 122 | |||
| 123 | 7 | return $children; |
|
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * {@inheritdoc} |
||
| 128 | */ |
||
| 129 | 8 | public function getVersions() |
|
| 130 | { |
||
| 131 | 8 | if (!$this->getRepository()) { |
|
| 132 | 4 | return new VersionList($this->getRepositoryPath(), array($this)); |
|
| 133 | } |
||
| 134 | |||
| 135 | 4 | return $this->getRepository()->getVersions($this->getRepositoryPath()); |
|
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * {@inheritdoc} |
||
| 140 | */ |
||
| 141 | public function getMetadata() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * {@inheritdoc} |
||
| 148 | */ |
||
| 149 | 888 | public function attachTo(ResourceRepository $repo, $path = null) |
|
| 150 | { |
||
| 151 | 888 | $this->repo = $repo; |
|
| 152 | |||
| 153 | 888 | if (null !== $path) { |
|
| 154 | 812 | $this->path = $path; |
|
| 155 | 812 | $this->repoPath = $path; |
|
| 156 | } |
||
| 157 | 888 | } |
|
| 158 | |||
| 159 | /** |
||
| 160 | * {@inheritdoc} |
||
| 161 | */ |
||
| 162 | 28 | public function detach() |
|
| 166 | |||
| 167 | /** |
||
| 168 | * {@inheritdoc} |
||
| 169 | */ |
||
| 170 | 402 | public function getRepository() |
|
| 174 | |||
| 175 | /** |
||
| 176 | * {@inheritdoc} |
||
| 177 | */ |
||
| 178 | 161 | public function getRepositoryPath() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * {@inheritdoc} |
||
| 185 | */ |
||
| 186 | 840 | public function isAttached() |
|
| 190 | |||
| 191 | /** |
||
| 192 | * {@inheritdoc} |
||
| 193 | */ |
||
| 194 | 56 | public function createReference($path) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * {@inheritdoc} |
||
| 204 | */ |
||
| 205 | 84 | public function isReference() |
|
| 209 | |||
| 210 | /** |
||
| 211 | * {@inheritdoc} |
||
| 212 | */ |
||
| 213 | 27 | public function serialize() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * {@inheritdoc} |
||
| 224 | */ |
||
| 225 | 27 | public function unserialize($string) |
|
| 231 | |||
| 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 | 27 | protected function preSerialize(array &$data) |
|
| 245 | |||
| 246 | /** |
||
| 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 | 27 | protected function postUnserialize(array $data) |
|
| 260 | } |
||
| 261 |