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 | abstract class CoffeeMaker implements CoffeeMakerInterface |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * Indicates that CoffeeMaker should construct a NEW entity instance from the provided arguments (if given) |
||
28 | */ |
||
29 | const BREW_NEW = 'new'; |
||
30 | |||
31 | /** |
||
32 | * Indicates that CoffeeMaker should always return a SHARED instance |
||
33 | */ |
||
34 | const BREW_SHARED = 'shared'; |
||
35 | |||
36 | /** |
||
37 | * Indicates that CoffeeMaker should only load the file/class/interface but NOT instantiate |
||
38 | */ |
||
39 | const BREW_LOAD_ONLY = 'load_only'; |
||
40 | |||
41 | |||
42 | /** |
||
43 | * @var CoffeePotInterface $coffee_pot |
||
44 | */ |
||
45 | private $coffee_pot; |
||
46 | |||
47 | /** |
||
48 | * @var DependencyInjector $injector |
||
49 | */ |
||
50 | private $injector; |
||
51 | |||
52 | |||
53 | |||
54 | /** |
||
55 | * @return array |
||
56 | */ |
||
57 | public static function getTypes() |
||
68 | |||
69 | |||
70 | |||
71 | /** |
||
72 | * @param $type |
||
73 | */ |
||
74 | View Code Duplication | public static function validateType($type) |
|
|
|||
75 | { |
||
76 | $types = CoffeeMaker::getTypes(); |
||
77 | if ( ! in_array($type, $types)) { |
||
78 | throw new InvalidIdentifierException( |
||
79 | is_object($type) ? get_class($type) : gettype($type), |
||
80 | __( |
||
81 | 'recipe type (one of the class constants on \EventEspresso\core\services\container\CoffeeMaker)', |
||
82 | 'event_espresso' |
||
83 | ) |
||
84 | ); |
||
85 | } |
||
86 | return $type; |
||
87 | } |
||
88 | |||
89 | |||
90 | |||
91 | /** |
||
92 | * CoffeeMaker constructor. |
||
93 | * |
||
94 | * @param CoffeePotInterface $coffee_pot |
||
95 | * @param InjectorInterface $injector |
||
96 | */ |
||
97 | public function __construct(CoffeePotInterface $coffee_pot, InjectorInterface $injector) |
||
102 | |||
103 | |||
104 | |||
105 | /** |
||
106 | * @return \EventEspresso\core\services\container\CoffeePotInterface |
||
107 | */ |
||
108 | protected function coffeePot() |
||
112 | |||
113 | |||
114 | |||
115 | /** |
||
116 | * @return \EventEspresso\core\services\container\DependencyInjector |
||
117 | */ |
||
118 | protected function injector() |
||
122 | |||
123 | |||
124 | |||
125 | /** |
||
126 | * Examines the constructor to determine which method should be used for instantiation |
||
127 | * |
||
128 | * @param \ReflectionClass $reflector |
||
129 | * @return mixed |
||
130 | */ |
||
131 | protected function resolveInstantiationMethod(\ReflectionClass $reflector) |
||
147 | |||
148 | |||
149 | |||
150 | /** |
||
151 | * Ensures files for classes that are not PSR-4 compatible are loaded |
||
152 | * and then verifies that classes exist where applicable |
||
153 | * |
||
154 | * @param RecipeInterface $recipe |
||
155 | * @throws InvalidClassException |
||
156 | */ |
||
157 | protected function resolveClassAndFilepath(RecipeInterface $recipe) |
||
178 | |||
179 | |||
180 | |||
181 | |||
182 | } |
||
183 | // End of file CoffeeMaker.php |
||
184 | // Location: /CoffeeMaker.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.