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 |
||
| 6 | class Startup{ |
||
| 7 | public static $urlParts; |
||
| 8 | private static $config; |
||
| 9 | private static $ctrlNS; |
||
| 10 | |||
| 11 | public static function run(array &$config,$url){ |
||
| 12 | @set_exception_handler(array('Startup', 'errorHandler')); |
||
| 13 | self::$config=$config; |
||
| 14 | self::startTemplateEngine($config); |
||
| 15 | |||
| 16 | session_start(); |
||
| 17 | |||
| 18 | $u=self::parseUrl($config, $url); |
||
| 19 | self::setCtrlNS($config); |
||
| 20 | if(class_exists(self::$ctrlNS.$u[0]) && StrUtils::startswith($u[0],"_")===false){ |
||
| 21 | //Construction de l'instance de la classe (1er élément du tableau) |
||
| 22 | try{ |
||
| 23 | if(isset($config['onStartup'])){ |
||
| 24 | if(is_callable($config['onStartup'])){ |
||
| 25 | $config["onStartup"]($u); |
||
| 26 | } |
||
| 27 | } |
||
| 28 | self::runAction($u); |
||
| 29 | }catch (\Exception $e){ |
||
| 30 | print "Error!: " . $e->getMessage() . "<br/>"; |
||
| 31 | } |
||
| 32 | }else{ |
||
| 33 | print "Le contrôleur `".self::$ctrlNS.$u[0]."` n'existe pas <br/>"; |
||
| 34 | } |
||
| 35 | } |
||
| 36 | |||
| 37 | private static function setCtrlNS($config){ |
||
| 38 | $ns=$config["mvcNS"]["controllers"]; |
||
| 39 | if($ns!=="" && $ns!==null){ |
||
| 40 | $ns.="\\"; |
||
| 41 | } |
||
| 42 | self::$ctrlNS=$ns; |
||
| 43 | } |
||
| 44 | |||
| 45 | private static function parseUrl($config,$url){ |
||
| 55 | |||
| 56 | private static function startTemplateEngine($config){ |
||
| 73 | |||
| 74 | public static function runAction($u,$initialize=true,$finalize=true){ |
||
| 75 | $config=self::getConfig(); |
||
| 98 | |||
| 99 | private static function callController(Controller $controller,$u){ |
||
| 124 | |||
| 125 | public static function getConfig(){ |
||
| 128 | |||
| 129 | public static function errorHandler($severity, $message, $filename, $lineno) { |
||
| 137 | } |
||
| 138 |