Conditions | 19 |
Paths | 47 |
Total Lines | 78 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
90 | private function applyFilter(CriteriaInterface $criteria, array $filter) |
||
91 | { |
||
92 | foreach ($filter as $expressionString => $value) { |
||
93 | |||
94 | $criteriaMap = $this->config->getCriteriaMap($criteria->getEntityName()); |
||
95 | if (isset($criteriaMap[$expressionString])) { |
||
96 | $expressionString = $criteriaMap[$expressionString]; |
||
97 | } |
||
98 | |||
99 | if (in_array($expressionString, ['limit', 'offset'])) { |
||
100 | $value = (int)$value; |
||
101 | if ($value < 0) { |
||
102 | throw new RuntimeException( |
||
103 | sprintf('Predicate %s must unsigned int, %s given', $expressionString, $value) |
||
104 | ); |
||
105 | } |
||
106 | |||
107 | $criteria->{$expressionString}($value); |
||
108 | continue; |
||
109 | } |
||
110 | |||
111 | if ($expressionString == 'page') { |
||
112 | if (!isset($filter['limit'])) { |
||
113 | throw new RuntimeException(sprintf('Predicate %s require limit', $expressionString)); |
||
114 | } |
||
115 | $criteria->offset($filter['limit'] * ($value - 1)); |
||
116 | continue; |
||
117 | } |
||
118 | |||
119 | if ($expressionString == 'order') { |
||
120 | $criteria->order($value); |
||
121 | continue; |
||
122 | } |
||
123 | |||
124 | if (strpos($expressionString, '.') !== false) { |
||
125 | $expressionArray = explode('.', $expressionString); |
||
126 | } else { |
||
127 | $expressionArray = explode('_', $expressionString); |
||
128 | } |
||
129 | |||
130 | if (count($expressionArray) > 2) { |
||
131 | continue; |
||
132 | } |
||
133 | |||
134 | if (count($expressionArray) == 2) { |
||
135 | list($attribute, $method) = $expressionArray; |
||
136 | |||
137 | if (in_array($method, ['isNull', 'isNotNull']) && $value) { |
||
138 | $criteria->{$method}($attribute); |
||
139 | continue; |
||
140 | } |
||
141 | |||
142 | if ($method == 'between') { |
||
143 | if (!is_array($value) || !isset($value[0]) || !isset($value[1])) { |
||
144 | throw new RuntimeException( |
||
145 | sprintf('Predicate %s must contain array [MIN_VALUE, MAX_VALUE], ', $method) |
||
146 | ); |
||
147 | } |
||
148 | |||
149 | $criteria->between($attribute, $value[0], $value[1]); |
||
150 | continue; |
||
151 | } |
||
152 | |||
153 | if (!method_exists($criteria, $method)) { |
||
154 | throw new RuntimeException(sprintf('Predicate %s does not exists', $method)); |
||
155 | } |
||
156 | |||
157 | call_user_func([$criteria, $method], $attribute, $value); |
||
158 | } |
||
159 | |||
160 | if (count($expressionArray) == 1) { |
||
161 | $customCriteriaInstance = $this->buildCustomCriteria($expressionArray[0], $criteria); |
||
162 | $customCriteriaInstance($criteria, $value); |
||
163 | } |
||
164 | } |
||
165 | |||
166 | return $criteria; |
||
167 | } |
||
168 | |||
195 |