| Conditions | 29 |
| Paths | 4951 |
| Total Lines | 76 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | 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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
| 1 | <?php |
||
| 65 | public function buildSchema(string $resourceClass, string $format = 'json', string $type = Schema::TYPE_OUTPUT, ?string $operationType = null, ?string $operationName = null, ?Schema $schema = null, ?array $serializerContext = null, bool $forceCollection = false): Schema |
||
| 66 | { |
||
| 67 | $schema = $schema ?? new Schema(); |
||
| 68 | if (null === $metadata = $this->getMetadata($resourceClass, $type, $operationType, $operationName, $serializerContext)) { |
||
| 69 | return $schema; |
||
| 70 | } |
||
| 71 | [$resourceMetadata, $serializerContext, $inputOrOutputClass] = $metadata; |
||
| 72 | |||
| 73 | $version = $schema->getVersion(); |
||
| 74 | $definitionName = $this->buildDefinitionName($resourceClass, $format, $type, $operationType, $operationName, $serializerContext); |
||
| 75 | |||
| 76 | if (null === $operationType || null === $operationName) { |
||
| 77 | $method = Schema::TYPE_INPUT === $type ? 'POST' : 'GET'; |
||
| 78 | } else { |
||
| 79 | $method = $resourceMetadata->getTypedOperationAttribute($operationType, $operationName, 'method'); |
||
| 80 | } |
||
| 81 | |||
| 82 | if (Schema::TYPE_OUTPUT !== $type && !\in_array($method, ['POST', 'PATCH', 'PUT'], true)) { |
||
| 83 | return $schema; |
||
| 84 | } |
||
| 85 | |||
| 86 | if (!isset($schema['$ref']) && !isset($schema['type'])) { |
||
| 87 | $ref = Schema::VERSION_OPENAPI === $version ? '#/components/schemas/'.$definitionName : '#/definitions/'.$definitionName; |
||
| 88 | |||
| 89 | $method = null !== $operationType && null !== $operationName ? $resourceMetadata->getTypedOperationAttribute($operationType, $operationName, 'method', 'GET') : 'GET'; |
||
| 90 | if ($forceCollection || (OperationType::COLLECTION === $operationType && 'POST' !== $method)) { |
||
| 91 | $schema['type'] = 'array'; |
||
| 92 | $schema['items'] = ['$ref' => $ref]; |
||
| 93 | } else { |
||
| 94 | $schema['$ref'] = $ref; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | $definitions = $schema->getDefinitions(); |
||
| 99 | if (isset($definitions[$definitionName])) { |
||
| 100 | // Already computed |
||
| 101 | return $schema; |
||
| 102 | } |
||
| 103 | |||
| 104 | $definition = new \ArrayObject(['type' => 'object']); |
||
| 105 | $definitions[$definitionName] = $definition; |
||
| 106 | if (null !== $description = $resourceMetadata->getDescription()) { |
||
| 107 | $definition['description'] = $description; |
||
| 108 | } |
||
| 109 | // see https://github.com/json-schema-org/json-schema-spec/pull/737 |
||
| 110 | if ( |
||
| 111 | Schema::VERSION_SWAGGER !== $version && |
||
| 112 | ( |
||
| 113 | (null !== $operationType && null !== $operationName && null !== $resourceMetadata->getTypedOperationAttribute($operationType, $operationName, 'deprecation_reason', null, true)) || |
||
| 114 | null !== $resourceMetadata->getAttribute('deprecation_reason', null) |
||
| 115 | ) |
||
| 116 | ) { |
||
| 117 | $definition['deprecated'] = true; |
||
| 118 | } |
||
| 119 | // externalDocs is an OpenAPI specific extension, but JSON Schema allows additional keys, so we always add it |
||
| 120 | // See https://json-schema.org/latest/json-schema-core.html#rfc.section.6.4 |
||
| 121 | if (null !== $iri = $resourceMetadata->getIri()) { |
||
| 122 | $definition['externalDocs'] = ['url' => $iri]; |
||
| 123 | } |
||
| 124 | |||
| 125 | $options = isset($serializerContext[AbstractNormalizer::GROUPS]) ? ['serializer_groups' => (array) $serializerContext[AbstractNormalizer::GROUPS]] : []; |
||
| 126 | foreach ($this->propertyNameCollectionFactory->create($inputOrOutputClass, $options) as $propertyName) { |
||
| 127 | $propertyMetadata = $this->propertyMetadataFactory->create($inputOrOutputClass, $propertyName); |
||
| 128 | if (!$propertyMetadata->isReadable() && !$propertyMetadata->isWritable()) { |
||
| 129 | continue; |
||
| 130 | } |
||
| 131 | |||
| 132 | $normalizedPropertyName = $this->nameConverter ? $this->nameConverter->normalize($propertyName, $inputOrOutputClass, $format, $serializerContext) : $propertyName; |
||
| 133 | if ($propertyMetadata->isRequired()) { |
||
| 134 | $definition['required'][] = $normalizedPropertyName; |
||
| 135 | } |
||
| 136 | |||
| 137 | $this->buildPropertySchema($schema, $definitionName, $normalizedPropertyName, $propertyMetadata, $serializerContext, $format); |
||
| 138 | } |
||
| 139 | |||
| 140 | return $schema; |
||
| 141 | } |
||
| 262 |
If an expression can have both
false, andnullas possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.