Conditions | 28 |
Paths | 245 |
Total Lines | 195 |
Code Lines | 124 |
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 |
||
156 | private function _buildOrderByTree(&$orderByPathSegments) |
||
157 | { |
||
158 | foreach ($orderByPathSegments as $index1 => &$orderBySubPathSegments) { |
||
159 | $currentNode = $this->_rootOrderByNode; |
||
160 | $currentObject = $this->_dummyObject; |
||
161 | $ascending = true; |
||
162 | $subPathCount = count($orderBySubPathSegments); |
||
163 | // Check sort order is specified in the path, if so set a |
||
164 | // flag and remove that segment |
||
165 | if ($subPathCount > 1) { |
||
166 | if ($orderBySubPathSegments[$subPathCount - 1] === '*desc') { |
||
167 | $ascending = false; |
||
168 | unset($orderBySubPathSegments[$subPathCount - 1]); |
||
169 | --$subPathCount; |
||
170 | } elseif ($orderBySubPathSegments[$subPathCount - 1] === '*asc') { |
||
171 | unset($orderBySubPathSegments[$subPathCount - 1]); |
||
172 | --$subPathCount; |
||
173 | } |
||
174 | } |
||
175 | |||
176 | $ancestors = array($this->_rootOrderByNode->getResourceSetWrapper()->getName()); |
||
177 | foreach ($orderBySubPathSegments as $index2 => $orderBySubPathSegment) { |
||
178 | $isLastSegment = ($index2 == $subPathCount - 1); |
||
179 | $resourceSetWrapper = null; |
||
180 | $resourceType = $currentNode->getResourceType(); |
||
181 | $resourceProperty = $resourceType->resolveProperty($orderBySubPathSegment); |
||
182 | if (is_null($resourceProperty)) { |
||
183 | throw ODataException::createSyntaxError( |
||
184 | Messages::orderByParserPropertyNotFound( |
||
185 | $resourceType->getFullName(), |
||
186 | $orderBySubPathSegment |
||
187 | ) |
||
188 | ); |
||
189 | } |
||
190 | |||
191 | if ($resourceProperty->isKindOf(ResourcePropertyKind::BAG)) { |
||
192 | throw ODataException::createBadRequestError( |
||
193 | Messages::orderByParserBagPropertyNotAllowed( |
||
194 | $resourceProperty->getName() |
||
195 | ) |
||
196 | ); |
||
197 | } elseif ($resourceProperty->isKindOf(ResourcePropertyKind::PRIMITIVE)) { |
||
198 | if (!$isLastSegment) { |
||
199 | throw ODataException::createBadRequestError( |
||
200 | Messages::orderByParserPrimitiveAsIntermediateSegment( |
||
201 | $resourceProperty->getName() |
||
202 | ) |
||
203 | ); |
||
204 | } |
||
205 | |||
206 | $type = $resourceProperty->getInstanceType(); |
||
207 | if ($type instanceof Binary) { |
||
208 | throw ODataException::createBadRequestError(Messages::orderByParserSortByBinaryPropertyNotAllowed($resourceProperty->getName())); |
||
209 | } |
||
210 | } elseif ($resourceProperty->getKind() == ResourcePropertyKind::RESOURCESET_REFERENCE |
||
211 | || $resourceProperty->getKind() == ResourcePropertyKind::RESOURCE_REFERENCE |
||
212 | ) { |
||
213 | $this->_assertion($currentNode instanceof OrderByRootNode || $currentNode instanceof OrderByNode); |
||
214 | $resourceSetWrapper = $currentNode->getResourceSetWrapper(); |
||
215 | $this->_assertion(!is_null($resourceSetWrapper)); |
||
216 | $resourceSetWrapper |
||
217 | = $this->_providerWrapper->getResourceSetWrapperForNavigationProperty( |
||
218 | $resourceSetWrapper, |
||
219 | $resourceType, |
||
220 | $resourceProperty |
||
221 | ); |
||
222 | if (is_null($resourceSetWrapper)) { |
||
223 | throw ODataException::createBadRequestError( |
||
224 | Messages::badRequestInvalidPropertyNameSpecified( |
||
225 | $resourceType->getFullName(), |
||
226 | $orderBySubPathSegment |
||
227 | ) |
||
228 | ); |
||
229 | } |
||
230 | |||
231 | if ($resourceProperty->getKind() == ResourcePropertyKind::RESOURCESET_REFERENCE) { |
||
232 | throw ODataException::createBadRequestError( |
||
233 | Messages::orderByParserResourceSetReferenceNotAllowed( |
||
234 | $resourceProperty->getName(), |
||
235 | $resourceType->getFullName() |
||
236 | ) |
||
237 | ); |
||
238 | } |
||
239 | |||
240 | $resourceSetWrapper->checkResourceSetRightsForRead(true); |
||
241 | if ($isLastSegment) { |
||
242 | throw ODataException::createBadRequestError( |
||
243 | Messages::orderByParserSortByNavigationPropertyIsNotAllowed( |
||
244 | $resourceProperty->getName() |
||
245 | ) |
||
246 | ); |
||
247 | } |
||
248 | |||
249 | $ancestors[] = $orderBySubPathSegment; |
||
250 | } elseif ($resourceProperty->isKindOf(ResourcePropertyKind::COMPLEX_TYPE)) { |
||
251 | if ($isLastSegment) { |
||
252 | throw ODataException::createBadRequestError( |
||
253 | Messages::orderByParserSortByComplexPropertyIsNotAllowed( |
||
254 | $resourceProperty->getName() |
||
255 | ) |
||
256 | ); |
||
257 | } |
||
258 | |||
259 | $ancestors[] = $orderBySubPathSegment; |
||
260 | } else { |
||
261 | throw ODataException::createInternalServerError( |
||
262 | Messages::orderByParserUnexpectedPropertyType() |
||
263 | ); |
||
264 | } |
||
265 | |||
266 | $node = $currentNode->findNode($orderBySubPathSegment); |
||
267 | if (is_null($node)) { |
||
268 | if ($resourceProperty->isKindOf(ResourcePropertyKind::PRIMITIVE)) { |
||
269 | $node = new OrderByLeafNode( |
||
270 | $orderBySubPathSegment, |
||
271 | $resourceProperty, |
||
272 | $ascending |
||
273 | ); |
||
274 | $this->_comparisonFunctions[] |
||
275 | = $node->buildComparisonFunction($ancestors); |
||
276 | } elseif ($resourceProperty->getKind() == ResourcePropertyKind::RESOURCE_REFERENCE) { |
||
277 | $node = new OrderByNode( |
||
278 | $orderBySubPathSegment, |
||
279 | $resourceProperty, |
||
280 | $resourceSetWrapper |
||
281 | ); |
||
282 | // Initialize this member variable (identified by |
||
283 | // $resourceProperty) of parent object. |
||
284 | try { |
||
285 | $object = $resourceProperty->getInstanceType()->newInstance(); |
||
286 | $resourceType->setPropertyValue($currentObject, $resourceProperty->getName(), $object); |
||
287 | $currentObject = $object; |
||
288 | } catch (\ReflectionException $reflectionException) { |
||
289 | throw ODataException::createInternalServerError( |
||
290 | Messages::orderByParserFailedToAccessOrInitializeProperty( |
||
291 | $resourceProperty->getName(), |
||
292 | $resourceType->getName() |
||
293 | ) |
||
294 | ); |
||
295 | } |
||
296 | } elseif ($resourceProperty->getKind() == ResourcePropertyKind::COMPLEX_TYPE) { |
||
297 | $node = new OrderByNode($orderBySubPathSegment, $resourceProperty, null); |
||
298 | // Initialize this member variable |
||
299 | // (identified by $resourceProperty)of parent object. |
||
300 | try { |
||
301 | $object = $resourceProperty->getInstanceType()->newInstance(); |
||
302 | $resourceType->setPropertyValue($currentObject, $resourceProperty->getName(), $object); |
||
303 | $currentObject = $object; |
||
304 | } catch (\ReflectionException $reflectionException) { |
||
305 | throw ODataException::createInternalServerError( |
||
306 | Messages::orderByParserFailedToAccessOrInitializeProperty( |
||
307 | $resourceProperty->getName(), |
||
308 | $resourceType->getName() |
||
309 | ) |
||
310 | ); |
||
311 | } |
||
312 | } |
||
313 | |||
314 | $currentNode->addNode($node); |
||
315 | } else { |
||
316 | try { |
||
317 | // If a magic method for properties exists (eg Eloquent), dive into it directly and return value |
||
318 | if (method_exists($currentObject, '__get')) { |
||
319 | $targProperty = $resourceProperty->getName(); |
||
320 | |||
321 | return $currentObject->$targProperty; |
||
322 | } |
||
323 | $reflectionClass = new \ReflectionClass(get_class($currentObject)); |
||
324 | $reflectionProperty = $reflectionClass->getProperty($resourceProperty->getName()); |
||
325 | $reflectionProperty->setAccessible(true); |
||
326 | $currentObject = $reflectionProperty->getValue($currentObject); |
||
327 | |||
328 | //$dummyProperty = new \ReflectionProperty( |
||
329 | // $currentObject, $resourceProperty->getName() |
||
330 | //); |
||
331 | //$currentObject = $dummyProperty->getValue($currentObject); |
||
332 | } catch (\ReflectionException $reflectionException) { |
||
333 | throw ODataException::createInternalServerError( |
||
334 | Messages::orderByParserFailedToAccessOrInitializeProperty( |
||
335 | $resourceProperty->getName(), |
||
336 | $resourceType->getName() |
||
337 | ) |
||
338 | ); |
||
339 | } |
||
340 | |||
341 | if ($node instanceof OrderByLeafNode) { |
||
342 | //remove duplicate orderby path |
||
343 | unset($orderByPathSegments[$index1]); |
||
344 | } |
||
345 | } |
||
346 | |||
347 | $currentNode = $node; |
||
348 | } |
||
349 | } |
||
350 | } |
||
351 | |||
486 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: