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