Conditions | 11 |
Paths | 49 |
Total Lines | 51 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
52 | public static function run() |
||
53 | { |
||
54 | global $argv; |
||
|
|||
55 | $output = null; |
||
56 | if (!Obj::isArray($argv) || Str::likeEmpty($argv[1])) { |
||
57 | $output = 'Console command is unknown! Type "console main/help" to get help guide'; |
||
58 | } else { |
||
59 | $controller_action = $argv[1]; |
||
60 | $arrInput = explode('/', $controller_action); |
||
61 | $controller = ucfirst(strtolower($arrInput[0])); |
||
62 | $action = ucfirst(strtolower($arrInput[1])); |
||
63 | if($action == null) { |
||
64 | $action = 'Index'; |
||
65 | } |
||
66 | // set action and id |
||
67 | $action = 'action' . $action; |
||
68 | $id = null; |
||
69 | if (isset($argv[2])) { |
||
70 | $id = $argv[2]; |
||
71 | } |
||
72 | |||
73 | try { |
||
74 | $controller_path = '/Apps/Controller/' . env_name . '/' . $controller . '.php'; |
||
75 | if(file_exists(root . $controller_path) && is_readable(root . $controller_path)) { |
||
76 | include_once(root . $controller_path); |
||
77 | $cname = 'Apps\Controller\\' . env_name . '\\' . $controller; |
||
78 | if(class_exists($cname)) { |
||
79 | $load = new $cname; |
||
80 | if(method_exists($cname, $action)) { |
||
81 | if($id !== null) { |
||
82 | $output = @$load->$action($id); |
||
83 | } else { |
||
84 | $output = @$load->$action(); |
||
85 | } |
||
86 | } else { |
||
87 | throw new NativeException('Method ' . $action . '() not founded in ' . $cname . ' in file {root}' . $controller_path); |
||
88 | } |
||
89 | unset($load); |
||
90 | } else { |
||
91 | throw new NativeException('Namespace\\Class - ' . $cname . ' not founded in {root}' . $controller_path); |
||
92 | } |
||
93 | } else { |
||
94 | throw new NativeException('Controller not founded: {root}' . $controller_path); |
||
95 | } |
||
96 | } catch(NativeException $e) { |
||
97 | $e->display($e->getMessage()); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | return self::$Output->write($output); |
||
102 | } |
||
103 | |||
104 | } |
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state