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 ReflectionObject |
||
| 19 | { |
||
| 20 | private $object; |
||
| 21 | private $properties; |
||
| 22 | private $injectionStrategy; |
||
| 23 | private $extractionStrategy; |
||
| 24 | |||
| 25 | 13 | View Code Duplication | public function __construct( |
| 46 | |||
| 47 | /** |
||
| 48 | * Add a property that will be injected |
||
| 49 | * |
||
| 50 | * @param string $name |
||
| 51 | * @param mixed $value |
||
| 52 | * |
||
| 53 | * @return self |
||
| 54 | */ |
||
| 55 | 4 | public function withProperty(string $name, $value) |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Add a set of properties that need to be injected |
||
| 67 | * |
||
| 68 | * @param array<string, mixed> $properties |
||
| 69 | * |
||
| 70 | * @return self |
||
| 71 | */ |
||
| 72 | 1 | View Code Duplication | public function withProperties(array $properties): self |
| 87 | |||
| 88 | /** |
||
| 89 | * Return the collection of properties that will be injected in the object |
||
| 90 | * |
||
| 91 | * @return MapInterface<string, mixed> |
||
| 92 | */ |
||
| 93 | 2 | public function properties(): MapInterface |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Return the list of injection strategies used |
||
| 100 | * |
||
| 101 | * @return InjectionStrategyInterface |
||
| 102 | */ |
||
| 103 | 1 | public function injectionStrategy(): InjectionStrategyInterface |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Return the list of extraction strategies used |
||
| 110 | * |
||
| 111 | * @return ExtractionStrategyInterface |
||
| 112 | */ |
||
| 113 | 1 | public function extractionStrategy(): ExtractionStrategyInterface |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Return the object with the list of properties set on it |
||
| 120 | * |
||
| 121 | * @return object |
||
| 122 | */ |
||
| 123 | public function build() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Extract the given list of properties |
||
| 134 | * |
||
| 135 | * @param string[] $properties |
||
| 136 | * |
||
| 137 | * @return MapInterface<string, mixed> |
||
| 138 | */ |
||
| 139 | 2 | public function extract(array $properties): MapInterface |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Inject the given key/value pair into the object |
||
| 155 | * |
||
| 156 | * @param string $key |
||
| 157 | * @param mixed $value |
||
| 158 | * |
||
| 159 | * @return void |
||
| 160 | */ |
||
| 161 | 5 | private function inject(string $key, $value): void |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Extract the given property out of the object |
||
| 168 | * |
||
| 169 | * @param string $property |
||
| 170 | * |
||
| 171 | * @return mixed |
||
| 172 | */ |
||
| 173 | 2 | private function extractProperty(string $property) |
|
| 177 | } |
||
| 178 |
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: