Total Complexity | 48 |
Total Lines | 201 |
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, string $format = 'json', bool $output = true, ?string $operationType = null, ?string $operationName = null, ?Schema $baseSchema = null, ?array $serializerContext = null): Schema |
||
115 | } |
||
116 | |||
117 | private function buildPropertySchema(Schema $schema, string $definitionName, string $normalizedPropertyName, PropertyMetadata $propertyMetadata, array $serializerContext, string $format): 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, $format, $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, string $format = 'json', bool $output = true, ?string $operationType = null, ?string $operationName = 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 ('json' !== $format) { |
||
188 | // We don't include JSON in the definition name because: |
||
189 | // * it necessary to preserve backward compatibility with Swagger\DocumentationNormalizer's generated names |
||
190 | // * it's the default (so it's useless) |
||
191 | $prefix .= ':'.$format; |
||
192 | } |
||
193 | |||
194 | if (isset($serializerContext[DocumentationNormalizer::SWAGGER_DEFINITION_NAME])) { |
||
195 | $name = sprintf('%s-%s', $prefix, $serializerContext[DocumentationNormalizer::SWAGGER_DEFINITION_NAME]); |
||
196 | } else { |
||
197 | $groups = (array) ($serializerContext[AbstractNormalizer::GROUPS] ?? []); |
||
198 | $name = $groups ? sprintf('%s-%s', $prefix, implode('_', $groups)) : $prefix; |
||
199 | } |
||
200 | |||
201 | return $name; |
||
202 | } |
||
203 | |||
204 | private function getMetadata(string $resourceClass, bool $output, ?string $operationType, ?string $operationName, ?array $serializerContext): ?array |
||
223 | ]; |
||
224 | } |
||
225 | |||
226 | private function getSerializerContext(ResourceMetadata $resourceMetadata, bool $output, ?string $operationType, ?string $operationName): array |
||
235 | } |
||
236 | } |
||
237 |
If an expression can have both
false
, andnull
as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.