| Conditions | 10 |
| Paths | 132 |
| Total Lines | 46 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 19 | static public function dispatch($path='',$app='\\admin') { |
||
| 20 | self::init(); |
||
| 21 | if($path==''){ |
||
| 22 | $path=array(); |
||
| 23 | }else{ |
||
| 24 | $path=str_replace('-','_',$path); |
||
| 25 | $path = explode('/',$path); |
||
| 26 | } |
||
| 27 | |||
| 28 | if(count($path)==0){ |
||
| 29 | array_push($path,'home'); |
||
| 30 | array_push($path,'index'); |
||
| 31 | } |
||
| 32 | elseif (count($path)==1){ |
||
| 33 | array_push($path,'index'); |
||
| 34 | } |
||
| 35 | if(!empty($path)){ |
||
| 36 | $tmpAction=array_pop($path); |
||
| 37 | $tmpAction=preg_replace('/\.(html|aspx|do|php|htm|h5|api)$/i', '', $tmpAction); |
||
| 38 | $tmpAction=parse_name($tmpAction,1); |
||
| 39 | $var['a']=$tmpAction; |
||
| 40 | } |
||
| 41 | define('ACTION_NAME',$var['a']); |
||
| 42 | if (!preg_match('/^[A-Za-z](\w)*$/', ACTION_NAME)) { |
||
| 43 | die("error action"); |
||
| 44 | } |
||
| 45 | if(!empty($path)){ |
||
| 46 | $tmpController=array_pop($path); |
||
| 47 | $tmpController=parse_name($tmpController,1); |
||
| 48 | $var['c']=$tmpController; |
||
| 49 | } |
||
| 50 | define('CONTROLLER_NAME',$var['c']); |
||
| 51 | if (!preg_match('/^[A-Za-z](\/|\w)*$/', CONTROLLER_NAME)) { |
||
| 52 | die("error controller"); |
||
| 53 | } |
||
| 54 | $class=$app.'\\controllers\\'.ucfirst(CONTROLLER_NAME); |
||
| 55 | if (!class_exists($class)) { |
||
| 56 | not_found('this controller is can not work now!'); |
||
| 57 | } |
||
| 58 | $class= new $class(); |
||
| 59 | if (!method_exists($class,ACTION_NAME)) { |
||
| 60 | not_found(); |
||
| 61 | } |
||
| 62 | self::param(); |
||
| 63 | self::exec($class,ACTION_NAME); |
||
| 64 | } |
||
| 65 | static public function exec($class,$function){ |
||
| 100 | } |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: