Conditions | 15 |
Paths | 292 |
Total Lines | 86 |
Code Lines | 57 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 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 |
||
44 | public function process($collectionEntity, array $context = array(), ?string $format = null) |
||
45 | { |
||
46 | $resourceMetadata = $this->resourceMetadataFactory->create($collectionEntity->getResource()); |
||
47 | $requestUri = null; |
||
48 | |||
49 | $collectionOperations = $resourceMetadata->getCollectionOperations(); |
||
50 | if ($collectionOperations && ($shortName = $resourceMetadata->getShortName())) { |
||
51 | $collectionOperations = array_change_key_case($collectionOperations, CASE_LOWER); |
||
52 | $baseRoute = trim(trim($resourceMetadata->getAttribute('route_prefix', '')), '/'); |
||
53 | $methods = ['post', 'get']; |
||
54 | foreach ($methods as $method) { |
||
55 | if (array_key_exists($method, $collectionOperations)) { |
||
56 | $path = $baseRoute . $this->operationPathResolver->resolveOperationPath( |
||
57 | $shortName, |
||
58 | $collectionOperations[$method], |
||
59 | OperationType::COLLECTION, |
||
60 | $method |
||
|
|||
61 | ); |
||
62 | $finalPath = preg_replace('/{_format}$/', $format, $path); |
||
63 | $collectionEntity->addCollectionRoute( |
||
64 | $method, |
||
65 | $finalPath |
||
66 | ); |
||
67 | if ($method === 'get') { |
||
68 | $requestUri = $finalPath; |
||
69 | } |
||
70 | } |
||
71 | } |
||
72 | } |
||
73 | |||
74 | /** @var ContextAwareCollectionDataProviderInterface $dataProvider */ |
||
75 | $dataProvider = $this->container->get(ContextAwareCollectionDataProviderInterface::class); |
||
76 | $isPaginated = (bool) $collectionEntity->getPerPage(); |
||
77 | |||
78 | if ($request = $this->requestStack->getCurrentRequest()) { |
||
79 | $resetQueryString = false; |
||
80 | // Set the default querystring for the RequestParser class if we have not passed one in the request |
||
81 | if ($defaultQueryString = $collectionEntity->getDefaultQueryString()) { |
||
82 | $qs = $request->server->get('QUERY_STRING'); |
||
83 | if (!$qs) { |
||
84 | $resetQueryString = true; |
||
85 | $request->server->set('QUERY_STRING', $defaultQueryString); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | if (null === $filters = $request->attributes->get('_api_filters')) { |
||
90 | $queryString = RequestParser::getQueryString($request); |
||
91 | $filters = $queryString ? RequestParser::parseRequestParams($queryString) : null; |
||
92 | } |
||
93 | |||
94 | if ($resetQueryString) { |
||
95 | $request->server->set('QUERY_STRING', ''); |
||
96 | } |
||
97 | |||
98 | $dataProviderContext = null === $filters ? [] : ['filters' => $filters]; |
||
99 | if ($isPaginated) { |
||
100 | $dataProviderContext['filters'] = $dataProviderContext['filters'] ?? []; |
||
101 | $dataProviderContext['filters'] = array_merge($dataProviderContext['filters'], [ |
||
102 | 'pagination' => true, |
||
103 | 'itemsPerPage' => $collectionEntity->getPerPage(), |
||
104 | '_page' => 1 |
||
105 | ]); |
||
106 | $request->attributes->set('_api_pagination', [ |
||
107 | 'pagination' => 'true', |
||
108 | 'itemsPerPage' => $collectionEntity->getPerPage() |
||
109 | ]); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | /** @var Paginator $collection */ |
||
114 | $collection = $dataProvider->getCollection($collectionEntity->getResource(), Request::METHOD_GET, $dataProviderContext); |
||
115 | |||
116 | $forcedContext = [ |
||
117 | 'resource_class' => $collectionEntity->getResource(), |
||
118 | 'request_uri' => $requestUri, |
||
119 | 'jsonld_has_context' => false, |
||
120 | 'api_sub_level' => null |
||
121 | ]; |
||
122 | $mergedContext = array_merge($context, $forcedContext); |
||
123 | $normalizedCollection = $this->itemNormalizer->normalize( |
||
124 | $collection, |
||
125 | $format, |
||
126 | $mergedContext |
||
127 | ); |
||
128 | if (\is_array($normalizedCollection)) { |
||
129 | $collectionEntity->setCollection($normalizedCollection); |
||
130 | } |
||
146 |
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.