| Conditions | 28 |
| Paths | > 20000 |
| Total Lines | 153 |
| Code Lines | 93 |
| Lines | 45 |
| Ratio | 29.41 % |
| 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 |
||
| 78 | public function getApiDocumentation() : array |
||
| 79 | { |
||
| 80 | $classes = []; |
||
| 81 | $operation = []; |
||
| 82 | $customOperation = []; |
||
| 83 | |||
| 84 | |||
| 85 | $itemOperationsDocs = []; |
||
| 86 | $definitions = []; |
||
| 87 | |||
| 88 | foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) { |
||
| 89 | $operation['item'] = []; |
||
| 90 | $operation['collection'] = []; |
||
| 91 | $customOperations['item'] = []; |
||
|
|
|||
| 92 | $customOperations['collection'] = []; |
||
| 93 | $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
||
| 94 | |||
| 95 | $shortName = $resourceMetadata->getShortName(); |
||
| 96 | $prefixedShortName = ($iri = $resourceMetadata->getIri()) ? $iri : '#'.$shortName; |
||
| 97 | |||
| 98 | $class = [ |
||
| 99 | 'name' => $shortName, |
||
| 100 | 'externalDocs' => ['url' => $prefixedShortName], |
||
| 101 | ]; |
||
| 102 | |||
| 103 | if ($description = $resourceMetadata->getDescription()) { |
||
| 104 | $class = [ |
||
| 105 | 'name' => $shortName, |
||
| 106 | 'description' => $description, |
||
| 107 | 'externalDocs' => ['url' => $prefixedShortName], |
||
| 108 | ]; |
||
| 109 | } |
||
| 110 | |||
| 111 | $attributes = $resourceMetadata->getAttributes(); |
||
| 112 | $context = []; |
||
| 113 | |||
| 114 | if (isset($attributes['normalization_context']['groups'])) { |
||
| 115 | $context['serializer_groups'] = $attributes['normalization_context']['groups']; |
||
| 116 | } |
||
| 117 | |||
| 118 | View Code Duplication | if (isset($attributes['denormalization_context']['groups'])) { |
|
| 119 | $context['serializer_groups'] = isset($context['serializer_groups']) ? array_merge($context['serializer_groups'], $attributes['denormalization_context']['groups']) : $context['serializer_groups']; |
||
| 120 | } |
||
| 121 | |||
| 122 | $definitions[$shortName] = [ |
||
| 123 | 'type' => 'object', |
||
| 124 | 'xml' => ['name' => 'response'], |
||
| 125 | ]; |
||
| 126 | |||
| 127 | foreach ($this->propertyNameCollectionFactory->create($resourceClass, $context) as $propertyName) { |
||
| 128 | $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName); |
||
| 129 | |||
| 130 | if ($propertyMetadata->isRequired()) { |
||
| 131 | $definitions[$shortName]['required'][] = $propertyName; |
||
| 132 | } |
||
| 133 | |||
| 134 | $range = $this->getRange($propertyMetadata); |
||
| 135 | if (null === $range) { |
||
| 136 | continue; |
||
| 137 | } |
||
| 138 | |||
| 139 | if ($propertyMetadata->getDescription()) { |
||
| 140 | $definitions[$shortName]['properties'][$propertyName]['description'] = $propertyMetadata->getDescription(); |
||
| 141 | } |
||
| 142 | |||
| 143 | if ($range['complex']) { |
||
| 144 | $definitions[$shortName]['properties'][$propertyName] = ['$ref' => $range['value']]; |
||
| 145 | } else { |
||
| 146 | $definitions[$shortName]['properties'][$propertyName] = ['type' => $range['value']]; |
||
| 147 | |||
| 148 | if (isset($range['example'])) { |
||
| 149 | $definitions[$shortName]['properties'][$propertyName]['example'] = $range['example']; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | View Code Duplication | if ($operations = $resourceMetadata->getItemOperations()) { |
|
| 155 | foreach ($operations as $operationName => $itemOperation) { |
||
| 156 | $method = $this->operationMethodResolver->getItemOperationMethod($resourceClass, $operationName); |
||
| 157 | $swaggerOperation = $this->getSwaggerOperation($resourceClass, $resourceMetadata, $operationName, $itemOperation, $prefixedShortName, false, $definitions, $method); |
||
| 158 | $operation['item'] = array_merge($operation['item'], $swaggerOperation); |
||
| 159 | if ($operationName !== strtolower($method)) { |
||
| 160 | $customOperations['item'][] = $operationName; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | View Code Duplication | if ($operations = $resourceMetadata->getCollectionOperations()) { |
|
| 166 | foreach ($operations as $operationName => $collectionOperation) { |
||
| 167 | $method = $this->operationMethodResolver->getCollectionOperationMethod($resourceClass, $operationName); |
||
| 168 | $swaggerOperation = $this->getSwaggerOperation($resourceClass, $resourceMetadata, $operationName, $collectionOperation, $prefixedShortName, true, $definitions, $method); |
||
| 169 | $operation['collection'] = array_merge($operation['collection'], $swaggerOperation); |
||
| 170 | if ($operationName !== strtolower($method)) { |
||
| 171 | $customOperations['collection'][] = $operationName; |
||
| 172 | } |
||
| 173 | } |
||
| 174 | } |
||
| 175 | try { |
||
| 176 | $resourceClassIri = $this->iriConverter->getIriFromResourceClass($resourceClass); |
||
| 177 | $itemOperationsDocs[$resourceClassIri] = $operation['collection']; |
||
| 178 | |||
| 179 | View Code Duplication | if (!empty($customOperations['collection'])) { |
|
| 180 | foreach ($customOperations['collection'] as $customOperation) { |
||
| 181 | $path = $resourceMetadata->getCollectionOperationAttribute($customOperation, 'path'); |
||
| 182 | if (null !== $path) { |
||
| 183 | $method = $this->operationMethodResolver->getCollectionOperationMethod($resourceClass, $customOperation); |
||
| 184 | $customSwaggerOperation = $this->getSwaggerOperation($resourceClass, $resourceMetadata, $customOperation, [$method], $prefixedShortName, true, $definitions, $method); |
||
| 185 | |||
| 186 | $itemOperationsDocs[$path] = $customSwaggerOperation; |
||
| 187 | } |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | |||
| 192 | $resourceClassIri .= '/{id}'; |
||
| 193 | |||
| 194 | $itemOperationsDocs[$resourceClassIri] = $operation['item']; |
||
| 195 | |||
| 196 | View Code Duplication | if (!empty($customOperations['item'])) { |
|
| 197 | foreach ($customOperations['item'] as $customOperation) { |
||
| 198 | $path = $resourceMetadata->getItemOperationAttribute($customOperation, 'path'); |
||
| 199 | if (null !== $path) { |
||
| 200 | $method = $this->operationMethodResolver->getItemOperationMethod($resourceClass, $customOperation); |
||
| 201 | $customSwaggerOperation = $this->getSwaggerOperation($resourceClass, $resourceMetadata, $customOperation, [$method], $prefixedShortName, true, $definitions, $method); |
||
| 202 | |||
| 203 | $itemOperationsDocs[$path] = $customSwaggerOperation; |
||
| 204 | } |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | } catch (InvalidArgumentException $e) { |
||
| 209 | } |
||
| 210 | |||
| 211 | $classes[] = $class; |
||
| 212 | } |
||
| 213 | |||
| 214 | $doc['swagger'] = self::SWAGGER_VERSION; |
||
| 215 | if ('' !== $this->title) { |
||
| 216 | $doc['info']['title'] = $this->title; |
||
| 217 | } |
||
| 218 | |||
| 219 | if ('' !== $this->description) { |
||
| 220 | $doc['info']['description'] = $this->description; |
||
| 221 | } |
||
| 222 | $doc['info']['version'] = $this->version ?? '0.0.0'; |
||
| 223 | $doc['basePath'] = $this->urlGenerator->generate('api_hydra_entrypoint'); |
||
| 224 | $doc['definitions'] = $definitions; |
||
| 225 | $doc['externalDocs'] = ['description' => 'Find more about API Platform', 'url' => 'https://api-platform.com']; |
||
| 226 | $doc['tags'] = $classes; |
||
| 227 | $doc['paths'] = $itemOperationsDocs; |
||
| 228 | |||
| 229 | return $doc; |
||
| 230 | } |
||
| 231 | |||
| 423 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.