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 |
||
| 21 | class Simulate extends Process |
||
| 22 | { |
||
| 23 | use Compression; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Execute backups. |
||
| 27 | * |
||
| 28 | * @param \phpbu\App\Configuration $configuration |
||
| 29 | * @return \phpbu\App\Result |
||
| 30 | * @throws \Exception |
||
| 31 | */ |
||
| 32 | 5 | public function run(Configuration $configuration) : Result |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Simulate the backup. |
||
| 62 | * |
||
| 63 | * @param \phpbu\App\Configuration\Backup $conf |
||
| 64 | * @param \phpbu\App\Backup\Target $target |
||
| 65 | * @throws \Exception |
||
| 66 | */ |
||
| 67 | 5 | View Code Duplication | protected function simulateSource(Configuration\Backup $conf, Target $target) |
| 79 | |||
| 80 | /** |
||
| 81 | * Simulate checks. |
||
| 82 | * |
||
| 83 | * @param \phpbu\App\Configuration\Backup $backup |
||
| 84 | * @param \phpbu\App\Backup\Target $target |
||
| 85 | * @param \phpbu\App\Backup\Collector $collector |
||
| 86 | * @throws \Exception |
||
| 87 | */ |
||
| 88 | 5 | View Code Duplication | protected function simulateChecks(Configuration\Backup $backup, Target $target, Collector $collector) |
| 99 | |||
| 100 | /** |
||
| 101 | * Simulate encryption. |
||
| 102 | * |
||
| 103 | * @param \phpbu\App\Configuration\Backup $backup |
||
| 104 | * @param \phpbu\App\Backup\Target $target |
||
| 105 | * @throws \phpbu\App\Exception |
||
| 106 | */ |
||
| 107 | 5 | View Code Duplication | protected function simulateCrypt(Configuration\Backup $backup, Target $target) |
| 119 | |||
| 120 | /** |
||
| 121 | * Simulate all syncs. |
||
| 122 | * |
||
| 123 | * @param \phpbu\App\Configuration\Backup $backup |
||
| 124 | * @param \phpbu\App\Backup\Target $target |
||
| 125 | * @throws \Exception |
||
| 126 | */ |
||
| 127 | 5 | protected function simulateSyncs(Configuration\Backup $backup, Target $target) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Simulate the cleanup. |
||
| 140 | * |
||
| 141 | * @param \phpbu\App\Configuration\Backup $backup |
||
| 142 | * @param \phpbu\App\Backup\Target $target |
||
| 143 | * @param \phpbu\App\Backup\Collector $collector |
||
| 144 | * @throws \phpbu\App\Exception |
||
| 145 | */ |
||
| 146 | 5 | View Code Duplication | protected function simulateCleanup(Configuration\Backup $backup, Target $target, Collector $collector) |
| 159 | |||
| 160 | /** |
||
| 161 | * Execute the compressor. |
||
| 162 | * Returns the path to the created archive file. |
||
| 163 | * |
||
| 164 | * @param \phpbu\App\Backup\Compressor\Executable $compressor |
||
| 165 | * @param \phpbu\App\Backup\Target $target |
||
| 166 | * @param \phpbu\App\Result $result |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | 3 | protected function executeCompressor(Compressor\Executable $compressor, Target $target, Result $result) : string |
|
| 174 | } |
||
| 175 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: