| Conditions | 12 |
| Paths | 49 |
| Total Lines | 45 |
| 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 |
||
| 27 | public function compare(EntityFetcher $fetcher) |
||
| 28 | { |
||
| 29 | $result = 1; |
||
| 30 | |||
| 31 | // joins, grouping and ordering are just lists so they have to exist |
||
| 32 | foreach (['joins', 'groupBy', 'orderBy'] as $attribute) { |
||
| 33 | foreach ($this->$attribute as $condition) { |
||
| 34 | if (!in_array($condition, $fetcher->$attribute)) { |
||
| 35 | return 0; |
||
| 36 | } |
||
| 37 | $result++; |
||
| 38 | } |
||
| 39 | } |
||
| 40 | |||
| 41 | // where conditions can have 'AND ' or 'OR ' in front |
||
| 42 | // there is a lot of logic behind these keywords that we ignore here |
||
| 43 | foreach ($this->where as $condition) { |
||
| 44 | $condition = preg_replace('/^(AND |OR )/', '', $condition); |
||
| 45 | foreach ($fetcher->where as $fetcherCondition) { |
||
| 46 | $fetcherCondition = preg_replace('/^(AND |OR )/', '', $fetcherCondition); |
||
| 47 | if ($condition === $fetcherCondition) { |
||
| 48 | $result++; |
||
| 49 | continue 2; // continue the outer foreach to not execute return 0 |
||
| 50 | } |
||
| 51 | } |
||
| 52 | return 0; // this is only reached when no condition matched |
||
| 53 | } |
||
| 54 | |||
| 55 | // check if limit and offset matches |
||
| 56 | if ($this->limit) { |
||
| 57 | if ($this->limit !== $fetcher->limit || $this->offset !== $fetcher->offset) { |
||
| 58 | return 0; |
||
| 59 | } |
||
| 60 | $result++; |
||
| 61 | } |
||
| 62 | |||
| 63 | // check if regular expressions match |
||
| 64 | foreach ($this->regularExpressions as $expression) { |
||
| 65 | if (!preg_match($expression, $fetcher->getQuery())) { |
||
| 66 | return 0; |
||
| 67 | } |
||
| 68 | $result++; |
||
| 69 | } |
||
| 70 | |||
| 71 | return $result; |
||
| 72 | } |
||
| 113 |