| Conditions | 12 |
| Paths | 1 |
| Total Lines | 46 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 41 | public function apply($query) |
||
| 42 | { |
||
| 43 | $query->where(function ($query) { |
||
| 44 | foreach ($this->ids as $compositeKey) { |
||
| 45 | // try to parse normalized key |
||
| 46 | if (!is_array($compositeKey)) { |
||
| 47 | $compositeKey = $this->parseNormalizedKey($compositeKey); |
||
| 48 | } else { |
||
| 49 | if (method_exists($query->getModel(), 'hexBinaryColumns') && $query->getModel()->hexBinaryColumns()) { |
||
| 50 | foreach ($compositeKey as $key => $value) { |
||
| 51 | $compositeKey[$key] = in_array($key, $this->getBinaryColumns()) ? $this->recoverBinaryKey( |
||
| 52 | $key, |
||
| 53 | $value |
||
| 54 | ) : $value; |
||
| 55 | } |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | $queryWriter = function ($query) use ($compositeKey) { |
||
| 60 | /* |
||
| 61 | * @var \Illuminate\Database\Query\Builder $query |
||
| 62 | */ |
||
| 63 | foreach ($this->key as $key) { |
||
| 64 | if (empty($compositeKey)) { |
||
| 65 | $query->where($query->getModel()->qualifyColumn($key), null); |
||
| 66 | } else { |
||
| 67 | if (!isset($compositeKey[$key])) { |
||
| 68 | throw new MissingPrimaryKeyValueException( |
||
| 69 | $key, |
||
| 70 | 'Missing value for key '.$key.' in record '.json_encode($compositeKey) |
||
| 71 | ); |
||
| 72 | } |
||
| 73 | |||
| 74 | if ($this->inverse) { |
||
| 75 | $query->orWhere($query->getModel()->qualifyColumn($key), '!=', $compositeKey[$key]); |
||
| 76 | } else { |
||
| 77 | $query->where($query->getModel()->qualifyColumn($key), $compositeKey[$key]); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | } |
||
| 81 | }; |
||
| 82 | |||
| 83 | if ($this->inverse) { |
||
| 84 | $query->where($queryWriter); |
||
| 85 | } else { |
||
| 86 | $query->orWhere($queryWriter); |
||
| 87 | } |
||
| 92 |