We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 43 |
| Total Lines | 285 |
| 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 |
|
| 104 | { |
||
| 105 | 15 | $interface = MappingInterface::class; |
|
| 106 | |||
| 107 | 15 | if (!\is_string($builderClass)) { |
|
| 108 | 5 | throw new \InvalidArgumentException( |
|
| 109 | 5 | \sprintf('%s builder class should be string, but %s given.', \ucfirst($type), \gettype($builderClass)) |
|
| 110 | ); |
||
| 111 | } |
||
| 112 | |||
| 113 | 10 | if (!\class_exists($builderClass)) { |
|
| 114 | 2 | throw new \InvalidArgumentException( |
|
| 115 | 2 | \sprintf('%s builder class "%s" not found.', \ucfirst($type), $builderClass) |
|
| 116 | ); |
||
| 117 | } |
||
| 118 | |||
| 119 | 8 | if (!\is_subclass_of($builderClass, $interface)) { |
|
| 120 | 3 | throw new \InvalidArgumentException( |
|
| 121 | 3 | \sprintf( |
|
| 122 | 3 | '%s builder class should implement "%s", but "%s" given.', |
|
| 123 | 3 | \ucfirst($type), |
|
| 124 | 3 | $interface, |
|
| 125 | 3 | $builderClass |
|
| 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 | 34 | } elseif (\is_string($field)) { |
|
| 142 | 2 | @\trigger_error( |
|
| 143 | 'The builder short syntax (Field: Builder => Field: {builder: Builder}) is deprecated as of 0.7 and will be removed in 0.12. '. |
||
| 144 | 2 | 'It will be replaced by the field type short syntax (Field: Type => Field: {type: Type})', |
|
| 145 | 2 | \E_USER_DEPRECATED |
|
| 146 | ); |
||
| 147 | 2 | $fieldBuilderName = $field; |
|
| 148 | } |
||
| 149 | |||
| 150 | 37 | $builderConfig = []; |
|
| 151 | 37 | if (isset($field['builderConfig'])) { |
|
| 152 | 10 | if (\is_array($field['builderConfig'])) { |
|
| 153 | 10 | $builderConfig = $field['builderConfig']; |
|
| 154 | } |
||
| 155 | 10 | unset($field['builderConfig']); |
|
| 156 | } |
||
| 157 | |||
| 158 | 37 | if ($fieldBuilderName) { |
|
| 159 | 11 | $mapping = self::getFieldBuilderMapping($fieldBuilderName, self::BUILDER_FIELD_TYPE, $builderConfig, $reservedTypesMap); |
|
| 160 | |||
| 161 | 9 | $fieldMapping = $mapping['field']; |
|
| 162 | 9 | $field = \is_array($field) ? \array_merge($fieldMapping, $field) : $fieldMapping; |
|
| 163 | 9 | $newTypes = \array_merge($newTypes, $mapping['types']); |
|
| 164 | } |
||
| 165 | 35 | if (isset($field['argsBuilder'])) { |
|
| 166 | 15 | $field = self::processFieldArgumentsBuilders($field); |
|
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | return [ |
||
| 171 | 33 | 'fields' => $fields, |
|
| 172 | 33 | 'types' => $newTypes, |
|
| 173 | ]; |
||
| 174 | } |
||
| 175 | |||
| 176 | 3 | private static function processFieldsBuilders(array $builders, array &$reservedTypesMap) |
|
| 194 | ]; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param string $builderName |
||
| 199 | * @param string $builderType |
||
| 200 | * @param array $builderConfig |
||
| 201 | * @param array $reservedTypesMap |
||
| 202 | * |
||
| 203 | * @return array |
||
| 204 | * |
||
| 205 | * @throws InvalidConfigurationException |
||
| 206 | */ |
||
| 207 | 13 | private static function getFieldBuilderMapping(string $builderName, string $builderType, array $builderConfig, array &$reservedTypesMap) |
|
| 208 | { |
||
| 209 | 13 | $builder = self::getBuilder($builderName, $builderType); |
|
| 210 | 12 | $mapping = $builder->toMappingDefinition($builderConfig); |
|
| 211 | |||
| 212 | 12 | $fieldMappingKey = null; |
|
| 213 | |||
| 214 | 12 | if (self::BUILDER_FIELD_TYPE === $builderType) { |
|
| 215 | 10 | $fieldMappingKey = 'field'; |
|
| 216 | 3 | } elseif (self::BUILDER_FIELDS_TYPE === $builderType) { |
|
| 217 | 3 | $fieldMappingKey = 'fields'; |
|
| 218 | } |
||
| 219 | |||
| 220 | 12 | $fieldMapping = $mapping[$fieldMappingKey] ?? $mapping; |
|
| 221 | 12 | $typesMapping = []; |
|
| 222 | |||
| 223 | 12 | if (isset($mapping[$fieldMappingKey], $mapping['types'])) { |
|
| 224 | 5 | $builderClass = \get_class($builder); |
|
| 225 | |||
| 226 | 5 | foreach ($mapping['types'] as $typeName => $typeConfig) { |
|
| 227 | 5 | if (isset($reservedTypesMap[$typeName])) { |
|
| 228 | 4 | throw new InvalidConfigurationException(\sprintf( |
|
| 229 | 4 | 'Type "%s" emitted by builder "%s" already exists. Type was provided by "%s". Builder may only emit new types. Overriding is not allowed.', |
|
| 230 | 4 | $typeName, |
|
| 231 | 4 | $builderClass, |
|
| 232 | 4 | $reservedTypesMap[$typeName] |
|
| 233 | )); |
||
| 234 | } |
||
| 235 | |||
| 236 | 3 | $reservedTypesMap[$typeName] = $builderClass; |
|
| 237 | 3 | $typesMapping[$typeName] = $typeConfig; |
|
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | return [ |
||
| 242 | 10 | $fieldMappingKey => $fieldMapping, |
|
| 243 | 10 | 'types' => $typesMapping, |
|
| 244 | ]; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param string $name |
||
| 249 | * @param string $type |
||
| 250 | * |
||
| 251 | * @return MappingInterface |
||
| 252 | * |
||
| 253 | * @throws InvalidConfigurationException if builder class not define |
||
| 254 | */ |
||
| 255 | 26 | private static function getBuilder($name, $type) |
|
| 279 | } |
||
| 280 | |||
| 281 | 15 | private static function processFieldArgumentsBuilders(array $field) |
|
| 304 | } |
||
| 305 | } |
||
| 306 |