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 |
||
| 12 | class ReflectionObject |
||
| 13 | { |
||
| 14 | private $object; |
||
| 15 | private $properties; |
||
| 16 | private $injectionStrategies; |
||
| 17 | |||
| 18 | 13 | public function __construct( |
|
| 45 | |||
| 46 | /** |
||
| 47 | * Add a property that will be injected |
||
| 48 | * |
||
| 49 | * @param string $name |
||
| 50 | * @param mixed $value |
||
| 51 | * |
||
| 52 | * @return self |
||
| 53 | */ |
||
| 54 | 4 | public function withProperty(string $name, $value) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Add a set of properties that need to be injected |
||
| 66 | * |
||
| 67 | * @param array $properties |
||
| 68 | * |
||
| 69 | * @return self |
||
| 70 | */ |
||
| 71 | 1 | public function withProperties(array $properties): self |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Return the collection of properties that will be injected in the object |
||
| 83 | * |
||
| 84 | * @return CollectionInterface |
||
| 85 | */ |
||
| 86 | 2 | public function getProperties(): CollectionInterface |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Return the list of injection strategies used |
||
| 93 | * |
||
| 94 | * @return TypedCollectionInterface |
||
| 95 | */ |
||
| 96 | 1 | public function getInjectionStrategies(): TypedCollectionInterface |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Return the list of extraction strategies used |
||
| 103 | * |
||
| 104 | * @return TypedCollectionInterface |
||
| 105 | */ |
||
| 106 | 1 | public function getExtractionStrategies(): TypedCollectionInterface |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Return the object with the list of properties set on it |
||
| 113 | * |
||
| 114 | * @return object |
||
| 115 | */ |
||
| 116 | 7 | public function buildObject() |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Extract the given list of properties |
||
| 127 | * |
||
| 128 | * @param array $properties |
||
| 129 | * |
||
| 130 | * @return CollectionInterface |
||
| 131 | */ |
||
| 132 | 2 | public function extract(array $properties): CollectionInterface |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Inject the given key/value pair into the object |
||
| 145 | * |
||
| 146 | * @param string $key |
||
| 147 | * @param mixed $value |
||
| 148 | * |
||
| 149 | * @return void |
||
| 150 | */ |
||
| 151 | 5 | View Code Duplication | private function inject(string $key, $value) |
| 166 | |||
| 167 | /** |
||
| 168 | * Extract the given property out of the object |
||
| 169 | * |
||
| 170 | * @param string $property |
||
| 171 | * |
||
| 172 | * @return mixed |
||
| 173 | */ |
||
| 174 | 2 | View Code Duplication | private function extractProperty(string $property) |
| 187 | } |
||
| 188 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: