| Conditions | 12 |
| Paths | 25 |
| Total Lines | 49 |
| Code Lines | 23 |
| 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 |
||
| 20 | public function addTablePrefix($values, bool $tableFieldMix = true) |
||
| 21 | { |
||
| 22 | if (is_null($this->getTablePrefix())) { |
||
| 23 | return $values; |
||
| 24 | } |
||
| 25 | |||
| 26 | // $value will be an array and we will add prefix to all table names |
||
| 27 | |||
| 28 | // If supplied value is not an array then make it one |
||
| 29 | $single = false; |
||
| 30 | if (!is_array($values)) { |
||
| 31 | $values = [$values]; |
||
| 32 | // We had single value, so should return a single value |
||
| 33 | $single = true; |
||
| 34 | } |
||
| 35 | |||
| 36 | $return = []; |
||
| 37 | |||
| 38 | foreach ($values as $key => $value) { |
||
| 39 | // It's a raw query, just add it to our return array and continue next |
||
| 40 | if ($value instanceof Raw || $value instanceof Closure) { |
||
| 41 | $return[$key] = $value; |
||
| 42 | continue; |
||
| 43 | } |
||
| 44 | |||
| 45 | // If key is not integer, it is likely a alias mapping, |
||
| 46 | // so we need to change prefix target |
||
| 47 | $target = &$value; |
||
| 48 | if (!is_int($key)) { |
||
| 49 | $target = &$key; |
||
| 50 | } |
||
| 51 | |||
| 52 | // Do prefix if the target is an expression or function. |
||
| 53 | if ( |
||
| 54 | !$tableFieldMix |
||
| 55 | || ( |
||
| 56 | is_string($target) // Must be a string |
||
| 57 | && (bool) preg_match('/^[A-Za-z0-9_.]+$/', $target) // Can only contain letters, numbers, underscore and full stops |
||
| 58 | && 1 === \substr_count($target, '.') // Contains a single full stop ONLY. |
||
| 59 | ) |
||
| 60 | ) { |
||
| 61 | $target = $this->getTablePrefix() . $target; |
||
| 62 | } |
||
| 63 | |||
| 64 | $return[$key] = $value; |
||
| 65 | } |
||
| 66 | |||
| 67 | // If we had single value then we should return a single value (end value of the array) |
||
| 68 | return true === $single ? end($return) : $return; |
||
| 69 | } |
||
| 86 |