| Total Complexity | 51 |
| Total Lines | 206 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SchemaBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SchemaBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | final class SchemaBuilder |
||
| 35 | { |
||
| 36 | use TypeResolverTrait; |
||
| 37 | |||
| 38 | private $resourceMetadataFactory; |
||
| 39 | private $propertyNameCollectionFactory; |
||
| 40 | private $propertyMetadataFactory; |
||
| 41 | private $nameConverter; |
||
| 42 | |||
| 43 | public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, NameConverterInterface $nameConverter = null) |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @throws ResourceClassNotFoundException |
||
| 53 | */ |
||
| 54 | public function buildSchema(string $resourceClass, string $format = 'json', bool $output = true, ?string $operationType = null, ?string $operationName = null, ?Schema $baseSchema = null, ?array $serializerContext = null): Schema |
||
| 118 | } |
||
| 119 | |||
| 120 | private function buildPropertySchema(Schema $schema, string $definitionName, string $normalizedPropertyName, PropertyMetadata $propertyMetadata, array $serializerContext, string $format): void |
||
| 121 | { |
||
| 122 | $version = $schema->getVersion(); |
||
| 123 | $swagger = false; |
||
| 124 | switch ($version) { |
||
| 125 | case Schema::VERSION_SWAGGER: |
||
| 126 | $swagger = true; |
||
| 127 | $basePropertySchemaAttribute = 'swagger_context'; |
||
| 128 | break; |
||
| 129 | case Schema::VERSION_OPENAPI: |
||
| 130 | $basePropertySchemaAttribute = 'openapi_context'; |
||
| 131 | break; |
||
| 132 | default: |
||
| 133 | $basePropertySchemaAttribute = 'json_schema_context'; |
||
| 134 | } |
||
| 135 | |||
| 136 | $propertySchema = $propertyMetadata->getAttributes()[$basePropertySchemaAttribute] ?? []; |
||
| 137 | if (false === $propertyMetadata->isWritable() && !$propertyMetadata->isInitializable()) { |
||
| 138 | $propertySchema['readOnly'] = true; |
||
| 139 | } |
||
| 140 | if (!$swagger && false === $propertyMetadata->isReadable()) { |
||
| 141 | $propertySchema['writeOnly'] = true; |
||
| 142 | } |
||
| 143 | if (null !== $description = $propertyMetadata->getDescription()) { |
||
| 144 | $propertySchema['description'] = $description; |
||
| 145 | } |
||
| 146 | // see https://github.com/json-schema-org/json-schema-spec/pull/737 |
||
| 147 | if (!$swagger && null !== $propertyMetadata->getAttribute('deprecation_reason')) { |
||
| 148 | $propertySchema['deprecated'] = true; |
||
| 149 | } |
||
| 150 | // externalDocs is an OpenAPI specific extension, but JSON Schema allows additional keys, so we always add it |
||
| 151 | // See https://json-schema.org/latest/json-schema-core.html#rfc.section.6.4 |
||
| 152 | if (null !== $iri = $propertyMetadata->getIri()) { |
||
| 153 | $propertySchema['externalDocs'] = ['url' => $iri]; |
||
| 154 | } |
||
| 155 | |||
| 156 | $valueSchema = []; |
||
| 157 | if (null !== $type = $propertyMetadata->getType()) { |
||
| 158 | $isCollection = $type->isCollection(); |
||
| 159 | if (null === $valueType = $isCollection ? $type->getCollectionValueType() : $type) { |
||
| 160 | $builtinType = 'string'; |
||
| 161 | $className = null; |
||
| 162 | } else { |
||
| 163 | $builtinType = $valueType->getBuiltinType(); |
||
| 164 | $className = $valueType->getClassName(); |
||
| 165 | } |
||
| 166 | |||
| 167 | $valueSchema = $this->getType($builtinType, $isCollection, $className, $format, $propertyMetadata->isReadableLink(), $serializerContext, $schema); |
||
| 168 | } |
||
| 169 | |||
| 170 | $propertySchema = new \ArrayObject($propertySchema + $valueSchema); |
||
| 171 | if (DocumentationNormalizer::OPENAPI_VERSION === $version) { |
||
| 172 | $schema['components']['schemas'][$definitionName]['properties'][$normalizedPropertyName] = $propertySchema; |
||
| 173 | |||
| 174 | return; |
||
| 175 | } |
||
| 176 | |||
| 177 | $schema->getDefinitions()[$definitionName]['properties'][$normalizedPropertyName] = $propertySchema; |
||
| 178 | } |
||
| 179 | |||
| 180 | private function buildDefinitionName(string $resourceClass, string $format = 'json', bool $output = true, ?string $operationType = null, ?string $operationName = null, ?array $serializerContext = null, string $version = DocumentationNormalizer::OPENAPI_VERSION): ?string |
||
| 181 | { |
||
| 182 | if (null === $metadata = $this->getMetadata($resourceClass, $output, $operationType, $operationName, $serializerContext)) { |
||
| 183 | return null; |
||
| 184 | } |
||
| 185 | [$resourceMetadata, $serializerContext, $inputOrOutputClass] = $metadata; |
||
| 186 | |||
| 187 | $prefix = $resourceMetadata->getShortName(); |
||
| 188 | if (null !== $inputOrOutputClass && $resourceClass !== $inputOrOutputClass) { |
||
| 189 | $prefix .= ':'.md5($inputOrOutputClass); |
||
| 190 | } |
||
| 191 | |||
| 192 | if ('json' !== $format) { |
||
| 193 | // We don't include JSON in the definition name because: |
||
| 194 | // * it necessary to preserve backward compatibility with Swagger\DocumentationNormalizer's generated names |
||
| 195 | // * it's the default (so it's useless) |
||
| 196 | $prefix .= ':'.$format; |
||
| 197 | } |
||
| 198 | |||
| 199 | if (isset($serializerContext[DocumentationNormalizer::SWAGGER_DEFINITION_NAME])) { |
||
| 200 | $name = sprintf('%s-%s', $prefix, $serializerContext[DocumentationNormalizer::SWAGGER_DEFINITION_NAME]); |
||
| 201 | } else { |
||
| 202 | $groups = (array) ($serializerContext[AbstractNormalizer::GROUPS] ?? []); |
||
| 203 | $name = $groups ? sprintf('%s-%s', $prefix, implode('_', $groups)) : $prefix; |
||
| 204 | } |
||
| 205 | |||
| 206 | return $name; |
||
| 207 | } |
||
| 208 | |||
| 209 | private function getMetadata(string $resourceClass, bool $output, ?string $operationType, ?string $operationName, ?array $serializerContext): ?array |
||
| 228 | ]; |
||
| 229 | } |
||
| 230 | |||
| 231 | private function getSerializerContext(ResourceMetadata $resourceMetadata, bool $output, ?string $operationType, ?string $operationName): array |
||
| 240 | } |
||
| 241 | } |
||
| 242 |
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.