| Total Complexity | 75 |
| Total Lines | 259 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| 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 |
||
| 36 | final class SchemaFactory implements SchemaFactoryInterface |
||
| 37 | { |
||
| 38 | use ResourceClassInfoTrait; |
||
| 39 | |||
| 40 | private $typeFactory; |
||
| 41 | private $propertyNameCollectionFactory; |
||
| 42 | private $propertyMetadataFactory; |
||
| 43 | private $nameConverter; |
||
| 44 | private $distinctFormats = []; |
||
| 45 | |||
| 46 | public function __construct(TypeFactoryInterface $typeFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, NameConverterInterface $nameConverter = null, ResourceClassResolverInterface $resourceClassResolver = null) |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * When added to the list, the given format will lead to the creation of a new definition. |
||
| 58 | * |
||
| 59 | * @internal |
||
| 60 | */ |
||
| 61 | public function addDistinctFormat(string $format): void |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * {@inheritdoc} |
||
| 68 | */ |
||
| 69 | public function buildSchema(string $className, string $format = 'json', string $type = Schema::TYPE_OUTPUT, ?string $operationType = null, ?string $operationName = null, ?Schema $schema = null, ?array $serializerContext = null, bool $forceCollection = false): Schema |
||
| 150 | } |
||
| 151 | |||
| 152 | private function buildPropertySchema(Schema $schema, string $definitionName, string $normalizedPropertyName, PropertyMetadata $propertyMetadata, array $serializerContext, string $format): void |
||
| 153 | { |
||
| 154 | $version = $schema->getVersion(); |
||
| 155 | $swagger = false; |
||
| 156 | $propertySchema = $propertyMetadata->getSchema() ?? []; |
||
| 157 | |||
| 158 | switch ($version) { |
||
| 159 | case Schema::VERSION_SWAGGER: |
||
| 160 | $swagger = true; |
||
| 161 | $basePropertySchemaAttribute = 'swagger_context'; |
||
| 162 | break; |
||
| 163 | case Schema::VERSION_OPENAPI: |
||
| 164 | $basePropertySchemaAttribute = 'openapi_context'; |
||
| 165 | break; |
||
| 166 | default: |
||
| 167 | $basePropertySchemaAttribute = 'json_schema_context'; |
||
| 168 | } |
||
| 169 | |||
| 170 | $propertySchema = array_merge( |
||
| 171 | $propertySchema, |
||
| 172 | $propertyMetadata->getAttributes()[$basePropertySchemaAttribute] ?? [] |
||
| 173 | ); |
||
| 174 | |||
| 175 | if (false === $propertyMetadata->isWritable() && !$propertyMetadata->isInitializable()) { |
||
| 176 | $propertySchema['readOnly'] = true; |
||
| 177 | } |
||
| 178 | if (!$swagger && false === $propertyMetadata->isReadable()) { |
||
| 179 | $propertySchema['writeOnly'] = true; |
||
| 180 | } |
||
| 181 | if (null !== $description = $propertyMetadata->getDescription()) { |
||
| 182 | $propertySchema['description'] = $description; |
||
| 183 | } |
||
| 184 | // see https://github.com/json-schema-org/json-schema-spec/pull/737 |
||
| 185 | if (!$swagger && null !== $propertyMetadata->getAttribute('deprecation_reason')) { |
||
| 186 | $propertySchema['deprecated'] = true; |
||
| 187 | } |
||
| 188 | // externalDocs is an OpenAPI specific extension, but JSON Schema allows additional keys, so we always add it |
||
| 189 | // See https://json-schema.org/latest/json-schema-core.html#rfc.section.6.4 |
||
| 190 | if (null !== $iri = $propertyMetadata->getIri()) { |
||
| 191 | $propertySchema['externalDocs'] = ['url' => $iri]; |
||
| 192 | } |
||
| 193 | |||
| 194 | if (!isset($propertySchema['default']) && null !== $default = $propertyMetadata->getDefault()) { |
||
| 195 | $propertySchema['default'] = $default; |
||
| 196 | } |
||
| 197 | |||
| 198 | if (!isset($propertySchema['example']) && null !== $example = $propertyMetadata->getExample()) { |
||
| 199 | $propertySchema['example'] = $example; |
||
| 200 | } |
||
| 201 | |||
| 202 | if (!isset($propertySchema['example']) && isset($propertySchema['default'])) { |
||
| 203 | $propertySchema['example'] = $propertySchema['default']; |
||
| 204 | } |
||
| 205 | |||
| 206 | $valueSchema = []; |
||
| 207 | if (null !== $type = $propertyMetadata->getType()) { |
||
| 208 | $isCollection = $type->isCollection(); |
||
| 209 | if (null === $valueType = $isCollection ? $type->getCollectionValueType() : $type) { |
||
| 210 | $builtinType = 'string'; |
||
| 211 | $className = null; |
||
| 212 | } else { |
||
| 213 | $builtinType = $valueType->getBuiltinType(); |
||
| 214 | $className = $valueType->getClassName(); |
||
| 215 | } |
||
| 216 | |||
| 217 | $valueSchema = $this->typeFactory->getType(new Type($builtinType, $type->isNullable(), $className, $isCollection), $format, $propertyMetadata->isReadableLink(), $serializerContext, $schema); |
||
| 218 | } |
||
| 219 | |||
| 220 | $propertySchema = new \ArrayObject($propertySchema + $valueSchema); |
||
| 221 | if (DocumentationNormalizer::OPENAPI_VERSION === $version) { |
||
| 222 | $schema->getDefinitions()[$definitionName]['properties'][$normalizedPropertyName] = $propertySchema; |
||
| 223 | |||
| 224 | return; |
||
| 225 | } |
||
| 226 | |||
| 227 | $schema->getDefinitions()[$definitionName]['properties'][$normalizedPropertyName] = $propertySchema; |
||
| 228 | } |
||
| 229 | |||
| 230 | private function buildDefinitionName(string $className, string $format = 'json', string $type = Schema::TYPE_OUTPUT, ?string $operationType = null, ?string $operationName = null, ?array $serializerContext = null): string |
||
| 254 | } |
||
| 255 | |||
| 256 | private function getMetadata(string $className, string $type = Schema::TYPE_OUTPUT, ?string $operationType, ?string $operationName, ?array $serializerContext): ?array |
||
| 257 | { |
||
| 258 | if (!$this->isResourceClass($className)) { |
||
| 259 | return [ |
||
| 260 | null, |
||
| 261 | $serializerContext ?? [], |
||
| 262 | $className, |
||
| 263 | ]; |
||
| 264 | } |
||
| 265 | |||
| 266 | $resourceMetadata = $this->resourceMetadataFactory->create($className); |
||
| 267 | $attribute = Schema::TYPE_OUTPUT === $type ? 'output' : 'input'; |
||
| 268 | if (null === $operationType || null === $operationName) { |
||
| 269 | $inputOrOutput = $resourceMetadata->getAttribute($attribute, ['class' => $className]); |
||
| 270 | } else { |
||
| 271 | $inputOrOutput = $resourceMetadata->getTypedOperationAttribute($operationType, $operationName, $attribute, ['class' => $className], true); |
||
| 272 | } |
||
| 273 | |||
| 274 | if (null === ($inputOrOutput['class'] ?? null)) { |
||
| 275 | // input or output disabled |
||
| 276 | return null; |
||
| 277 | } |
||
| 278 | |||
| 279 | return [ |
||
| 280 | $resourceMetadata, |
||
| 281 | $serializerContext ?? $this->getSerializerContext($resourceMetadata, $type, $operationType, $operationName), |
||
| 282 | $inputOrOutput['class'], |
||
| 283 | ]; |
||
| 284 | } |
||
| 285 | |||
| 286 | private function getSerializerContext(ResourceMetadata $resourceMetadata, string $type = Schema::TYPE_OUTPUT, ?string $operationType, ?string $operationName): array |
||
| 295 | } |
||
| 296 | } |
||
| 297 |
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.