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