| Conditions | 17 |
| Paths | 97 |
| Total Lines | 34 |
| Code Lines | 26 |
| 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 |
||
| 22 | public function createInstance() |
||
| 23 | { |
||
| 24 | switch ($this->method) { |
||
| 25 | case 'addBigIntegerColumn': |
||
| 26 | case 'addBinaryColumn': |
||
| 27 | case 'addBooleanColumn': |
||
| 28 | case 'addDateColumn': |
||
| 29 | case 'addDateTimeColumn': |
||
| 30 | case 'addDecimalColumn': |
||
| 31 | case 'addFloatColumn': |
||
| 32 | case 'addIntegerColumn': |
||
| 33 | case 'addSmallIntegerColumn': |
||
| 34 | case 'addStringColumn': |
||
| 35 | case 'addTextColumn': |
||
| 36 | case 'addTimeColumn': |
||
| 37 | $namespace = 'Rentgen\\Database\\Column\\'; |
||
| 38 | $class = $namespace . ltrim($this->method, 'add'); |
||
| 39 | $options = isset($this->params[1]) ? $this->params[1] : array(); |
||
| 40 | |||
| 41 | if ('addStringColumn' === $this->method && !isset($options['limit'])) { |
||
| 42 | $options['limit'] = 255; |
||
| 43 | } |
||
| 44 | $column = new $class($this->params[0], $options); |
||
| 45 | |||
| 46 | if (isset($options['comment'])) { |
||
| 47 | $column->setDescription($options['comment']); |
||
| 48 | } |
||
| 49 | break; |
||
| 50 | default: |
||
| 51 | throw new \Exception(sprintf("Unsupported method " . $this->method)); |
||
| 52 | } |
||
| 53 | |||
| 54 | return $column; |
||
| 55 | } |
||
| 56 | } |
||
| 57 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: