| Conditions | 14 |
| Paths | 25 |
| Total Lines | 55 |
| Code Lines | 33 |
| 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 declare(strict_types=1); |
||
| 61 | private function sqlColumns(): string |
||
| 62 | { |
||
| 63 | // no columns givens select all (SELECT *) |
||
| 64 | if (!count($this->columns) > 0 ) { |
||
| 65 | return '*'; |
||
| 66 | } |
||
| 67 | |||
| 68 | // use DISTINCT ? |
||
| 69 | $sqlDistinct = $this->distinct ? 'DISTINCT ': ''; |
||
| 70 | |||
| 71 | // parse columns |
||
| 72 | $colsList = array(); |
||
| 73 | foreach ($this->columns as $val){ |
||
| 74 | switch ($val['type']){ |
||
| 75 | |||
| 76 | // 'classic' column |
||
| 77 | case 'column': |
||
| 78 | $name = $this->escape($val['name']); |
||
| 79 | $alias = isset($val['alias']) ? 'AS '. $this->escape($val['alias']) : ''; |
||
| 80 | $colsList[] = trim(sprintf('%s %s', $name, $alias)); |
||
| 81 | break; |
||
| 82 | |||
| 83 | // COUNT() column |
||
| 84 | case 'count': |
||
| 85 | $colsList[] = trim(sprintf('COUNT(*) AS %s', $this->escape($val['alias']))); |
||
| 86 | break; |
||
| 87 | |||
| 88 | // SUM() column |
||
| 89 | case 'sum': |
||
| 90 | $name = $this->escape($val['name']); |
||
| 91 | $alias = isset($val['alias']) ? ' AS '. $this->escape($val['alias']) : ''; |
||
| 92 | $colsList[] = sprintf('SUM(%s)', $name) . $alias; |
||
| 93 | break; |
||
| 94 | |||
| 95 | // MIN() column |
||
| 96 | case 'min': |
||
| 97 | $name = $this->escape($val['name']); |
||
| 98 | $alias = isset($val['alias']) ? ' AS '. $this->escape($val['alias']) : ''; |
||
| 99 | $colsList[] = sprintf('MIN(%s)', $name) . $alias; |
||
| 100 | break; |
||
| 101 | |||
| 102 | // max() column |
||
| 103 | case 'max': |
||
| 104 | $name = $this->escape($val['name']); |
||
| 105 | $alias = isset($val['alias']) ? ' AS '. $this->escape($val['alias']) : ''; |
||
| 106 | $colsList[] = sprintf('MAX(%s)', $name) . $alias; |
||
| 107 | break; |
||
| 108 | |||
| 109 | // sub query |
||
| 110 | case 'sub_query': |
||
| 111 | $colsList[] = '('. $val['query']->sql() .') AS '. $this->escape($val['alias']); |
||
| 112 | break; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | return $sqlDistinct . implode(', ', $colsList); |
||
| 116 | } |
||
| 219 | } |