| Conditions | 17 |
| Paths | 361 |
| Total Lines | 76 |
| Code Lines | 52 |
| 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 |
||
| 62 | public function main() { |
||
| 63 | if (empty($this->args)) { |
||
| 64 | return $this->out($this->OptionParser->help()); |
||
| 65 | } |
||
| 66 | |||
| 67 | $type = strtolower($this->args[0]); |
||
| 68 | |||
| 69 | if (isset($this->paths[$type])) { |
||
| 70 | $path = $this->paths[$type]; |
||
| 71 | } else { |
||
| 72 | $path = $this->paths['core']; |
||
| 73 | } |
||
| 74 | |||
| 75 | $count = count($this->args); |
||
| 76 | if ($count > 1) { |
||
| 77 | $file = Inflector::underscore($this->args[1]); |
||
| 78 | $class = Inflector::camelize($this->args[1]); |
||
| 79 | } elseif ($count) { |
||
| 80 | $file = $type; |
||
| 81 | $class = Inflector::camelize($type); |
||
| 82 | } |
||
| 83 | $objects = App::objects('class', $path); |
||
| 84 | if (in_array($class, $objects)) { |
||
| 85 | if (in_array($type, array('behavior', 'component', 'helper')) && $type !== $file) { |
||
|
|
|||
| 86 | if (!preg_match('/' . Inflector::camelize($type) . '$/', $class)) { |
||
| 87 | $class .= Inflector::camelize($type); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | } else { |
||
| 92 | $this->error(__d('cake_console', '%s not found', $class)); |
||
| 93 | } |
||
| 94 | |||
| 95 | $parsed = $this->_parseClass($path . $class . '.php', $class); |
||
| 96 | |||
| 97 | if (!empty($parsed)) { |
||
| 98 | if (isset($this->params['method'])) { |
||
| 99 | if (!isset($parsed[$this->params['method']])) { |
||
| 100 | $this->err(__d('cake_console', '%s::%s() could not be found', $class, $this->params['method'])); |
||
| 101 | return $this->_stop(); |
||
| 102 | } |
||
| 103 | $method = $parsed[$this->params['method']]; |
||
| 104 | $this->out($class . '::' . $method['method'] . $method['parameters']); |
||
| 105 | $this->hr(); |
||
| 106 | $this->out($method['comment'], true); |
||
| 107 | } else { |
||
| 108 | $this->out(ucwords($class)); |
||
| 109 | $this->hr(); |
||
| 110 | $i = 0; |
||
| 111 | foreach ($parsed as $method) { |
||
| 112 | $list[] = ++$i . ". " . $method['method'] . $method['parameters']; |
||
| 113 | } |
||
| 114 | $this->out($list); |
||
| 115 | |||
| 116 | $methods = array_keys($parsed); |
||
| 117 | while ($number = strtolower($this->in(__d('cake_console', 'Select a number to see the more information about a specific method. q to quit. l to list.'), null, 'q'))) { |
||
| 118 | if ($number === 'q') { |
||
| 119 | $this->out(__d('cake_console', 'Done')); |
||
| 120 | return $this->_stop(); |
||
| 121 | } |
||
| 122 | |||
| 123 | if ($number === 'l') { |
||
| 124 | $this->out($list); |
||
| 125 | } |
||
| 126 | |||
| 127 | if (isset($methods[--$number])) { |
||
| 128 | $method = $parsed[$methods[$number]]; |
||
| 129 | $this->hr(); |
||
| 130 | $this->out($class . '::' . $method['method'] . $method['parameters']); |
||
| 131 | $this->hr(); |
||
| 132 | $this->out($method['comment'], true); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | } |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 239 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: