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 |
||
22 | class DependencyInjector implements InjectorInterface |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * @var CoffeePotInterface $coffee_pot |
||
27 | */ |
||
28 | private $coffee_pot; |
||
29 | |||
30 | /** |
||
31 | * @var \EEH_Array $array_helper |
||
32 | */ |
||
33 | private $array_helper; |
||
34 | |||
35 | /** |
||
36 | * @var \ReflectionClass[] $reflectors |
||
37 | */ |
||
38 | private $reflectors; |
||
39 | |||
40 | /** |
||
41 | * @var \ReflectionMethod[] $constructors |
||
42 | */ |
||
43 | private $constructors; |
||
44 | |||
45 | /** |
||
46 | * @var \ReflectionParameter[] $parameters |
||
47 | */ |
||
48 | private $parameters; |
||
49 | |||
50 | |||
51 | |||
52 | /** |
||
53 | * DependencyInjector constructor |
||
54 | * |
||
55 | * @param CoffeePotInterface $coffee_pot |
||
56 | * @param \EEH_Array $array_helper |
||
57 | */ |
||
58 | public function __construct(CoffeePotInterface $coffee_pot, \EEH_Array $array_helper) |
||
63 | |||
64 | |||
65 | |||
66 | /** |
||
67 | * getReflectionClass |
||
68 | * checks if a ReflectionClass object has already been generated for a class |
||
69 | * and returns that instead of creating a new one |
||
70 | * |
||
71 | * @param string $class_name |
||
72 | * @return \ReflectionClass |
||
73 | */ |
||
74 | View Code Duplication | public function getReflectionClass($class_name) |
|
84 | |||
85 | |||
86 | |||
87 | /** |
||
88 | * getConstructor |
||
89 | * checks if a ReflectionMethod object has already been generated for the class constructor |
||
90 | * and returns that instead of creating a new one |
||
91 | * |
||
92 | * @param \ReflectionClass $reflector |
||
93 | * @return \ReflectionMethod |
||
94 | */ |
||
95 | protected function getConstructor(\ReflectionClass $reflector) |
||
105 | |||
106 | |||
107 | |||
108 | /** |
||
109 | * getParameters |
||
110 | * checks if an array of ReflectionParameter objects has already been generated for the class constructor |
||
111 | * and returns that instead of creating a new one |
||
112 | * |
||
113 | * @param \ReflectionMethod $constructor |
||
114 | * @return \ReflectionParameter[] |
||
115 | */ |
||
116 | protected function getParameters(\ReflectionMethod $constructor) |
||
123 | |||
124 | |||
125 | |||
126 | /** |
||
127 | * resolveDependencies |
||
128 | * examines the constructor for the requested class to determine |
||
129 | * if any dependencies exist, and if they can be injected. |
||
130 | * If so, then those classes will be added to the array of arguments passed to the constructor |
||
131 | * PLZ NOTE: this is achieved by type hinting the constructor params |
||
132 | * For example: |
||
133 | * if attempting to load a class "Foo" with the following constructor: |
||
134 | * __construct( Bar $bar_class, Fighter $grohl_class ) |
||
135 | * then $bar_class and $grohl_class will be added to the $arguments array, |
||
136 | * but only IF they are NOT already present in the incoming arguments array, |
||
137 | * and the correct classes can be loaded |
||
138 | * |
||
139 | * @param RecipeInterface $recipe |
||
140 | * @param \ReflectionClass $reflector |
||
141 | * @param array $arguments |
||
142 | * @return array |
||
143 | */ |
||
144 | public function resolveDependencies(RecipeInterface $recipe, \ReflectionClass $reflector, $arguments = array()) |
||
210 | |||
211 | |||
212 | |||
213 | /** |
||
214 | * @param string $param_class |
||
215 | * @return mixed |
||
216 | */ |
||
217 | private function injectDependency($param_class) |
||
233 | |||
234 | } |
||
235 | // End of file DependencyInjector.php |
||
236 | // Location: /DependencyInjector.php |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.