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 |
||
| 18 | class Path |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Path |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | private $path; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Raw path, that could contain placeholders |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | private $pathRaw; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Part of path, that is permanent |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private $pathNotChanging; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Indicates if the path changes over time. |
||
| 43 | * |
||
| 44 | * @var bool |
||
| 45 | */ |
||
| 46 | private $pathIsChanging = false; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * List of all path elements. |
||
| 50 | * |
||
| 51 | * @var string[] |
||
| 52 | */ |
||
| 53 | private $pathElements = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Time for replacing placeholders |
||
| 57 | * |
||
| 58 | * @var int |
||
| 59 | */ |
||
| 60 | private $time; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Path constructor. |
||
| 64 | * |
||
| 65 | * @param string $path |
||
| 66 | * @param int|null $time |
||
| 67 | */ |
||
| 68 | 1 | View Code Duplication | public function __construct(string $path, $time = null) |
| 85 | |||
| 86 | /** |
||
| 87 | * Find path elements that can't change because of placeholder usage. |
||
| 88 | * |
||
| 89 | * @param string $path |
||
| 90 | */ |
||
| 91 | View Code Duplication | private function detectPathNotChanging(string $path) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Return path element at given index. |
||
| 115 | * |
||
| 116 | * @param int $index |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | public function getPathElementAtIndex(int $index) : string |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Return the full target path depth. |
||
| 126 | * |
||
| 127 | * @return int |
||
| 128 | */ |
||
| 129 | public function getPathDepth() : int |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Return the path to the backup file. |
||
| 136 | * |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | public function getPath() : string |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Return the path to the backup file. |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | 1 | public function getPathRaw() : string |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Is dirname configured with any date placeholders. |
||
| 156 | * |
||
| 157 | * @return bool |
||
| 158 | */ |
||
| 159 | public function hasChangingPath() : bool |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Return the part of the path that is not changing. |
||
| 166 | * |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | 1 | public function getPathThatIsNotChanging() : string |
|
| 173 | } |
||
| 174 |