Conditions | 41 |
Paths | 54 |
Total Lines | 189 |
Code Lines | 129 |
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 |
||
164 | private function createNextSegment(SegmentDescriptor $previous, $segment, $checkRights) |
||
165 | { |
||
166 | $previousKind = $previous->getTargetKind(); |
||
167 | if ($previousKind == TargetKind::METADATA() |
||
168 | || $previousKind == TargetKind::BATCH() |
||
169 | || $previousKind == TargetKind::PRIMITIVE_VALUE() |
||
170 | || $previousKind == TargetKind::BAG() |
||
171 | || $previousKind == TargetKind::MEDIA_RESOURCE() |
||
172 | ) { |
||
173 | //All these targets are terminal segments, there cannot be anything after them. |
||
174 | throw ODataException::resourceNotFoundError( |
||
175 | Messages::segmentParserMustBeLeafSegment($previous->getIdentifier()) |
||
176 | ); |
||
177 | } |
||
178 | |||
179 | $identifier = $keyPredicate = null; |
||
180 | $this->extractSegmentIdentifierAndKeyPredicate($segment, $identifier, $keyPredicate); |
||
181 | $hasPredicate = null !== $keyPredicate; |
||
182 | |||
183 | $singleton = $this->providerWrapper->resolveSingleton($identifier); |
||
184 | if (null !== $singleton) { |
||
185 | throw ODataException::createSyntaxError('Singleton must be first element'); |
||
186 | } |
||
187 | |||
188 | if ($previousKind == TargetKind::PRIMITIVE()) { |
||
189 | if ($identifier !== ODataConstants::URI_VALUE_SEGMENT) { |
||
190 | throw ODataException::resourceNotFoundError( |
||
191 | Messages::segmentParserOnlyValueSegmentAllowedAfterPrimitivePropertySegment( |
||
192 | $identifier, |
||
193 | $previous->getIdentifier() |
||
194 | ) |
||
195 | ); |
||
196 | } |
||
197 | |||
198 | $this->assertion(!$hasPredicate); |
||
199 | $current = SegmentDescriptor::createFrom($previous); |
||
200 | $current->setIdentifier(ODataConstants::URI_VALUE_SEGMENT); |
||
201 | $current->setTargetKind(TargetKind::PRIMITIVE_VALUE()); |
||
202 | $current->setSingleResult(true); |
||
203 | } elseif (null !== $previous->getPrevious() |
||
204 | && $previous->getPrevious()->getIdentifier() === ODataConstants::URI_LINK_SEGMENT |
||
205 | && $identifier !== ODataConstants::URI_COUNT_SEGMENT) { |
||
206 | throw ODataException::createBadRequestError( |
||
207 | Messages::segmentParserNoSegmentAllowedAfterPostLinkSegment($identifier) |
||
208 | ); |
||
209 | } elseif ($previousKind == TargetKind::RESOURCE() |
||
210 | && $previous->isSingleResult() |
||
211 | && $identifier === ODataConstants::URI_LINK_SEGMENT |
||
212 | ) { |
||
213 | $this->assertion(!$hasPredicate); |
||
214 | $current = SegmentDescriptor::createFrom($previous); |
||
215 | $current->setIdentifier(ODataConstants::URI_LINK_SEGMENT); |
||
216 | $current->setTargetKind(TargetKind::LINK()); |
||
217 | } else { |
||
218 | //Do a sanity check here |
||
219 | if ($previousKind != TargetKind::COMPLEX_OBJECT() |
||
220 | && $previousKind != TargetKind::RESOURCE() |
||
221 | && $previousKind != TargetKind::LINK() |
||
222 | ) { |
||
223 | throw ODataException::createInternalServerError( |
||
224 | Messages::segmentParserInconsistentTargetKindState() |
||
225 | ); |
||
226 | } |
||
227 | |||
228 | if (!$previous->isSingleResult() && $identifier !== ODataConstants::URI_COUNT_SEGMENT) { |
||
229 | throw ODataException::createBadRequestError( |
||
230 | Messages::segmentParserCannotQueryCollection($previous->getIdentifier()) |
||
231 | ); |
||
232 | } |
||
233 | |||
234 | $current = new SegmentDescriptor(); |
||
235 | $current->setIdentifier($identifier); |
||
236 | $current->setTargetSource(TargetSource::PROPERTY); |
||
|
|||
237 | $previousType = $previous->getTargetResourceType(); |
||
238 | $projectedProperty = $previousType->resolveProperty($identifier); |
||
239 | $current->setProjectedProperty($projectedProperty); |
||
240 | |||
241 | if ($identifier === ODataConstants::URI_COUNT_SEGMENT) { |
||
242 | if ($previousKind != TargetKind::RESOURCE()) { |
||
243 | throw ODataException::createBadRequestError( |
||
244 | Messages::segmentParserCountCannotBeApplied($previous->getIdentifier()) |
||
245 | ); |
||
246 | } |
||
247 | |||
248 | if ($previous->isSingleResult()) { |
||
249 | throw ODataException::createBadRequestError( |
||
250 | Messages::segmentParserCountCannotFollowSingleton($previous->getIdentifier()) |
||
251 | ); |
||
252 | } |
||
253 | |||
254 | $current->setTargetKind(TargetKind::PRIMITIVE_VALUE()); |
||
255 | $current->setSingleResult(true); |
||
256 | $current->setTargetResourceSetWrapper( |
||
257 | $previous->getTargetResourceSetWrapper() |
||
258 | ); |
||
259 | $current->setTargetResourceType( |
||
260 | $previous->getTargetResourceType() |
||
261 | ); |
||
262 | } elseif ($identifier === ODataConstants::URI_VALUE_SEGMENT |
||
263 | && $previousKind == TargetKind::RESOURCE() |
||
264 | ) { |
||
265 | $current->setSingleResult(true); |
||
266 | $current->setTargetResourceType( |
||
267 | $previous->getTargetResourceType() |
||
268 | ); |
||
269 | $current->setTargetKind(TargetKind::MEDIA_RESOURCE()); |
||
270 | } elseif (null === $projectedProperty) { |
||
271 | if (null !== $previous->getTargetResourceType() |
||
272 | && null !== $previous->getTargetResourceType()->tryResolveNamedStreamByName($identifier) |
||
273 | ) { |
||
274 | $current->setTargetKind(TargetKind::MEDIA_RESOURCE()); |
||
275 | $current->setSingleResult(true); |
||
276 | $current->setTargetResourceType( |
||
277 | $previous->getTargetResourceType() |
||
278 | ); |
||
279 | } else { |
||
280 | throw ODataException::createResourceNotFoundError($identifier); |
||
281 | } |
||
282 | } else { |
||
283 | $current->setTargetResourceType($projectedProperty->getResourceType()); |
||
284 | $current->setSingleResult($projectedProperty->getKind() != ResourcePropertyKind::RESOURCESET_REFERENCE); |
||
285 | if ($previousKind == TargetKind::LINK() |
||
286 | && $projectedProperty->getTypeKind() != ResourceTypeKind::ENTITY() |
||
287 | ) { |
||
288 | throw ODataException::createBadRequestError( |
||
289 | Messages::segmentParserLinkSegmentMustBeFollowedByEntitySegment( |
||
290 | $identifier |
||
291 | ) |
||
292 | ); |
||
293 | } |
||
294 | |||
295 | switch ($projectedProperty->getKind()) { |
||
296 | case ResourcePropertyKind::COMPLEX_TYPE: |
||
297 | $current->setTargetKind(TargetKind::COMPLEX_OBJECT()); |
||
298 | break; |
||
299 | case ResourcePropertyKind::BAG | ResourcePropertyKind::PRIMITIVE: |
||
300 | case ResourcePropertyKind::BAG | ResourcePropertyKind::COMPLEX_TYPE: |
||
301 | $current->setTargetKind(TargetKind::BAG()); |
||
302 | break; |
||
303 | case ResourcePropertyKind::RESOURCE_REFERENCE: |
||
304 | case ResourcePropertyKind::RESOURCESET_REFERENCE: |
||
305 | $current->setTargetKind(TargetKind::RESOURCE()); |
||
306 | $prevResource = $previous->getTargetResourceType(); |
||
307 | assert($prevResource instanceof ResourceEntityType); |
||
308 | $resourceSetWrapper = $this->providerWrapper->getResourceSetWrapperForNavigationProperty( |
||
309 | $previous->getTargetResourceSetWrapper(), |
||
310 | $prevResource, |
||
311 | $projectedProperty |
||
312 | ); |
||
313 | if (null === $resourceSetWrapper) { |
||
314 | throw ODataException::createResourceNotFoundError($projectedProperty->getName()); |
||
315 | } |
||
316 | |||
317 | $current->setTargetResourceSetWrapper($resourceSetWrapper); |
||
318 | break; |
||
319 | default: |
||
320 | if (!$projectedProperty->isKindOf(ResourcePropertyKind::PRIMITIVE)) { |
||
321 | throw ODataException::createInternalServerError( |
||
322 | Messages::segmentParserUnExpectedPropertyKind('Primitive') |
||
323 | ); |
||
324 | } |
||
325 | |||
326 | $current->setTargetKind(TargetKind::PRIMITIVE()); |
||
327 | break; |
||
328 | } |
||
329 | |||
330 | if ($hasPredicate) { |
||
331 | $this->assertion(!$current->isSingleResult()); |
||
332 | $keyDescriptor = $this->createKeyDescriptor( |
||
333 | $identifier . '(' . $keyPredicate . ')', |
||
334 | $projectedProperty->getResourceType(), |
||
335 | $keyPredicate |
||
336 | ); |
||
337 | $current->setKeyDescriptor($keyDescriptor); |
||
338 | if (!$keyDescriptor->isEmpty()) { |
||
339 | $current->setSingleResult(true); |
||
340 | } |
||
341 | } |
||
342 | |||
343 | if ($checkRights && null !== $current->getTargetResourceSetWrapper()) { |
||
344 | $current->getTargetResourceSetWrapper() |
||
345 | ->checkResourceSetRightsForRead( |
||
346 | $current->isSingleResult() |
||
347 | ); |
||
348 | } |
||
349 | } |
||
350 | } |
||
351 | |||
352 | return $current; |
||
353 | } |
||
502 |