We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 40 |
Total Lines | 230 |
Duplicated Lines | 0 % |
Coverage | 99.03% |
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 |
||
17 | final class BuilderProcessor implements ProcessorInterface |
||
18 | { |
||
19 | public const BUILDER_FIELD_TYPE = 'field'; |
||
20 | public const BUILDER_FIELDS_TYPE = 'fields'; |
||
21 | public const BUILDER_ARGS_TYPE = 'args'; |
||
22 | |||
23 | public const BUILDER_TYPES = [ |
||
24 | self::BUILDER_FIELD_TYPE, |
||
25 | self::BUILDER_FIELDS_TYPE, |
||
26 | self::BUILDER_ARGS_TYPE, |
||
27 | ]; |
||
28 | |||
29 | /** @var MappingInterface[] */ |
||
30 | private static $builderClassMap = [ |
||
31 | self::BUILDER_ARGS_TYPE => [ |
||
32 | 'Relay::ForwardConnection' => ForwardConnectionArgsDefinition::class, |
||
33 | 'Relay::BackwardConnection' => BackwardConnectionArgsDefinition::class, |
||
34 | 'Relay::Connection' => ConnectionArgsDefinition::class, |
||
35 | ], |
||
36 | self::BUILDER_FIELD_TYPE => [ |
||
37 | 'Relay::Mutation' => MutationFieldDefinition::class, |
||
38 | 'Relay::GlobalId' => GlobalIdFieldDefinition::class, |
||
39 | 'Relay::Node' => NodeFieldDefinition::class, |
||
40 | 'Relay::PluralIdentifyingRoot' => PluralIdentifyingRootFieldDefinition::class, |
||
41 | ], |
||
42 | self::BUILDER_FIELDS_TYPE => [], |
||
43 | ]; |
||
44 | |||
45 | /** |
||
46 | * {@inheritdoc} |
||
47 | */ |
||
48 | 39 | public static function process(array $configs): array |
|
49 | { |
||
50 | 39 | $addedTypes = []; |
|
51 | |||
52 | 39 | foreach ($configs as &$config) { |
|
53 | 39 | if (isset($config['config']['builders']) && \is_array($config['config']['builders'])) { |
|
54 | 1 | ['fields' => $buildersFields, 'types' => $buildersTypes] = self::processFieldsBuilders($config['config']['builders']); |
|
55 | |||
56 | 1 | $config['config']['fields'] = isset($config['config']['fields']) ? \array_merge($buildersFields, $config['config']['fields']) : $buildersFields; |
|
57 | |||
58 | 1 | $addedTypes = \array_merge($addedTypes, $buildersTypes); |
|
59 | |||
60 | 1 | unset($config['config']['builders']); |
|
61 | } |
||
62 | |||
63 | 39 | if (isset($config['config']['fields']) && \is_array($config['config']['fields'])) { |
|
64 | 34 | ['fields' => $buildersFields, 'types' => $buildersTypes] = self::processFieldBuilders($config['config']['fields']); |
|
65 | |||
66 | 32 | $config['config']['fields'] = $buildersFields; |
|
67 | |||
68 | 32 | $addedTypes = \array_merge($addedTypes, $buildersTypes); |
|
69 | } |
||
70 | } |
||
71 | |||
72 | 37 | return \array_merge($configs, $addedTypes); |
|
73 | } |
||
74 | |||
75 | 11 | public static function addBuilderClass($name, $type, $builderClass): void |
|
76 | { |
||
77 | 11 | self::checkBuilderClass($builderClass, $type); |
|
78 | 1 | self::$builderClassMap[$type][$name] = $builderClass; |
|
79 | 1 | } |
|
80 | |||
81 | /** |
||
82 | * @param string $builderClass |
||
83 | * @param string $type |
||
84 | */ |
||
85 | 11 | private static function checkBuilderClass($builderClass, $type): void |
|
86 | { |
||
87 | 11 | $interface = MappingInterface::class; |
|
88 | |||
89 | 11 | if (!\is_string($builderClass)) { |
|
|
|||
90 | 5 | throw new \InvalidArgumentException( |
|
91 | 5 | \sprintf('%s builder class should be string, but %s given.', \ucfirst($type), \gettype($builderClass)) |
|
92 | ); |
||
93 | } |
||
94 | |||
95 | 6 | if (!\class_exists($builderClass)) { |
|
96 | 2 | throw new \InvalidArgumentException( |
|
97 | 2 | \sprintf('%s builder class "%s" not found.', \ucfirst($type), $builderClass) |
|
98 | ); |
||
99 | } |
||
100 | |||
101 | 4 | if (!\is_subclass_of($builderClass, $interface)) { |
|
102 | 3 | throw new \InvalidArgumentException( |
|
103 | 3 | \sprintf( |
|
104 | 3 | '%s builder class should implement "%s", but "%s" given.', |
|
105 | 3 | \ucfirst($type), |
|
106 | 3 | $interface, |
|
107 | 3 | $builderClass |
|
108 | ) |
||
109 | ); |
||
110 | } |
||
111 | 1 | } |
|
112 | |||
113 | 34 | private static function processFieldBuilders(array $fields) |
|
114 | { |
||
115 | 34 | $types = []; |
|
116 | |||
117 | 34 | foreach ($fields as &$field) { |
|
118 | 34 | $fieldBuilderName = null; |
|
119 | |||
120 | 34 | if (isset($field['builder']) && \is_string($field['builder'])) { |
|
121 | 9 | $fieldBuilderName = $field['builder']; |
|
122 | 9 | unset($field['builder']); |
|
123 | 33 | } elseif (\is_string($field)) { |
|
124 | 2 | @\trigger_error( |
|
125 | 'The builder short syntax (Field: Builder => Field: {builder: Builder}) is deprecated as of 0.7 and will be removed in 0.12. '. |
||
126 | 2 | 'It will be replaced by the field type short syntax (Field: Type => Field: {type: Type})', |
|
127 | 2 | \E_USER_DEPRECATED |
|
128 | ); |
||
129 | 2 | $fieldBuilderName = $field; |
|
130 | } |
||
131 | |||
132 | 34 | $builderConfig = []; |
|
133 | 34 | if (isset($field['builderConfig'])) { |
|
134 | 8 | if (\is_array($field['builderConfig'])) { |
|
135 | 8 | $builderConfig = $field['builderConfig']; |
|
136 | } |
||
137 | 8 | unset($field['builderConfig']); |
|
138 | } |
||
139 | |||
140 | 34 | if ($fieldBuilderName) { |
|
141 | 9 | $mapping = self::getBuilder($fieldBuilderName, self::BUILDER_FIELD_TYPE)->toMappingDefinition($builderConfig); |
|
142 | |||
143 | 8 | $fieldMapping = $mapping['field'] ?? $mapping; |
|
144 | 8 | $field = \is_array($field) ? \array_merge($fieldMapping, $field) : $fieldMapping; |
|
145 | |||
146 | 8 | if (isset($mapping['field'], $mapping['types'])) { |
|
147 | 1 | $types = \array_merge($types, $mapping['types']); |
|
148 | } |
||
149 | } |
||
150 | 33 | if (isset($field['argsBuilder'])) { |
|
151 | 15 | $field = self::processFieldArgumentsBuilders($field); |
|
152 | } |
||
153 | } |
||
154 | |||
155 | return [ |
||
156 | 32 | 'fields' => $fields, |
|
157 | 32 | 'types' => $types, |
|
158 | ]; |
||
159 | } |
||
160 | |||
161 | 1 | private static function processFieldsBuilders(array $builders) |
|
187 | ]; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @param string $name |
||
192 | * @param string $type |
||
193 | * |
||
194 | * @return MappingInterface |
||
195 | * |
||
196 | * @throws InvalidConfigurationException if builder class not define |
||
197 | */ |
||
198 | 22 | private static function getBuilder($name, $type) |
|
222 | } |
||
223 | |||
224 | 15 | private static function processFieldArgumentsBuilders(array $field) |
|
247 | } |
||
248 | } |
||
249 |