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 |
||
| 35 | class CustomPropertiesBackend implements BackendInterface { |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Ignored properties |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | private $ignoredProperties = array( |
||
| 43 | '{DAV:}getcontentlength', |
||
| 44 | '{DAV:}getcontenttype', |
||
| 45 | '{DAV:}getetag', |
||
| 46 | '{DAV:}quota-used-bytes', |
||
| 47 | '{DAV:}quota-available-bytes', |
||
| 48 | '{http://owncloud.org/ns}permissions', |
||
| 49 | '{http://owncloud.org/ns}downloadURL', |
||
| 50 | '{http://owncloud.org/ns}dDC', |
||
| 51 | '{http://owncloud.org/ns}size', |
||
| 52 | ); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var Tree |
||
| 56 | */ |
||
| 57 | private $tree; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var IDBConnection |
||
| 61 | */ |
||
| 62 | private $connection; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | private $user; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Properties cache |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | private $cache = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param Tree $tree node tree |
||
| 78 | * @param IDBConnection $connection database connection |
||
| 79 | * @param IUser $user owner of the tree and properties |
||
| 80 | */ |
||
| 81 | View Code Duplication | public function __construct( |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Fetches properties for a path. |
||
| 92 | * |
||
| 93 | * @param string $path |
||
| 94 | * @param PropFind $propFind |
||
| 95 | * @return void |
||
| 96 | */ |
||
| 97 | public function propFind($path, PropFind $propFind) { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Updates properties for a path |
||
| 139 | * |
||
| 140 | * @param string $path |
||
| 141 | * @param PropPatch $propPatch |
||
| 142 | * |
||
| 143 | * @return void |
||
| 144 | */ |
||
| 145 | public function propPatch($path, PropPatch $propPatch) { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * This method is called after a node is deleted. |
||
| 153 | * |
||
| 154 | * @param string $path path of node for which to delete properties |
||
| 155 | */ |
||
| 156 | View Code Duplication | public function delete($path) { |
|
| 165 | |||
| 166 | /** |
||
| 167 | * This method is called after a successful MOVE |
||
| 168 | * |
||
| 169 | * @param string $source |
||
| 170 | * @param string $destination |
||
| 171 | * |
||
| 172 | * @return void |
||
| 173 | */ |
||
| 174 | View Code Duplication | public function move($source, $destination) { |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Returns a list of properties for this nodes.; |
||
| 185 | * @param string $path |
||
| 186 | * @param array $requestedProperties requested properties or empty array for "all" |
||
| 187 | * @return array |
||
| 188 | * @note The properties list is a list of propertynames the client |
||
| 189 | * requested, encoded as xmlnamespace#tagName, for example: |
||
| 190 | * http://www.example.org/namespace#author If the array is empty, all |
||
| 191 | * properties should be returned |
||
| 192 | */ |
||
| 193 | View Code Duplication | private function getProperties($path, array $requestedProperties) { |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Update properties |
||
| 230 | * |
||
| 231 | * @param string $path node for which to update properties |
||
| 232 | * @param array $properties array of properties to update |
||
| 233 | * |
||
| 234 | * @return bool |
||
| 235 | */ |
||
| 236 | View Code Duplication | private function updateProperties($path, $properties) { |
|
| 290 | |||
| 291 | } |
||
| 292 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.