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 |
||
| 19 | class Container |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Config |
||
| 23 | * |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $config; |
||
| 27 | |||
| 28 | |||
| 29 | /** |
||
| 30 | * Service registry |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $service = []; |
||
| 35 | |||
| 36 | |||
| 37 | /** |
||
| 38 | * Registered but not initialized service registry |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $registry = []; |
||
| 43 | |||
| 44 | |||
| 45 | /** |
||
| 46 | * Create container |
||
| 47 | * |
||
| 48 | * @param array $config |
||
| 49 | */ |
||
| 50 | public function __construct(array $config=[]) |
||
| 54 | |||
| 55 | |||
| 56 | /** |
||
| 57 | * Get service |
||
| 58 | * |
||
| 59 | * @param string $name |
||
| 60 | * @return mixed |
||
| 61 | */ |
||
| 62 | public function get(string $name) |
||
| 76 | |||
| 77 | |||
| 78 | /** |
||
| 79 | * Add service |
||
| 80 | * |
||
| 81 | * @param string $name |
||
| 82 | * @param Closure $service |
||
| 83 | * @return Container |
||
| 84 | */ |
||
| 85 | public function add(string $name, Closure $service): Container |
||
| 94 | |||
| 95 | |||
| 96 | /** |
||
| 97 | * Check if service is registered |
||
| 98 | * |
||
| 99 | * @return bool |
||
| 100 | */ |
||
| 101 | public function has(string $name): bool |
||
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * Auto wire service |
||
| 109 | * |
||
| 110 | * @param string $name |
||
| 111 | * @return mixed |
||
| 112 | */ |
||
| 113 | protected function autoWire(string $name) |
||
| 153 | |||
| 154 | |||
| 155 | /** |
||
| 156 | * Create instance |
||
| 157 | * |
||
| 158 | * @param string $name |
||
| 159 | * @param ReflectionClass $class |
||
| 160 | * @param array $args |
||
| 161 | * @return mixed |
||
| 162 | */ |
||
| 163 | protected function createInstance(string $name, ReflectionClass $class, array $args) |
||
| 185 | |||
| 186 | |||
| 187 | /** |
||
| 188 | * Get config value |
||
| 189 | * |
||
| 190 | * @param string $name |
||
| 191 | * @param string $param |
||
| 192 | * @return mixed |
||
| 193 | */ |
||
| 194 | public function getParam(string $name, string $param) |
||
| 206 | } |
||
| 207 |
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: