| Conditions | 40 | 
| Paths | 53 | 
| Total Lines | 176 | 
| Code Lines | 120 | 
| 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 | ||
| 145 |     private function createNextSegment(SegmentDescriptor $previous, $segment, $checkRights){ | ||
| 146 | $previousKind = $previous->getTargetKind(); | ||
| 147 | if ($previousKind == TargetKind::METADATA() | ||
| 148 | || $previousKind == TargetKind::BATCH() | ||
| 149 | || $previousKind == TargetKind::PRIMITIVE_VALUE() | ||
| 150 | || $previousKind == TargetKind::BAG() | ||
| 151 | || $previousKind == TargetKind::MEDIA_RESOURCE() | ||
| 152 |         ) { | ||
| 153 | //All these targets are terminal segments, there cannot be anything after them. | ||
| 154 | throw ODataException::resourceNotFoundError( | ||
| 155 | Messages::segmentParserMustBeLeafSegment($previous->getIdentifier()) | ||
| 156 | ); | ||
| 157 | } | ||
| 158 | |||
| 159 | $identifier = $keyPredicate = null; | ||
| 160 | $this->extractSegmentIdentifierAndKeyPredicate($segment, $identifier, $keyPredicate); | ||
| 161 | $hasPredicate = !is_null($keyPredicate); | ||
| 162 | $current = null; | ||
|  | |||
| 163 |         if ($previousKind == TargetKind::PRIMITIVE()) { | ||
| 164 |             if ($identifier !== ODataConstants::URI_VALUE_SEGMENT) { | ||
| 165 | throw ODataException::resourceNotFoundError( | ||
| 166 | Messages::segmentParserOnlyValueSegmentAllowedAfterPrimitivePropertySegment( | ||
| 167 | $identifier, $previous->getIdentifier() | ||
| 168 | ) | ||
| 169 | ); | ||
| 170 | } | ||
| 171 | |||
| 172 | $this->_assertion(!$hasPredicate); | ||
| 173 | $current = SegmentDescriptor::createFrom($previous); | ||
| 174 | $current->setIdentifier(ODataConstants::URI_VALUE_SEGMENT); | ||
| 175 | $current->setTargetKind(TargetKind::PRIMITIVE_VALUE()); | ||
| 176 | $current->setSingleResult(true); | ||
| 177 |         } else if (!is_null($previous->getPrevious()) && $previous->getPrevious()->getIdentifier() === ODataConstants::URI_LINK_SEGMENT && $identifier !== ODataConstants::URI_COUNT_SEGMENT) { | ||
| 178 | throw ODataException::createBadRequestError( | ||
| 179 | Messages::segmentParserNoSegmentAllowedAfterPostLinkSegment($identifier) | ||
| 180 | ); | ||
| 181 | } else if ($previousKind == TargetKind::RESOURCE() | ||
| 182 | && $previous->isSingleResult() | ||
| 183 | && $identifier === ODataConstants::URI_LINK_SEGMENT | ||
| 184 |         ) { | ||
| 185 | $this->_assertion(!$hasPredicate); | ||
| 186 | $current = SegmentDescriptor::createFrom($previous); | ||
| 187 | $current->setIdentifier(ODataConstants::URI_LINK_SEGMENT); | ||
| 188 | $current->setTargetKind(TargetKind::LINK()); | ||
| 189 |         } else { | ||
| 190 | //Do a sanity check here | ||
| 191 | if ($previousKind != TargetKind::COMPLEX_OBJECT() | ||
| 192 | && $previousKind != TargetKind::RESOURCE() | ||
| 193 | && $previousKind != TargetKind::LINK() | ||
| 194 |             ) { | ||
| 195 | throw ODataException::createInternalServerError( | ||
| 196 | Messages::segmentParserInconsistentTargetKindState() | ||
| 197 | ); | ||
| 198 | } | ||
| 199 | |||
| 200 |             if (!$previous->isSingleResult() && $identifier !== ODataConstants::URI_COUNT_SEGMENT) { | ||
| 201 | throw ODataException::createBadRequestError( | ||
| 202 | Messages::segmentParserCannotQueryCollection($previous->getIdentifier()) | ||
| 203 | ); | ||
| 204 | } | ||
| 205 | |||
| 206 | $current = new SegmentDescriptor(); | ||
| 207 | $current->setIdentifier($identifier); | ||
| 208 | $current->setTargetSource(TargetSource::PROPERTY); | ||
| 209 | $projectedProperty = $previous->getTargetResourceType()->resolveProperty($identifier); | ||
| 210 | $current->setProjectedProperty($projectedProperty); | ||
| 211 | |||
| 212 |             if ($identifier === ODataConstants::URI_COUNT_SEGMENT) { | ||
| 213 |                 if ($previousKind != TargetKind::RESOURCE()) { | ||
| 214 | throw ODataException::createBadRequestError( | ||
| 215 | Messages::segmentParserCountCannotBeApplied($previous->getIdentifier()) | ||
| 216 | ); | ||
| 217 | } | ||
| 218 | |||
| 219 |                 if ($previous->isSingleResult()) { | ||
| 220 | throw ODataException::createBadRequestError( | ||
| 221 | Messages::segmentParserCountCannotFollowSingleton($previous->getIdentifier()) | ||
| 222 | ); | ||
| 223 | } | ||
| 224 | |||
| 225 | $current->setTargetKind(TargetKind::PRIMITIVE_VALUE()); | ||
| 226 | $current->setSingleResult(true); | ||
| 227 | $current->setTargetResourceSetWrapper( | ||
| 228 | $previous->getTargetResourceSetWrapper() | ||
| 229 | ); | ||
| 230 | $current->setTargetResourceType( | ||
| 231 | $previous->getTargetResourceType() | ||
| 232 | ); | ||
| 233 | } else if ($identifier === ODataConstants::URI_VALUE_SEGMENT | ||
| 234 | && $previousKind == TargetKind::RESOURCE() | ||
| 235 |             ) { | ||
| 236 | $current->setSingleResult(true); | ||
| 237 | $current->setTargetResourceType( | ||
| 238 | $previous->getTargetResourceType() | ||
| 239 | ); | ||
| 240 | $current->setTargetKind(TargetKind::MEDIA_RESOURCE()); | ||
| 241 |             } else if (is_null($projectedProperty)) { | ||
| 242 | if (!is_null($previous->getTargetResourceType()) | ||
| 243 | && !is_null($previous->getTargetResourceType()->tryResolveNamedStreamByName($identifier)) | ||
| 244 |                 ) { | ||
| 245 | $current->setTargetKind(TargetKind::MEDIA_RESOURCE()); | ||
| 246 | $current->setSingleResult(true); | ||
| 247 | $current->setTargetResourceType( | ||
| 248 | $previous->getTargetResourceType() | ||
| 249 | ); | ||
| 250 |                 } else { | ||
| 251 | throw ODataException::createResourceNotFoundError($identifier); | ||
| 252 | } | ||
| 253 |             } else { | ||
| 254 | $current->setTargetResourceType($projectedProperty->getResourceType()); | ||
| 255 | $current->setSingleResult($projectedProperty->getKind() != ResourcePropertyKind::RESOURCESET_REFERENCE); | ||
| 256 | if ($previousKind == TargetKind::LINK() | ||
| 257 | && $projectedProperty->getTypeKind() != ResourceTypeKind::ENTITY | ||
| 258 |                 ) { | ||
| 259 | throw ODataException::createBadRequestError( | ||
| 260 | Messages::segmentParserLinkSegmentMustBeFollowedByEntitySegment( | ||
| 261 | $identifier | ||
| 262 | ) | ||
| 263 | ); | ||
| 264 | } | ||
| 265 | |||
| 266 |                 switch($projectedProperty->getKind()) { | ||
| 267 | case ResourcePropertyKind::COMPLEX_TYPE: | ||
| 268 | $current->setTargetKind(TargetKind::COMPLEX_OBJECT()); | ||
| 269 | break; | ||
| 270 | case ResourcePropertyKind::BAG | ResourcePropertyKind::PRIMITIVE: | ||
| 271 | case ResourcePropertyKind::BAG | ResourcePropertyKind::COMPLEX_TYPE: | ||
| 272 | $current->setTargetKind(TargetKind::BAG()); | ||
| 273 | break; | ||
| 274 | case ResourcePropertyKind::RESOURCE_REFERENCE: | ||
| 275 | case ResourcePropertyKind::RESOURCESET_REFERENCE: | ||
| 276 | $current->setTargetKind(TargetKind::RESOURCE()); | ||
| 277 | $resourceSetWrapper = $this->providerWrapper->getResourceSetWrapperForNavigationProperty($previous->getTargetResourceSetWrapper(), $previous->getTargetResourceType(), $projectedProperty); | ||
| 278 |                         if (is_null($resourceSetWrapper)) { | ||
| 279 | throw ODataException::createResourceNotFoundError($projectedProperty->getName()); | ||
| 280 | } | ||
| 281 | |||
| 282 | $current->setTargetResourceSetWrapper($resourceSetWrapper); | ||
| 283 | break; | ||
| 284 | default: | ||
| 285 |                         if (!$projectedProperty->isKindOf(ResourcePropertyKind::PRIMITIVE)) { | ||
| 286 | throw ODataException::createInternalServerError( | ||
| 287 | Messages::segmentParserUnExpectedPropertyKind( | ||
| 288 | 'Primitive' | ||
| 289 | ) | ||
| 290 | ); | ||
| 291 | } | ||
| 292 | |||
| 293 | $current->setTargetKind(TargetKind::PRIMITIVE()); | ||
| 294 | break; | ||
| 295 | } | ||
| 296 | |||
| 297 |                 if ($hasPredicate) { | ||
| 298 | $this->_assertion(!$current->isSingleResult()); | ||
| 299 | $keyDescriptor = $this->_createKeyDescriptor( | ||
| 300 |                         $identifier . '(' . $keyPredicate . ')', | ||
| 301 | $projectedProperty->getResourceType(), | ||
| 302 | $keyPredicate | ||
| 303 | ); | ||
| 304 | $current->setKeyDescriptor($keyDescriptor); | ||
| 305 |                     if (!$keyDescriptor->isEmpty()) { | ||
| 306 | $current->setSingleResult(true); | ||
| 307 | } | ||
| 308 | } | ||
| 309 | |||
| 310 |                 if ($checkRights&& !is_null($current->getTargetResourceSetWrapper())) { | ||
| 311 | $current->getTargetResourceSetWrapper() | ||
| 312 | ->checkResourceSetRightsForRead( | ||
| 313 | $current->isSingleResult() | ||
| 314 | ); | ||
| 315 | } | ||
| 316 | } | ||
| 317 | } | ||
| 318 | |||
| 319 | return $current; | ||
| 320 | } | ||
| 321 | |||
| 454 | } | 
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.