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