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 |
||
| 8 | class Startup { |
||
| 9 | public static $urlParts; |
||
| 10 | private static $config; |
||
| 11 | private static $ctrlNS; |
||
| 12 | |||
| 13 | public static function run(array &$config, $url) { |
||
| 14 | @\set_exception_handler(array ('Startup','errorHandler' )); |
||
| 15 | self::$config=$config; |
||
| 16 | self::startTemplateEngine($config); |
||
| 17 | |||
| 18 | session_start(); |
||
| 19 | |||
| 20 | $u=self::parseUrl($url); |
||
| 21 | if (($ru=Router::getRoute($url)) !== false) { |
||
| 22 | if (\is_array($ru)) |
||
| 23 | self::runAction($ru); |
||
| 24 | else |
||
| 25 | echo $ru; |
||
| 26 | } else { |
||
| 27 | self::setCtrlNS($config); |
||
| 28 | $u[0]=self::$ctrlNS . $u[0]; |
||
| 29 | if (\class_exists($u[0])) { |
||
| 30 | self::runAction($u); |
||
| 31 | } else { |
||
| 32 | print "Le contrôleur `" . $u[0] . "` n'existe pas <br/>"; |
||
| 33 | } |
||
| 34 | } |
||
| 35 | } |
||
| 36 | |||
| 37 | public static function getNS($part="controllers"){ |
||
| 38 | $config=self::$config; |
||
| 39 | $ns=$config["mvcNS"][$part]; |
||
| 40 | if ($ns !== "" && $ns !== null) { |
||
| 41 | $ns.="\\"; |
||
| 42 | } |
||
| 43 | return $ns; |
||
| 44 | } |
||
| 45 | |||
| 46 | private static function setCtrlNS($config) { |
||
|
|
|||
| 47 | self::$ctrlNS=self::getNS(); |
||
| 48 | } |
||
| 49 | |||
| 50 | private static function parseUrl(&$url) { |
||
| 51 | if (!$url) { |
||
| 52 | $url="_default"; |
||
| 53 | } |
||
| 54 | View Code Duplication | if (StrUtils::endswith($url, "/")) |
|
| 55 | $url=\substr($url, 0, strlen($url) - 1); |
||
| 56 | self::$urlParts=\explode("/", $url); |
||
| 57 | |||
| 58 | return self::$urlParts; |
||
| 59 | } |
||
| 60 | |||
| 61 | private static function startTemplateEngine($config) { |
||
| 62 | try { |
||
| 63 | $engineOptions=array ('cache' => ROOT . DS . "views/cache/" ); |
||
| 64 | if (isset($config["templateEngine"])) { |
||
| 65 | $templateEngine=$config["templateEngine"]; |
||
| 66 | if (isset($config["templateEngineOptions"])) { |
||
| 67 | $engineOptions=$config["templateEngineOptions"]; |
||
| 68 | } |
||
| 69 | $engine=new $templateEngine($engineOptions); |
||
| 70 | if ($engine instanceof TemplateEngine) { |
||
| 71 | self::$config["templateEngine"]=$engine; |
||
| 72 | } |
||
| 73 | } |
||
| 74 | } catch ( \Exception $e ) { |
||
| 75 | echo $e->getTraceAsString(); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | public static function runAction($u, $initialize=true, $finalize=true) { |
||
| 80 | $config=self::getConfig(); |
||
| 81 | $ctrl=$u[0]; |
||
| 82 | $controller=new $ctrl(); |
||
| 83 | if (!$controller instanceof Controller) { |
||
| 84 | print "`{$u[0]}` n'est pas une instance de contrôleur.`<br/>"; |
||
| 85 | return; |
||
| 86 | } |
||
| 87 | // Dependency injection |
||
| 88 | if (\array_key_exists("di", $config)) { |
||
| 89 | $di=$config["di"]; |
||
| 90 | if (\is_array($di)) { |
||
| 91 | foreach ( $di as $k => $v ) { |
||
| 92 | $controller->$k=$v(); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | if ($initialize) |
||
| 98 | $controller->initialize(); |
||
| 99 | self::callController($controller, $u); |
||
| 100 | if ($finalize) |
||
| 101 | $controller->finalize(); |
||
| 102 | } |
||
| 103 | |||
| 104 | public static function runAsString($u, $initialize=true, $finalize=true) { |
||
| 105 | \ob_start(); |
||
| 106 | self::runAction($u, $initialize, $finalize); |
||
| 107 | return \ob_get_clean(); |
||
| 108 | } |
||
| 109 | |||
| 110 | private static function callController(Controller $controller, $u) { |
||
| 135 | |||
| 136 | public static function getConfig() { |
||
| 139 | |||
| 140 | public static function getModelsDir() { |
||
| 141 | return self::$config["mvcNS"]["models"]; |
||
| 142 | } |
||
| 143 | |||
| 144 | public static function getRealModelsDir() { |
||
| 145 | return \realpath(self::$config["siteUrl"]."/".self::$config["mvcNS"]["models"]); |
||
| 146 | } |
||
| 147 | |||
| 148 | public static function errorHandler($severity, $message, $filename, $lineno) { |
||
| 156 | } |
||
| 157 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.