| Conditions | 10 |
| Paths | 12 |
| Total Lines | 68 |
| Code Lines | 47 |
| 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 |
||
| 43 | public function process($collectionEntity, array $context = array(), ?string $format = null) |
||
| 44 | { |
||
| 45 | $resourceMetadata = $this->resourceMetadataFactory->create($collectionEntity->getResource()); |
||
| 46 | $requestUri = null; |
||
| 47 | |||
| 48 | $collectionOperations = $resourceMetadata->getCollectionOperations(); |
||
| 49 | if ($collectionOperations && ($shortName = $resourceMetadata->getShortName())) { |
||
| 50 | $collectionOperations = array_change_key_case($collectionOperations, CASE_LOWER); |
||
| 51 | $baseRoute = trim(trim($resourceMetadata->getAttribute('route_prefix', '')), '/'); |
||
| 52 | $methods = ['post', 'get']; |
||
| 53 | foreach ($methods as $method) { |
||
| 54 | if (array_key_exists($method, $collectionOperations)) { |
||
| 55 | $path = $baseRoute . $this->operationPathResolver->resolveOperationPath( |
||
| 56 | $shortName, |
||
| 57 | $collectionOperations[$method], |
||
| 58 | OperationType::COLLECTION, |
||
| 59 | $method |
||
|
|
|||
| 60 | ); |
||
| 61 | $finalPath = preg_replace('/{_format}$/', $format, $path); |
||
| 62 | $collectionEntity->addCollectionRoute( |
||
| 63 | $method, |
||
| 64 | $finalPath |
||
| 65 | ); |
||
| 66 | if ($method === 'get') { |
||
| 67 | $requestUri = $finalPath; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | /** @var ContextAwareCollectionDataProviderInterface $dataProvider */ |
||
| 74 | $dataProvider = $this->container->get(ContextAwareCollectionDataProviderInterface::class); |
||
| 75 | $isPaginated = (bool) $collectionEntity->getPerPage(); |
||
| 76 | if (!$isPaginated) { |
||
| 77 | $dataProviderContext = []; |
||
| 78 | } else { |
||
| 79 | $dataProviderContext = [ |
||
| 80 | 'filters' => [ |
||
| 81 | 'pagination' => $isPaginated, |
||
| 82 | 'itemsPerPage' => $collectionEntity->getPerPage(), |
||
| 83 | '_page' => 1 |
||
| 84 | ] |
||
| 85 | ]; |
||
| 86 | if ($collectionEntity->getPerPage() !== null && ($request = $this->requestStack->getCurrentRequest())) { |
||
| 87 | $request->attributes->set('_api_pagination', [ |
||
| 88 | 'itemsPerPage' => $collectionEntity->getPerPage(), |
||
| 89 | 'pagination' => 'true' |
||
| 90 | ]); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | /** @var Paginator $collection */ |
||
| 95 | $collection = $dataProvider->getCollection($collectionEntity->getResource(), Request::METHOD_GET, $dataProviderContext); |
||
| 96 | |||
| 97 | $forcedContext = [ |
||
| 98 | 'resource_class' => $collectionEntity->getResource(), |
||
| 99 | 'request_uri' => $requestUri, |
||
| 100 | 'jsonld_has_context' => false, |
||
| 101 | 'api_sub_level' => null |
||
| 102 | ]; |
||
| 103 | $mergedContext = array_merge($context, $forcedContext); |
||
| 104 | $normalizedCollection = $this->itemNormalizer->normalize( |
||
| 105 | $collection, |
||
| 106 | $format, |
||
| 107 | $mergedContext |
||
| 108 | ); |
||
| 109 | if (\is_array($normalizedCollection)) { |
||
| 110 | $collectionEntity->setCollection($normalizedCollection); |
||
| 111 | } |
||
| 127 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.