Total Complexity | 61 |
Total Lines | 226 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like SchemaFactory 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 SchemaFactory, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | final class SchemaFactory implements SchemaFactoryInterface |
||
35 | { |
||
36 | private $resourceMetadataFactory; |
||
37 | private $propertyNameCollectionFactory; |
||
38 | private $propertyMetadataFactory; |
||
39 | private $typeFactory; |
||
40 | private $nameConverter; |
||
41 | private $distinctFormats = []; |
||
42 | |||
43 | public function __construct(TypeFactoryInterface $typeFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, NameConverterInterface $nameConverter = null) |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * When added to the list, the given format will lead to the creation of a new definition. |
||
54 | * |
||
55 | * @internal |
||
56 | */ |
||
57 | public function addDistinctFormat(string $format): void |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
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 | } |
||
142 | |||
143 | private function buildPropertySchema(Schema $schema, string $definitionName, string $normalizedPropertyName, PropertyMetadata $propertyMetadata, array $serializerContext, string $format): void |
||
201 | } |
||
202 | |||
203 | private function buildDefinitionName(string $resourceClass, string $format = 'json', string $type = Schema::TYPE_OUTPUT, ?string $operationType = null, ?string $operationName = null, ?array $serializerContext = null): string |
||
204 | { |
||
205 | [$resourceMetadata, $serializerContext, $inputOrOutputClass] = $this->getMetadata($resourceClass, $type, $operationType, $operationName, $serializerContext); |
||
206 | |||
207 | $prefix = $resourceMetadata->getShortName(); |
||
208 | if (null !== $inputOrOutputClass && $resourceClass !== $inputOrOutputClass) { |
||
209 | $parts = explode('\\', $inputOrOutputClass); |
||
210 | $shortName = end($parts); |
||
211 | $prefix .= ':'.$shortName; |
||
212 | } |
||
213 | |||
214 | if (isset($this->distinctFormats[$format])) { |
||
215 | // JSON is the default, and so isn't included in the definition name |
||
216 | $prefix .= ':'.$format; |
||
217 | } |
||
218 | |||
219 | if (isset($serializerContext[DocumentationNormalizer::SWAGGER_DEFINITION_NAME])) { |
||
220 | $name = sprintf('%s-%s', $prefix, $serializerContext[DocumentationNormalizer::SWAGGER_DEFINITION_NAME]); |
||
221 | } else { |
||
222 | $groups = (array) ($serializerContext[AbstractNormalizer::GROUPS] ?? []); |
||
223 | $name = $groups ? sprintf('%s-%s', $prefix, implode('_', $groups)) : $prefix; |
||
224 | } |
||
225 | |||
226 | return $name; |
||
227 | } |
||
228 | |||
229 | private function getMetadata(string $resourceClass, string $type = Schema::TYPE_OUTPUT, ?string $operationType, ?string $operationName, ?array $serializerContext): ?array |
||
230 | { |
||
231 | $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
||
232 | $attribute = Schema::TYPE_OUTPUT === $type ? 'output' : 'input'; |
||
233 | if (null === $operationType || null === $operationName) { |
||
234 | $inputOrOutput = $resourceMetadata->getAttribute($attribute, ['class' => $resourceClass]); |
||
235 | } else { |
||
236 | $inputOrOutput = $resourceMetadata->getTypedOperationAttribute($operationType, $operationName, $attribute, ['class' => $resourceClass], true); |
||
237 | } |
||
238 | |||
239 | if (null === ($inputOrOutput['class'] ?? null)) { |
||
240 | // input or output disabled |
||
241 | return null; |
||
242 | } |
||
243 | |||
244 | return [ |
||
245 | $resourceMetadata, |
||
246 | $serializerContext ?? $this->getSerializerContext($resourceMetadata, $type, $operationType, $operationName), |
||
247 | $inputOrOutput['class'], |
||
248 | ]; |
||
249 | } |
||
250 | |||
251 | private function getSerializerContext(ResourceMetadata $resourceMetadata, string $type = Schema::TYPE_OUTPUT, ?string $operationType, ?string $operationName): array |
||
260 | } |
||
261 | } |
||
262 |
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.