Conditions | 27 |
Paths | 44 |
Total Lines | 106 |
Lines | 21 |
Ratio | 19.81 % |
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 |
||
34 | public function cmdHelp($l) { |
||
|
|||
35 | if ("? " == substr($l, 0, strlen("? "))) { |
||
36 | $str = substr($l, 2); |
||
37 | |||
38 | $cmd = ''; |
||
39 | |||
40 | if (preg_match('#^([A-Za-z0-9_]+)::([a-zA-Z0-9_]+)\(\s*\)\s*#', $str, $a)) { |
||
41 | /* ? Class::method() */ |
||
42 | |||
43 | $class = $a[1]; |
||
44 | $method = $a[2]; |
||
45 | |||
46 | if (false !== ($proto = PHP_ShellPrototypes::getInstance()->get($class.'::'.$method))) { |
||
47 | |||
48 | $cmd = sprintf("/**\n* %s\n\n* @params %s\n* @return %s\n*/\n", |
||
49 | $proto['description'], |
||
50 | $proto['params'], |
||
51 | $proto['return'] |
||
52 | ); |
||
53 | } else if (class_exists($class, false)) { |
||
54 | $c = new ReflectionClass($class); |
||
55 | |||
56 | if ($c->hasMethod($method)) { |
||
57 | $cmd = $c->getMethod($method)->getDocComment(); |
||
58 | } |
||
59 | } |
||
60 | } else if (preg_match('#^\$([A-Za-z0-9_]+)->([a-zA-Z0-9_]+)\(\s*\)\s*#', $str, $a)) { |
||
61 | /* ? $obj->method() */ |
||
62 | View Code Duplication | if (isset($GLOBALS[$a[1]]) && is_object($GLOBALS[$a[1]])) { |
|
63 | $class = get_class($GLOBALS[$a[1]]); |
||
64 | $method = $a[2]; |
||
65 | |||
66 | $c = new ReflectionClass($class); |
||
67 | |||
68 | if ($c->hasMethod($method)) { |
||
69 | $cmd = $c->getMethod($method)->getDocComment(); |
||
70 | } |
||
71 | } |
||
72 | } else if (preg_match('#^([A-Za-z0-9_]+)::([a-zA-Z0-9_]+)\s*$#', $str, $a)) { |
||
73 | /* ? Class::property */ |
||
74 | $class = $a[1]; |
||
75 | $property = $a[2]; |
||
76 | if (class_exists($class, false)) { |
||
77 | $c = new ReflectionClass($class); |
||
78 | |||
79 | if ($c->hasProperty($property)) { |
||
80 | $cmd = $c->getProperty($property)->getDocComment(); |
||
81 | } |
||
82 | } |
||
83 | } else if (preg_match('#^\$([A-Za-z0-9_]+)->([a-zA-Z0-9_]+)\s*$#', $str, $a)) { |
||
84 | /* ? $obj->property */ |
||
85 | View Code Duplication | if (isset($GLOBALS[$a[1]]) && is_object($GLOBALS[$a[1]])) { |
|
86 | $class = get_class($GLOBALS[$a[1]]); |
||
87 | $method = $a[2]; |
||
88 | |||
89 | $c = new ReflectionClass($class); |
||
90 | |||
91 | if ($c->hasProperty($property)) { |
||
92 | $cmd = $c->getProperty($property)->getDocComment(); |
||
93 | } |
||
94 | |||
95 | } |
||
96 | } else if (preg_match('#^([A-Za-z0-9_]+)$#', $str, $a)) { |
||
97 | /* ? Class */ |
||
98 | if (class_exists($a[1], false)) { |
||
99 | $c = new ReflectionClass($a[1]); |
||
100 | $cmd = $c->getDocComment(); |
||
101 | } |
||
102 | } else if (preg_match('#^\$([A-Za-z0-9_]+)$#', $str, $a)) { |
||
103 | /* ? $object */ |
||
104 | $obj = $a[1]; |
||
105 | if (isset($GLOBALS[$obj]) && is_object($GLOBALS[$obj])) { |
||
106 | $class = get_class($GLOBALS[$obj]); |
||
107 | |||
108 | $c = new ReflectionClass($class); |
||
109 | $cmd = $c->getDocComment(); |
||
110 | } |
||
111 | |||
112 | } else if (preg_match('#^([A-Za-z0-9_]+)\(\s*\)$#', $str, $a)) { |
||
113 | /* ? function() */ |
||
114 | $func = $a[1]; |
||
115 | |||
116 | if (false !== ($proto = PHP_ShellPrototypes::getInstance()->get($func))) { |
||
117 | $cmd = sprintf("/**\n* %s\n*\n* @params %s\n* @return %s\n*/\n", |
||
118 | $proto['description'], |
||
119 | $proto['params'], |
||
120 | $proto['return'] |
||
121 | ); |
||
122 | } else if (function_exists($func)) { |
||
123 | $c = new ReflectionFunction($func); |
||
124 | $cmd = $c->getDocComment(); |
||
125 | } |
||
126 | } |
||
127 | |||
128 | if ($cmd == '') { |
||
129 | $cmd = var_export(sprintf('no help found for \'%s\'', $str), 1); |
||
130 | } else { |
||
131 | $cmd = var_export($cmd, 1); |
||
132 | } |
||
133 | } else if ("?" == $l) { |
||
134 | $cmd = $this->getHelp(); |
||
135 | $cmd = var_export($cmd, 1); |
||
136 | } |
||
137 | |||
138 | return $cmd; |
||
139 | } |
||
140 | } |
||
141 |
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: