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 |
||
| 23 | final class UnixDisk implements Disk |
||
| 24 | { |
||
| 25 | private static $columns = [ |
||
| 26 | 'Size' => 'size', |
||
| 27 | 'Used' => 'used', |
||
| 28 | 'Avail' => 'available', |
||
| 29 | 'Use%' => 'usage', |
||
| 30 | 'Capacity' => 'usage', |
||
| 31 | 'Mounted' => 'mountPoint', |
||
| 32 | ]; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * {@inheritdoc} |
||
| 36 | */ |
||
| 37 | 2 | public function volumes(): MapInterface |
|
| 43 | |||
| 44 | 1 | public function get(MountPoint $point): Volume |
|
| 50 | |||
| 51 | 2 | View Code Duplication | private function run(string $command): Str |
| 62 | |||
| 63 | /** |
||
| 64 | * @return MapInterface<string, Volume> |
||
|
1 ignored issue
–
show
|
|||
| 65 | */ |
||
| 66 | 2 | private function parse(Str $output): MapInterface |
|
| 122 | } |
||
| 123 |
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: