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 |
||
10 | class Errors |
||
11 | { |
||
12 | /** |
||
13 | * @var \BFW\Application $app : L'instance d'Application |
||
14 | */ |
||
15 | protected static $app = null; |
||
16 | |||
17 | /** |
||
18 | * Constructeur |
||
19 | * |
||
20 | * @param \BFW\Application $app : L'instance d'Application à utiliser |
||
21 | */ |
||
22 | public function __construct(Application $app) |
||
32 | |||
33 | /** |
||
34 | * Find and create the handler for errors |
||
35 | * |
||
36 | * @return void |
||
37 | */ |
||
38 | protected function defineErrorHandler() |
||
52 | |||
53 | /** |
||
54 | * Find and create the handler for exceptions |
||
55 | * |
||
56 | * @return type |
||
57 | */ |
||
58 | protected function defineExceptionHandler() |
||
72 | |||
73 | /** |
||
74 | * Get the Application instance |
||
75 | * It's a method to allow override |
||
76 | * |
||
77 | * @return \BFW\Application |
||
78 | */ |
||
79 | protected static function getApp() |
||
87 | |||
88 | /** |
||
89 | * get the error render from config for cli or default |
||
90 | * |
||
91 | * @return boolean|array Render infos |
||
92 | * Boolean : false if no render to use |
||
93 | * Array : Infos from config |
||
94 | */ |
||
95 | View Code Duplication | public static function getErrorRender() |
|
103 | |||
104 | /** |
||
105 | * get the exception render from config for cli or default |
||
106 | * |
||
107 | * @return boolean|array Render infos |
||
108 | * Boolean : false if no render to use |
||
109 | * Array : Infos from config |
||
110 | */ |
||
111 | View Code Duplication | public static function getExceptionRender() |
|
119 | |||
120 | /** |
||
121 | * Find the render to use with the config |
||
122 | * If cli render is not define, it's use the default render. |
||
123 | * |
||
124 | * @param array $renderConfig : Render infos from config |
||
125 | * |
||
126 | * @return boolean|array : Render to use |
||
127 | * Boolean : false is no enable or no render define |
||
128 | * Array : The render used |
||
129 | */ |
||
130 | protected static function defineRenderToUse($renderConfig) |
||
149 | |||
150 | /** |
||
151 | * The default exception handler included in BFW |
||
152 | * |
||
153 | * @param \Exception $exception : Exception informations |
||
154 | * |
||
155 | * @return void |
||
156 | */ |
||
157 | public static function exceptionHandler($exception) |
||
173 | |||
174 | /** |
||
175 | * The default error handler included in BFW |
||
176 | * |
||
177 | * @param type $errSeverity : Error severity |
||
178 | * @param type $errMsg : Error message |
||
179 | * @param type $errFile : File where the error is triggered |
||
180 | * @param type $errLine : Line where the error is triggered |
||
181 | * |
||
182 | * @return void |
||
183 | */ |
||
184 | public static function errorHandler( |
||
205 | |||
206 | /** |
||
207 | * Call the personnal class-method or function declared on config where |
||
208 | * an exception or an error is triggered. |
||
209 | * |
||
210 | * @param array $renderInfos : Infos from config |
||
211 | * @param type $erreurType : Error severity |
||
212 | * @param type $errMsg : Error/exception message |
||
213 | * @param type $errFile : File where the error/exception is triggered |
||
214 | * @param type $errLine : Line where the error/exception is triggered |
||
215 | * @param type $backtrace : Error/exception backtrace |
||
216 | * |
||
217 | * @return void |
||
218 | */ |
||
219 | protected static function callRender( |
||
258 | |||
259 | /** |
||
260 | * Map array to have a human readable severity. |
||
261 | * |
||
262 | * @see http://fr2.php.net/manual/fr/function.set-error-handler.php#113567 |
||
263 | * |
||
264 | * @param int $errSeverity : The error severity with PHP constant |
||
265 | * |
||
266 | * @return string |
||
267 | */ |
||
268 | protected static function getErrorType($errSeverity) |
||
298 | |||
299 | /** |
||
300 | * The default cli render in BFW |
||
301 | * |
||
302 | * @param type $erreurType : Error severity |
||
303 | * @param type $errMsg : Error/exception message |
||
304 | * @param type $errFile : File where the error/exception is triggered |
||
305 | * @param type $errLine : Line where the error/exception is triggered |
||
306 | * @param type $backtrace : Error/exception backtrace |
||
307 | * |
||
308 | * @return void |
||
309 | */ |
||
310 | public static function defaultCliErrorRender( |
||
328 | |||
329 | /** |
||
330 | * The default render in BFW |
||
331 | * |
||
332 | * @param type $erreurType : Error severity |
||
333 | * @param type $errMsg : Error/exception message |
||
334 | * @param type $errFile : File where the error/exception is triggered |
||
335 | * @param type $errLine : Line where the error/exception is triggered |
||
336 | * @param type $backtrace : Error/exception backtrace |
||
337 | * |
||
338 | * @return void |
||
339 | */ |
||
340 | public static function defaultErrorRender( |
||
406 | } |
||
407 |
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.