| Conditions | 1 |
| Paths | 1 |
| Total Lines | 63 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 2 | 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 |
||
| 105 | public function getFilterCriteriaData(): array |
||
| 106 | { |
||
| 107 | return [ |
||
| 108 | // Simple AND |
||
| 109 | [ |
||
| 110 | [ |
||
| 111 | 'filters' => [ |
||
| 112 | ['name' => 'eq', 'field' => 'forename', 'value' => 'Fred'], |
||
| 113 | ['name' => 'between', 'field' => 'age', 'from' => 18, 'to' => 30], |
||
| 114 | ], |
||
| 115 | ], |
||
| 116 | 'SELECT c FROM Customer c WHERE c.forename = :%s AND (c.age BETWEEN :%s AND :%s)', |
||
| 117 | ['Fred', 18, 30], |
||
| 118 | ], |
||
| 119 | |||
| 120 | // OR conditions |
||
| 121 | [ |
||
| 122 | [ |
||
| 123 | 'filters' => [ |
||
| 124 | ['name' => 'eq', 'field' => 'enabled', 'value' => true], |
||
| 125 | ['name' => 'or', |
||
| 126 | 'conditions' => [ |
||
| 127 | ['name' => 'eq', 'field' => 'username', 'value' => 'Fred'], |
||
| 128 | ['name' => 'eq', 'field' => 'username', 'value' => 'bob'], |
||
| 129 | ] |
||
| 130 | ], |
||
| 131 | ], |
||
| 132 | ], |
||
| 133 | 'SELECT c FROM Customer c WHERE c.enabled = :%s AND (c.username = :%s OR c.username = :%s)', |
||
| 134 | [true, 'Fred', 'bob',], |
||
| 135 | ], |
||
| 136 | |||
| 137 | // Sorting |
||
| 138 | [ |
||
| 139 | [ |
||
| 140 | 'filters' => [ |
||
| 141 | ['name' => 'eq', 'field' => 'id', 'value' => 123], |
||
| 142 | ], |
||
| 143 | 'sort' => [ |
||
| 144 | ['name' => Field::class, 'field' => 'id', 'direction' => OrderByDirection::DESC->value], |
||
| 145 | ['field' => 'createdDate'] |
||
| 146 | ] |
||
| 147 | ], |
||
| 148 | 'SELECT c FROM Customer c WHERE c.id = :%s ORDER BY c.id DESC, c.createdDate ASC', |
||
| 149 | [123] |
||
| 150 | ], |
||
| 151 | |||
| 152 | // Like |
||
| 153 | [ |
||
| 154 | [ |
||
| 155 | 'filters' => [ |
||
| 156 | [ |
||
| 157 | 'name' => 'or', |
||
| 158 | 'conditions' => [ |
||
| 159 | ['name' => 'like', 'field' => 'forename', 'value' => '%Bob%'], |
||
| 160 | ['name' => 'gt', 'field' => 'age', 'value' => 18], |
||
| 161 | ] |
||
| 162 | ], |
||
| 163 | ['name' => 'eq', 'field' => 'enabled', 'value' => 1], |
||
| 164 | ] |
||
| 165 | ], |
||
| 166 | 'SELECT c FROM Customer c WHERE (c.forename LIKE :%s OR c.age > :%s) AND c.enabled = :%s', |
||
| 167 | ['%Bob%', 18, true], |
||
| 168 | ] |
||
| 214 |