Conditions | 12 |
Paths | 17 |
Total Lines | 42 |
Code Lines | 32 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 1 |
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 filter(ProxyQueryInterface $proxyQuery, $alias, $field, $data) |
||
23 | { |
||
24 | if (!$data || !is_array($data) || !isset($data['value'])) { |
||
25 | return; |
||
26 | } |
||
27 | |||
28 | $data['type'] = isset($data['type']) ? $data['type'] : DateType::TYPE_EQUAL; |
||
29 | |||
30 | $where = $this->getWhere($proxyQuery); |
||
|
|||
31 | |||
32 | $from = $data['value']; |
||
33 | $to = new \DateTime($from->format('Y-m-d') . ' +86399 seconds'); // 23 hours 59 minutes 59 seconds |
||
34 | |||
35 | switch ($data['type']) { |
||
36 | case DateType::TYPE_GREATER_EQUAL: |
||
37 | $where->gte()->field('a.'.$field)->literal($from); |
||
38 | break; |
||
39 | case DateType::TYPE_GREATER_THAN: |
||
40 | $where->gt()->field('a.'.$field)->literal($from); |
||
41 | break; |
||
42 | case DateType::TYPE_LESS_EQUAL: |
||
43 | $where->lte()->field('a.'.$field)->literal($from); |
||
44 | break; |
||
45 | case DateType::TYPE_LESS_THAN: |
||
46 | $where->lt()->field('a.'.$field)->literal($from); |
||
47 | break; |
||
48 | case DateType::TYPE_NULL: |
||
49 | $where->eq()->field('a.'.$field)->literal(null); |
||
50 | break; |
||
51 | case DateType::TYPE_NOT_NULL: |
||
52 | $where->neq()->field('a.'.$field)->literal(null); |
||
53 | break; |
||
54 | case DateType::TYPE_EQUAL: |
||
55 | default: |
||
56 | $where->andX() |
||
57 | ->gte()->field('a.'.$field)->literal($from)->end() |
||
58 | ->lte()->field('a.'.$field)->literal($to)->end(); |
||
59 | } |
||
60 | |||
61 | // filter is active as we have now modified the query |
||
62 | $this->active = true; |
||
63 | } |
||
64 | |||
87 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.