| Total Complexity | 47 |
| Total Lines | 198 |
| Duplicated Lines | 0 % |
| Changes | 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) |
||
| 44 | { |
||
| 45 | $this->resourceMetadataFactory = $resourceMetadataFactory; |
||
| 46 | $this->propertyNameCollectionFactory = $propertyNameCollectionFactory; |
||
| 47 | $this->propertyMetadataFactory = $propertyMetadataFactory; |
||
| 48 | $this->nameConverter = $nameConverter; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @throws ResourceClassNotFoundException |
||
| 53 | */ |
||
| 54 | public function buildSchema(string $resourceClass, bool $output = true, ?string $operationType = null, ?string $operationName = null, ?string $format = null, ?Schema $baseSchema = null, ?array $serializerContext = null): Schema |
||
| 115 | } |
||
| 116 | |||
| 117 | private function buildPropertySchema(Schema $schema, string $definitionName, string $normalizedPropertyName, PropertyMetadata $propertyMetadata, array $serializerContext): void |
||
| 118 | { |
||
| 119 | $version = $schema->getVersion(); |
||
| 120 | switch ($version) { |
||
| 121 | case Schema::VERSION_SWAGGER: |
||
| 122 | $basePropertySchemaAttribute = 'swagger_context'; |
||
| 123 | break; |
||
| 124 | case Schema::VERSION_OPENAPI: |
||
| 125 | $basePropertySchemaAttribute = 'openapi_context'; |
||
| 126 | break; |
||
| 127 | default: |
||
| 128 | $basePropertySchemaAttribute = 'json_schema_context'; |
||
| 129 | } |
||
| 130 | |||
| 131 | $propertySchema = $propertyMetadata->getAttributes()[$basePropertySchemaAttribute] ?? []; |
||
| 132 | if (false === $propertyMetadata->isWritable() && !$propertyMetadata->isInitializable()) { |
||
| 133 | $propertySchema['readOnly'] = true; |
||
| 134 | } |
||
| 135 | if (false === $propertyMetadata->isReadable()) { |
||
| 136 | $propertySchema['writeOnly'] = true; |
||
| 137 | } |
||
| 138 | if (null !== $description = $propertyMetadata->getDescription()) { |
||
| 139 | $propertySchema['description'] = $description; |
||
| 140 | } |
||
| 141 | // see https://github.com/json-schema-org/json-schema-spec/pull/737 |
||
| 142 | if (null !== $propertyMetadata->getAttribute('deprecation_reason')) { |
||
| 143 | $propertySchema['deprecated'] = true; |
||
| 144 | } |
||
| 145 | // externalDocs is an OpenAPI specific extension, but JSON Schema allows additional keys, so we always add it |
||
| 146 | // See https://json-schema.org/latest/json-schema-core.html#rfc.section.6.4 |
||
| 147 | if (null !== $iri = $propertyMetadata->getIri()) { |
||
| 148 | $propertySchema['externalDocs'] = ['url' => $iri]; |
||
| 149 | } |
||
| 150 | |||
| 151 | $valueSchema = []; |
||
| 152 | if (null !== $type = $propertyMetadata->getType()) { |
||
| 153 | $isCollection = $type->isCollection(); |
||
| 154 | if (null === $valueType = $isCollection ? $type->getCollectionValueType() : $type) { |
||
| 155 | $builtinType = 'string'; |
||
| 156 | $className = null; |
||
| 157 | } else { |
||
| 158 | $builtinType = $valueType->getBuiltinType(); |
||
| 159 | $className = $valueType->getClassName(); |
||
| 160 | } |
||
| 161 | |||
| 162 | $valueSchema = $this->getType($builtinType, $isCollection, $className, $propertyMetadata->isReadableLink(), $serializerContext, $schema); |
||
| 163 | } |
||
| 164 | |||
| 165 | $propertySchema = new \ArrayObject($propertySchema + $valueSchema); |
||
| 166 | if (DocumentationNormalizer::OPENAPI_VERSION === $version) { |
||
| 167 | $schema['components']['schemas'][$definitionName]['properties'][$normalizedPropertyName] = $propertySchema; |
||
| 168 | |||
| 169 | return; |
||
| 170 | } |
||
| 171 | |||
| 172 | $schema->getDefinitions()[$definitionName]['properties'][$normalizedPropertyName] = $propertySchema; |
||
| 173 | } |
||
| 174 | |||
| 175 | private function buildDefinitionName(string $resourceClass, bool $output = true, ?string $operationType = null, ?string $operationName = null, ?string $format = null, ?array $serializerContext = null, string $version = DocumentationNormalizer::OPENAPI_VERSION): ?string |
||
| 176 | { |
||
| 177 | if (null === $metadata = $this->getMetadata($resourceClass, $output, $operationType, $operationName, $serializerContext)) { |
||
| 178 | return null; |
||
| 179 | } |
||
| 180 | [$resourceMetadata, $serializerContext, $inputOrOutputClass] = $metadata; |
||
| 181 | |||
| 182 | $prefix = $resourceMetadata->getShortName(); |
||
| 183 | if (null !== $inputOrOutputClass && $resourceClass !== $inputOrOutputClass) { |
||
| 184 | $prefix .= ':'.md5($inputOrOutputClass); |
||
| 185 | } |
||
| 186 | |||
| 187 | /*if (null !== $format) { |
||
| 188 | $prefix .= ':'.$format; |
||
| 189 | }*/ |
||
| 190 | |||
| 191 | if (isset($serializerContext[DocumentationNormalizer::SWAGGER_DEFINITION_NAME])) { |
||
| 192 | $name = sprintf('%s-%s', $prefix, $serializerContext[DocumentationNormalizer::SWAGGER_DEFINITION_NAME]); |
||
| 193 | } else { |
||
| 194 | $groups = (array) ($serializerContext[AbstractNormalizer::GROUPS] ?? []); |
||
| 195 | $name = $groups ? sprintf('%s-%s', $prefix, implode('_', $groups)) : $prefix; |
||
| 196 | } |
||
| 197 | |||
| 198 | return $name; |
||
| 199 | } |
||
| 200 | |||
| 201 | private function getMetadata(string $resourceClass, bool $output, ?string $operationType, ?string $operationName, ?array $serializerContext): ?array |
||
| 220 | ]; |
||
| 221 | } |
||
| 222 | |||
| 223 | private function getSerializerContext(ResourceMetadata $resourceMetadata, bool $output, ?string $operationType, ?string $operationName): array |
||
| 232 | } |
||
| 233 | } |
||
| 234 |
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.