We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 41 |
Total Lines | 268 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
Complex classes like BuilderProcessor 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 BuilderProcessor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | final class BuilderProcessor implements ProcessorInterface |
||
20 | { |
||
21 | public const BUILDER_FIELD_TYPE = 'field'; |
||
22 | public const BUILDER_FIELDS_TYPE = 'fields'; |
||
23 | public const BUILDER_ARGS_TYPE = 'args'; |
||
24 | |||
25 | public const BUILDER_TYPES = [ |
||
26 | self::BUILDER_FIELD_TYPE, |
||
27 | self::BUILDER_FIELDS_TYPE, |
||
28 | self::BUILDER_ARGS_TYPE, |
||
29 | ]; |
||
30 | |||
31 | /** @var MappingInterface[] */ |
||
32 | private static $builderClassMap = [ |
||
33 | self::BUILDER_ARGS_TYPE => [ |
||
34 | 'Relay::ForwardConnection' => ForwardConnectionArgsDefinition::class, |
||
35 | 'Relay::BackwardConnection' => BackwardConnectionArgsDefinition::class, |
||
36 | 'Relay::Connection' => ConnectionArgsDefinition::class, |
||
37 | ], |
||
38 | self::BUILDER_FIELD_TYPE => [ |
||
39 | 'Relay::Mutation' => MutationFieldDefinition::class, |
||
40 | 'Relay::GlobalId' => GlobalIdFieldDefinition::class, |
||
41 | 'Relay::Node' => NodeFieldDefinition::class, |
||
42 | 'Relay::PluralIdentifyingRoot' => PluralIdentifyingRootFieldDefinition::class, |
||
43 | ], |
||
44 | self::BUILDER_FIELDS_TYPE => [ |
||
45 | 'relay-connection' => RelayConnectionFieldsBuilder::class, |
||
46 | 'relay-edge' => RelayEdgeFieldsBuilder::class, |
||
47 | ], |
||
48 | ]; |
||
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | 43 | public static function process(array $configs): array |
|
54 | { |
||
55 | 43 | $addedTypes = []; |
|
56 | // map: "type name" => "provided by" for better DX, while debugging accidental type overrides in builders |
||
57 | 43 | $reservedTypesMap = \array_combine( |
|
58 | 43 | \array_keys($configs), |
|
59 | 43 | \array_fill(0, \count($configs), 'configs') |
|
60 | ); |
||
61 | |||
62 | 43 | foreach ($configs as &$config) { |
|
63 | 43 | if (isset($config['config']['builders']) && \is_array($config['config']['builders'])) { |
|
64 | 3 | ['fields' => $buildersFields, 'types' => $buildersTypes] = self::processFieldsBuilders( |
|
65 | 3 | $config['config']['builders'], |
|
66 | 3 | $reservedTypesMap |
|
|
|||
67 | ); |
||
68 | |||
69 | 2 | $config['config']['fields'] = isset($config['config']['fields']) |
|
70 | 1 | ? \array_merge($buildersFields, $config['config']['fields']) |
|
71 | 2 | : $buildersFields; |
|
72 | |||
73 | 2 | $addedTypes = \array_merge($addedTypes, $buildersTypes); |
|
74 | |||
75 | 2 | unset($config['config']['builders']); |
|
76 | } |
||
77 | |||
78 | 42 | if (isset($config['config']['fields']) && \is_array($config['config']['fields'])) { |
|
79 | 37 | ['fields' => $buildersFields, 'types' => $buildersTypes] = self::processFieldBuilders( |
|
80 | 37 | $config['config']['fields'], |
|
81 | 37 | $reservedTypesMap |
|
82 | ); |
||
83 | |||
84 | 33 | $config['config']['fields'] = $buildersFields; |
|
85 | |||
86 | 33 | $addedTypes = \array_merge($addedTypes, $buildersTypes); |
|
87 | } |
||
88 | } |
||
89 | |||
90 | 37 | return \array_merge($configs, $addedTypes); |
|
91 | } |
||
92 | |||
93 | 15 | public static function addBuilderClass($name, $type, $builderClass): void |
|
94 | { |
||
95 | 15 | self::checkBuilderClass($builderClass, $type); |
|
96 | 5 | self::$builderClassMap[$type][$name] = $builderClass; |
|
97 | 5 | } |
|
98 | |||
99 | /** |
||
100 | * @param string $builderClass |
||
101 | * @param string $type |
||
102 | */ |
||
103 | 15 | private static function checkBuilderClass($builderClass, $type): void |
|
126 | ) |
||
127 | ); |
||
128 | } |
||
129 | 5 | } |
|
130 | |||
131 | 37 | private static function processFieldBuilders(array $fields, array &$reservedTypesMap) |
|
132 | { |
||
133 | 37 | $newTypes = []; |
|
134 | |||
135 | 37 | foreach ($fields as &$field) { |
|
136 | 37 | $fieldBuilderName = null; |
|
137 | |||
138 | 37 | if (isset($field['builder']) && \is_string($field['builder'])) { |
|
139 | 11 | $fieldBuilderName = $field['builder']; |
|
140 | 11 | unset($field['builder']); |
|
141 | } |
||
142 | |||
143 | 37 | $builderConfig = []; |
|
144 | 37 | if (isset($field['builderConfig'])) { |
|
145 | 10 | if (\is_array($field['builderConfig'])) { |
|
146 | 10 | $builderConfig = $field['builderConfig']; |
|
147 | } |
||
148 | 10 | unset($field['builderConfig']); |
|
149 | } |
||
150 | |||
151 | 37 | if ($fieldBuilderName) { |
|
152 | 11 | $mapping = self::getFieldBuilderMapping($fieldBuilderName, self::BUILDER_FIELD_TYPE, $builderConfig, $reservedTypesMap); |
|
153 | |||
154 | 9 | $fieldMapping = $mapping['field']; |
|
155 | 9 | $field = \is_array($field) ? \array_merge($fieldMapping, $field) : $fieldMapping; |
|
156 | 9 | $newTypes = \array_merge($newTypes, $mapping['types']); |
|
157 | } |
||
158 | 35 | if (isset($field['argsBuilder'])) { |
|
159 | 15 | $field = self::processFieldArgumentsBuilders($field); |
|
160 | } |
||
161 | } |
||
162 | |||
163 | return [ |
||
164 | 33 | 'fields' => $fields, |
|
165 | 33 | 'types' => $newTypes, |
|
166 | ]; |
||
167 | } |
||
168 | |||
169 | 3 | private static function processFieldsBuilders(array $builders, array &$reservedTypesMap) |
|
187 | ]; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @param string $builderName |
||
192 | * @param string $builderType |
||
193 | * @param array $builderConfig |
||
194 | * @param array $reservedTypesMap |
||
195 | * |
||
196 | * @return array |
||
197 | * |
||
198 | * @throws InvalidConfigurationException |
||
199 | */ |
||
200 | 13 | private static function getFieldBuilderMapping(string $builderName, string $builderType, array $builderConfig, array &$reservedTypesMap) |
|
201 | { |
||
202 | 13 | $builder = self::getBuilder($builderName, $builderType); |
|
203 | 12 | $mapping = $builder->toMappingDefinition($builderConfig); |
|
204 | |||
205 | 12 | $fieldMappingKey = null; |
|
206 | |||
207 | 12 | if (self::BUILDER_FIELD_TYPE === $builderType) { |
|
208 | 10 | $fieldMappingKey = 'field'; |
|
209 | 3 | } elseif (self::BUILDER_FIELDS_TYPE === $builderType) { |
|
210 | 3 | $fieldMappingKey = 'fields'; |
|
211 | } |
||
212 | |||
213 | 12 | $fieldMapping = $mapping[$fieldMappingKey] ?? $mapping; |
|
214 | 12 | $typesMapping = []; |
|
215 | |||
216 | 12 | if (isset($mapping[$fieldMappingKey], $mapping['types'])) { |
|
217 | 5 | $builderClass = \get_class($builder); |
|
218 | |||
219 | 5 | foreach ($mapping['types'] as $typeName => $typeConfig) { |
|
220 | 5 | if (isset($reservedTypesMap[$typeName])) { |
|
221 | 4 | throw new InvalidConfigurationException(\sprintf( |
|
222 | 4 | 'Type "%s" emitted by builder "%s" already exists. Type was provided by "%s". Builder may only emit new types. Overriding is not allowed.', |
|
223 | 4 | $typeName, |
|
224 | 4 | $builderClass, |
|
225 | 4 | $reservedTypesMap[$typeName] |
|
226 | )); |
||
227 | } |
||
228 | |||
229 | 3 | $reservedTypesMap[$typeName] = $builderClass; |
|
230 | 3 | $typesMapping[$typeName] = $typeConfig; |
|
231 | } |
||
232 | } |
||
233 | |||
234 | return [ |
||
235 | 10 | $fieldMappingKey => $fieldMapping, |
|
236 | 10 | 'types' => $typesMapping, |
|
237 | ]; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * @param string $name |
||
242 | * @param string $type |
||
243 | * |
||
244 | * @return MappingInterface |
||
245 | * |
||
246 | * @throws InvalidConfigurationException if builder class not define |
||
247 | */ |
||
248 | 26 | private static function getBuilder($name, $type) |
|
262 | } |
||
263 | |||
264 | 15 | private static function processFieldArgumentsBuilders(array $field) |
|
287 | } |
||
288 | } |
||
289 |