Total Complexity | 40 |
Total Lines | 238 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ApiPlatformParser 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 ApiPlatformParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | final class ApiPlatformParser implements ParserInterface |
||
37 | { |
||
38 | public const IN_PREFIX = 'api_platform_in'; |
||
39 | public const OUT_PREFIX = 'api_platform_out'; |
||
40 | public const TYPE_IRI = 'IRI'; |
||
41 | public const TYPE_MAP = [ |
||
42 | Type::BUILTIN_TYPE_BOOL => DataTypes::BOOLEAN, |
||
43 | Type::BUILTIN_TYPE_FLOAT => DataTypes::FLOAT, |
||
44 | Type::BUILTIN_TYPE_INT => DataTypes::INTEGER, |
||
45 | Type::BUILTIN_TYPE_STRING => DataTypes::STRING, |
||
46 | ]; |
||
47 | |||
48 | private $resourceMetadataFactory; |
||
49 | private $propertyNameCollectionFactory; |
||
50 | private $propertyMetadataFactory; |
||
51 | private $nameConverter; |
||
52 | |||
53 | public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, NameConverterInterface $nameConverter = null) |
||
54 | { |
||
55 | @trigger_error('The '.__CLASS__.' class is deprecated since version 2.2 and will be removed in 3.0. NelmioApiDocBundle 3 has native support for API Platform.', E_USER_DEPRECATED); |
||
56 | |||
57 | $this->resourceMetadataFactory = $resourceMetadataFactory; |
||
58 | $this->propertyNameCollectionFactory = $propertyNameCollectionFactory; |
||
59 | $this->propertyMetadataFactory = $propertyMetadataFactory; |
||
60 | $this->nameConverter = $nameConverter; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | public function supports(array $item) |
||
67 | { |
||
68 | $data = explode(':', $item['class'], 3); |
||
69 | if (!\in_array($data[0], [self::IN_PREFIX, self::OUT_PREFIX], true)) { |
||
70 | return false; |
||
71 | } |
||
72 | if (!isset($data[1])) { |
||
73 | return false; |
||
74 | } |
||
75 | |||
76 | try { |
||
77 | $this->resourceMetadataFactory->create($data[1]); |
||
78 | |||
79 | return true; |
||
80 | } catch (ResourceClassNotFoundException $e) { |
||
81 | // return false |
||
|
|||
82 | } |
||
83 | |||
84 | return false; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * {@inheritdoc} |
||
89 | */ |
||
90 | public function parse(array $item): array |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Parses a class. |
||
106 | * |
||
107 | * @param string[] $visited |
||
108 | */ |
||
109 | private function parseResource(ResourceMetadata $resourceMetadata, string $resourceClass, string $io, array $visited = []): array |
||
110 | { |
||
111 | $visited[] = $resourceClass; |
||
112 | |||
113 | $options = []; |
||
114 | $attributes = $resourceMetadata->getAttributes(); |
||
115 | |||
116 | if (isset($attributes['normalization_context'][AbstractNormalizer::GROUPS])) { |
||
117 | $options['serializer_groups'] = (array) $attributes['normalization_context'][AbstractNormalizer::GROUPS]; |
||
118 | } |
||
119 | |||
120 | if (isset($attributes['denormalization_context'][AbstractNormalizer::GROUPS])) { |
||
121 | if (isset($options['serializer_groups'])) { |
||
122 | $options['serializer_groups'] += $attributes['denormalization_context'][AbstractNormalizer::GROUPS]; |
||
123 | } else { |
||
124 | $options['serializer_groups'] = (array) $attributes['denormalization_context'][AbstractNormalizer::GROUPS]; |
||
125 | } |
||
126 | } |
||
127 | |||
128 | return $this->getPropertyMetadata($resourceMetadata, $resourceClass, $io, $visited, $options); |
||
129 | } |
||
130 | |||
131 | private function getGroupsContext(ResourceMetadata $resourceMetadata, string $operationName, bool $isNormalization): array |
||
132 | { |
||
133 | $groupsContext = $isNormalization ? 'normalization_context' : 'denormalization_context'; |
||
134 | $itemOperationAttribute = $resourceMetadata->getItemOperationAttribute($operationName, $groupsContext, [AbstractNormalizer::GROUPS => []], true)[AbstractNormalizer::GROUPS]; |
||
135 | $collectionOperationAttribute = $resourceMetadata->getCollectionOperationAttribute($operationName, $groupsContext, [AbstractNormalizer::GROUPS => []], true)[AbstractNormalizer::GROUPS]; |
||
136 | |||
137 | return [ |
||
138 | $groupsContext => [ |
||
139 | AbstractNormalizer::GROUPS => array_merge((array) ($itemOperationAttribute ?? []), (array) ($collectionOperationAttribute ?? [])), |
||
140 | ], |
||
141 | ]; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Returns groups of item & collection. |
||
146 | */ |
||
147 | private function getGroupsForItemAndCollectionOperation(ResourceMetadata $resourceMetadata, string $operationName, string $io): array |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Returns a property metadata. |
||
169 | * |
||
170 | * @param string[] $visited |
||
171 | * @param string[] $options |
||
172 | */ |
||
173 | private function getPropertyMetadata(ResourceMetadata $resourceMetadata, string $resourceClass, string $io, array $visited, array $options): array |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Parses a property. |
||
193 | * |
||
194 | * @param string $io |
||
195 | * @param string[] $visited |
||
196 | */ |
||
197 | private function parseProperty(ResourceMetadata $resourceMetadata, PropertyMetadata $propertyMetadata, $io, Type $type = null, array $visited = []): array |
||
276 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.