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 ControllerAction { |
||
| 12 | private $controller; |
||
| 13 | private $action; |
||
| 14 | private $parameters; |
||
| 15 | private $dValues; |
||
| 16 | private $annots; |
||
| 17 | |||
| 18 | private static $excludeds=["__construct","isValid","initialize","finalize","onInvalidControl","loadView","forward"]; |
||
| 19 | |||
| 20 | |||
| 21 | public function __construct($controller="",$action="",$parameters=[],$dValues=[],$annots=[]){ |
||
| 22 | $this->controller=$controller; |
||
| 23 | $this->action=$action; |
||
| 24 | $this->parameters=$parameters; |
||
| 25 | $this->dValues=$dValues; |
||
| 26 | $this->annots=$annots; |
||
| 27 | } |
||
| 28 | |||
| 29 | public static function initWithPath($url){ |
||
| 30 | $result=[]; |
||
| 31 | $config=Startup::getConfig(); |
||
| 32 | $ns=$config["mvcNS"]["controllers"]; |
||
| 33 | if ($ns !== "" && $ns !== null) { |
||
| 34 | $ns.="\\"; |
||
| 35 | } |
||
| 36 | if (!$url) { |
||
| 37 | $url="_default"; |
||
| 38 | } |
||
| 39 | if (StrUtils::endswith($url, "/")) |
||
| 40 | $url=\substr($url, 0, strlen($url) - 1); |
||
| 41 | $u=\explode("/", $url); |
||
| 42 | $u[0]=$ns . $u[0]; |
||
| 43 | if(\class_exists($u[0])){ |
||
| 44 | $controllerClass=$u[0]; |
||
| 45 | if(\count($u)<2) |
||
| 46 | $u[]="index"; |
||
| 47 | if(\method_exists($controllerClass, $u[1])){ |
||
| 48 | $method=new \ReflectionMethod($u[0],$u[1]); |
||
| 49 | $r=self::scanMethod($controllerClass, $method); |
||
| 50 | if(isset($r)) |
||
| 51 | $result[]=$r; |
||
| 52 | } |
||
| 53 | } |
||
| 54 | return $result; |
||
| 55 | } |
||
| 56 | public static function init(){ |
||
| 57 | $result=[]; |
||
| 58 | $config=Startup::getConfig(); |
||
| 59 | |||
| 60 | $files=CacheManager::getControllerFiles($config,true); |
||
| 61 | foreach ( $files as $file ) { |
||
| 62 | if (is_file($file)) { |
||
| 63 | $controllerClass=ClassUtils::getClassFullNameFromFile($file); |
||
| 64 | $reflect=new \ReflectionClass($controllerClass); |
||
| 65 | if (!$reflect->isAbstract() && $reflect->isSubclassOf("micro\controllers\Controller")) { |
||
| 66 | $methods=$reflect->getMethods(\ReflectionMethod::IS_PUBLIC); |
||
| 67 | foreach ( $methods as $method ) { |
||
| 68 | $r=self::scanMethod($controllerClass, $method); |
||
| 69 | if(isset($r)) |
||
| 70 | $result[]=$r; |
||
| 71 | } |
||
| 72 | } |
||
| 73 | } |
||
| 74 | } |
||
| 75 | return $result; |
||
| 76 | } |
||
| 77 | |||
| 78 | private static function scanMethod($controllerClass,\ReflectionMethod $method){ |
||
| 79 | $result=null; |
||
| 80 | if(\array_search($method->name, self::$excludeds)===false && !StrUtils::startswith($method->name, "_")){ |
||
| 81 | $annots=Router::getAnnotations($controllerClass, $method->name); |
||
| 82 | $parameters=$method->getParameters(); |
||
| 83 | $defaults=[]; |
||
| 84 | foreach ($parameters as $param){ |
||
| 85 | if($param->isOptional()){ |
||
| 86 | $defaults[$param->name]=$param->getDefaultValue(); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | $result=new ControllerAction($controllerClass,$method->name,$parameters,$defaults,$annots); |
||
| 90 | } |
||
| 91 | return $result; |
||
| 92 | } |
||
| 93 | |||
| 94 | public function getController() { |
||
| 95 | return $this->controller; |
||
| 96 | } |
||
| 97 | |||
| 98 | public function setController($controller) { |
||
| 99 | $this->controller=$controller; |
||
| 100 | return $this; |
||
| 101 | } |
||
| 102 | |||
| 103 | public function getAction() { |
||
| 104 | return $this->action; |
||
| 105 | } |
||
| 106 | |||
| 107 | public function setAction($action) { |
||
| 108 | $this->action=$action; |
||
| 109 | return $this; |
||
| 110 | } |
||
| 111 | |||
| 112 | public function getParameters() { |
||
| 113 | return $this->parameters; |
||
| 114 | } |
||
| 115 | |||
| 116 | public function setParameters($parameters) { |
||
| 117 | $this->parameters=$parameters; |
||
| 118 | return $this; |
||
| 119 | } |
||
| 120 | |||
| 121 | public function getDValues() { |
||
| 122 | return $this->dValues; |
||
| 123 | } |
||
| 124 | |||
| 125 | public function setDValues($dValues) { |
||
| 126 | $this->dValues=$dValues; |
||
| 127 | return $this; |
||
| 128 | } |
||
| 129 | |||
| 130 | public function getAnnots() { |
||
| 131 | return $this->annots; |
||
| 132 | } |
||
| 133 | |||
| 134 | public function setAnnots($annots) { |
||
| 135 | $this->annots=$annots; |
||
| 136 | return $this; |
||
| 137 | } |
||
| 138 | |||
| 139 | public function getPath(){ |
||
| 144 |