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 |
||
29 | class ContainerBuilder |
||
30 | { |
||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | private $containerClass; |
||
35 | |||
36 | /** |
||
37 | * @var bool |
||
38 | */ |
||
39 | private $useAutowiring = true; |
||
40 | |||
41 | /** |
||
42 | * @var bool |
||
43 | */ |
||
44 | private $useAnnotation = false; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | private $definitionSources = []; |
||
50 | |||
51 | /** |
||
52 | * @var ContainerInterface |
||
53 | */ |
||
54 | private $delegateContainer; |
||
55 | |||
56 | /** |
||
57 | * ContainerBuilder constructor. |
||
58 | * |
||
59 | * @param string $containerClass |
||
60 | */ |
||
61 | public function __construct($containerClass = 'Stack\DI\Container') |
||
65 | |||
66 | /** |
||
67 | * Add definitions to the container. |
||
68 | * |
||
69 | * @param array $definitions |
||
70 | * |
||
71 | * @throws \InvalidArgumentException |
||
72 | * |
||
73 | * @return $this |
||
74 | */ |
||
75 | public function addDefinitions($definitions) |
||
89 | |||
90 | /** |
||
91 | * Build and return a container. |
||
92 | * |
||
93 | * @return Container |
||
94 | */ |
||
95 | public function build() |
||
108 | |||
109 | /** |
||
110 | * Build a container configured for the dev environment. |
||
111 | * |
||
112 | * @return Container |
||
113 | */ |
||
114 | public static function buildDevContainer() |
||
120 | |||
121 | /** |
||
122 | * Enable or disable the use of autowiring to guess injections. |
||
123 | * |
||
124 | * Enabled by default. |
||
125 | * |
||
126 | * @param $bool |
||
127 | * |
||
128 | * @return $this |
||
129 | */ |
||
130 | public function useAutowiring($bool) |
||
136 | |||
137 | /** |
||
138 | * Enable or disable the use of annotations to guess injections. |
||
139 | * |
||
140 | * Disabled by default. |
||
141 | * |
||
142 | * @param $bool |
||
143 | * |
||
144 | * @return $this |
||
145 | */ |
||
146 | public function useAnnotation($bool) |
||
152 | |||
153 | /** |
||
154 | * Delegate the container for dependencies. |
||
155 | * |
||
156 | * @param ContainerInterface $delegateContainer |
||
157 | * |
||
158 | * @return $this |
||
159 | */ |
||
160 | public function setDelegateContainer(ContainerInterface $delegateContainer) |
||
166 | } |
||
167 |
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.