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