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 |
||
11 | class CIPHPUnitTest |
||
12 | { |
||
13 | private static $loader_class = 'CI_Loader'; |
||
14 | private static $config_class = 'CI_Config'; |
||
15 | private static $controller_class; |
||
16 | private static $autoload_dirs; |
||
17 | |||
18 | /** |
||
19 | * Initialize CIPHPUnitTest |
||
20 | * |
||
21 | * @param array $autoload_dirs directories to search class file for autoloader |
||
22 | * |
||
23 | * Exclude from code coverage: This is test suite bootstrap code, so we |
||
24 | * know it's executed, but because it's bootstrap code, it runs outside of |
||
25 | * any coverage tracking. |
||
26 | * |
||
27 | * @codeCoverageIgnore |
||
28 | */ |
||
29 | public static function init(array $autoload_dirs = null) |
||
120 | |||
121 | /** |
||
122 | * @param bool $use_my_controller |
||
123 | */ |
||
124 | public static function createCodeIgniterInstance($use_my_controller = false) |
||
125 | { |
||
126 | if (! self::wiredesignzHmvcInstalled()) |
||
127 | { |
||
128 | if ($use_my_controller && self::hasMyController()) |
||
129 | { |
||
130 | new self::$controller_class; |
||
131 | } |
||
132 | else |
||
133 | { |
||
134 | new CI_Controller(); |
||
135 | } |
||
136 | } |
||
137 | else |
||
138 | { |
||
139 | new CI(); |
||
140 | new MX_Controller(); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | private static function hasMyController() |
||
145 | { |
||
146 | if (self::$controller_class !== null) { |
||
147 | return self::$controller_class !== 'CI_Controller'; |
||
148 | } |
||
149 | |||
150 | $my_controller_file = |
||
151 | APPPATH . 'core/' . config_item('subclass_prefix') . 'Controller.php'; |
||
152 | |||
153 | if (file_exists($my_controller_file)) |
||
154 | { |
||
155 | $controller_class = config_item('subclass_prefix') . 'Controller'; |
||
156 | if ( ! class_exists($controller_class)) |
||
157 | { |
||
158 | require $my_controller_file; |
||
159 | } |
||
160 | |||
161 | self::$controller_class = $controller_class; |
||
162 | return true; |
||
163 | } |
||
164 | |||
165 | self::$controller_class = 'CI_Controller'; |
||
166 | return false; |
||
167 | } |
||
168 | |||
169 | public static function wiredesignzHmvcInstalled() |
||
170 | { |
||
171 | if (file_exists(APPPATH.'third_party/MX')) |
||
172 | { |
||
173 | return true; |
||
174 | } |
||
175 | |||
176 | return false; |
||
177 | } |
||
178 | |||
179 | public static function getAutoloadDirs() |
||
180 | { |
||
181 | return self::$autoload_dirs; |
||
182 | } |
||
183 | |||
184 | View Code Duplication | protected static function replaceLoader() |
|
|
|||
185 | { |
||
186 | $my_loader_file = |
||
187 | APPPATH . 'core/' . config_item('subclass_prefix') . 'Loader.php'; |
||
188 | |||
189 | if (file_exists($my_loader_file)) |
||
190 | { |
||
191 | self::$loader_class = config_item('subclass_prefix') . 'Loader'; |
||
192 | if ( ! class_exists(self::$loader_class)) |
||
193 | { |
||
194 | require $my_loader_file; |
||
195 | } |
||
196 | } |
||
197 | self::loadLoader(); |
||
198 | } |
||
199 | |||
200 | View Code Duplication | protected static function replaceConfig() |
|
215 | |||
216 | protected static function replaceHelpers() |
||
223 | |||
224 | protected static function loadHelper($helper) |
||
233 | |||
234 | public static function setPatcherCacheDir($dir = null) |
||
245 | |||
246 | public static function loadLoader() |
||
251 | |||
252 | public static function loadConfig() |
||
253 | { |
||
254 | $config= new self::$config_class; |
||
255 | load_class_instance('Config', $config); |
||
256 | } |
||
257 | } |
||
258 |
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.