We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 64 |
| Total Lines | 279 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like FormDescriber 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 FormDescriber, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | final class FormDescriber |
||
| 27 | { |
||
| 28 | |||
| 29 | const SIMPLE_TYPES = [ |
||
| 30 | 'text'=>'string', |
||
| 31 | 'number'=>'float', |
||
| 32 | 'integer'=>'int', |
||
| 33 | 'date'=>'string', |
||
| 34 | 'datetime'=>'string', |
||
| 35 | 'password'=>'string', |
||
| 36 | 'checkbox'=>'boolean', |
||
| 37 | ]; |
||
| 38 | |||
| 39 | private $formFactory; |
||
| 40 | public function __construct(FormFactoryInterface $formFactory = null) |
||
| 41 | { |
||
| 42 | $this->formFactory = $formFactory; |
||
| 43 | } |
||
| 44 | public function describe($class) |
||
| 45 | { |
||
| 46 | if (method_exists(AbstractType::class, 'setDefaultOptions')) { |
||
| 47 | throw new \LogicException('symfony/form < 3.0 is not supported, please upgrade to an higher version to use a form as a model.'); |
||
| 48 | } |
||
| 49 | if (null === $this->formFactory) { |
||
| 50 | throw new \LogicException('You need to enable forms in your application to use a form as a model.'); |
||
| 51 | } |
||
| 52 | $form = $this->formFactory->create($class, null, []); |
||
| 53 | return $this->parseForm($form); |
||
| 54 | } |
||
| 55 | |||
| 56 | private function parseForm(FormInterface $form) |
||
| 57 | { |
||
| 58 | $fields = []; |
||
| 59 | foreach ($form as $name => $child) { |
||
| 60 | $config = $child->getConfig(); |
||
| 61 | $required = $config->getRequired(); |
||
| 62 | $field = [ |
||
| 63 | 'name'=>$name, |
||
| 64 | 'type'=>$this->getType($config, $required), |
||
| 65 | |||
| 66 | ]; |
||
| 67 | $fields[] = $field; |
||
| 68 | } |
||
| 69 | |||
| 70 | return $fields; |
||
| 71 | } |
||
| 72 | |||
| 73 | private function getType(FormConfigBuilderInterface $config, $required) |
||
| 74 | { |
||
| 75 | $type = $config->getType(); |
||
| 76 | |||
| 77 | if (!$builtinFormType = $this->getBuiltinFormType($type)) { |
||
| 78 | // if form type is not builtin in Form component. |
||
| 79 | $type = get_class($type->getInnerType()); |
||
| 80 | //TODO : Create nested type |
||
| 81 | return $type; |
||
| 82 | } |
||
| 83 | |||
| 84 | $blockPrefix = $builtinFormType->getBlockPrefix(); |
||
| 85 | if(in_array($blockPrefix, array_keys(self::SIMPLE_TYPES))) { |
||
| 86 | $typeName = self::SIMPLE_TYPES[$blockPrefix]; |
||
| 87 | $type = GraphQLType::{$typeName}(); |
||
| 88 | |||
| 89 | return $required ? GraphQLType::nonNull($type) : $type; |
||
| 90 | } |
||
| 91 | |||
| 92 | if ('choice' === $blockPrefix) { |
||
| 93 | $multiple = $config->getOption('multiple'); |
||
| 94 | if (($choices = $config->getOption('choices')) && is_array($choices) && count($choices)) { |
||
| 95 | $enums = array_values($choices); |
||
| 96 | if ($this->isNumbersArray($enums)) { |
||
| 97 | $type = 'number'; |
||
| 98 | } elseif ($this->isBooleansArray($enums)) { |
||
| 99 | $type = 'boolean'; |
||
| 100 | } else { |
||
| 101 | $type = 'string'; |
||
| 102 | } |
||
| 103 | $enumName = $this->createEnum($config->getName(), $enums); |
||
| 104 | if ($multiple) { |
||
| 105 | return sprintf("[%s]%s", $enumName, $required?"!":""); |
||
| 106 | } |
||
| 107 | return sprintf("[%s]%s", $enumName, $required?"!":""); |
||
| 108 | } |
||
| 109 | if ($multiple) { |
||
| 110 | return sprintf("[String]%s", $required?"!":""); |
||
| 111 | } |
||
| 112 | return sprintf("String%s", $required?"!":""); |
||
| 113 | } |
||
| 114 | |||
| 115 | if ('repeated' === $blockPrefix) { |
||
| 116 | $property->setType('object'); |
||
| 117 | $property->setRequired([$config->getOption('first_name'), $config->getOption('second_name')]); |
||
| 118 | $subType = $config->getOption('type'); |
||
| 119 | foreach (['first', 'second'] as $subField) { |
||
| 120 | $subName = $config->getOption($subField.'_name'); |
||
| 121 | $subForm = $this->formFactory->create($subType, null, array_merge($config->getOption('options'), $config->getOption($subField.'_options'))); |
||
| 122 | $this->findFormType($subForm->getConfig(), $property->getProperties()->get($subName)); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | if ('collection' === $blockPrefix) { |
||
| 126 | $subType = $config->getOption('entry_type'); |
||
| 127 | $subOptions = $config->getOption('entry_options'); |
||
| 128 | $subForm = $this->formFactory->create($subType, null, $subOptions); |
||
| 129 | $property->setType('array'); |
||
| 130 | $itemsProp = $property->getItems(); |
||
| 131 | $this->findFormType($subForm->getConfig(), $itemsProp); |
||
| 132 | //break; |
||
| 133 | } |
||
| 134 | // The DocumentType is bundled with the DoctrineMongoDBBundle |
||
| 135 | if ('entity' === $blockPrefix || 'document' === $blockPrefix) { |
||
| 136 | $entityClass = $config->getOption('class'); |
||
| 137 | if ($config->getOption('multiple')) { |
||
| 138 | $property->setFormat(sprintf('[%s id]', $entityClass)); |
||
| 139 | $property->setType('array'); |
||
| 140 | $property->getItems()->setType('string'); |
||
| 141 | } else { |
||
| 142 | $property->setType('string'); |
||
| 143 | $property->setFormat(sprintf('%s id', $entityClass)); |
||
| 144 | } |
||
| 145 | //break; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Finds and sets the schema type on $property based on $config info. |
||
| 151 | * |
||
| 152 | * Returns true if a native Swagger type was found, false otherwise |
||
| 153 | * |
||
| 154 | * @param FormConfigBuilderInterface $config |
||
| 155 | * @param $property |
||
| 156 | */ |
||
| 157 | private function findFormType(FormConfigBuilderInterface $config, $property) |
||
| 158 | { |
||
| 159 | $type = $config->getType(); |
||
| 160 | if (!$builtinFormType = $this->getBuiltinFormType($type)) { |
||
| 161 | // if form type is not builtin in Form component. |
||
| 162 | $model = new Model(new Type(Type::BUILTIN_TYPE_OBJECT, false, get_class($type->getInnerType()))); |
||
| 163 | $property->setRef($this->modelRegistry->register($model)); |
||
| 164 | return; |
||
| 165 | } |
||
| 166 | do { |
||
| 167 | $blockPrefix = $builtinFormType->getBlockPrefix(); |
||
| 168 | if ('text' === $blockPrefix) { |
||
| 169 | $property->setType('string'); |
||
| 170 | break; |
||
| 171 | } |
||
| 172 | if ('number' === $blockPrefix) { |
||
| 173 | $property->setType('number'); |
||
| 174 | break; |
||
| 175 | } |
||
| 176 | if ('integer' === $blockPrefix) { |
||
| 177 | $property->setType('integer'); |
||
| 178 | break; |
||
| 179 | } |
||
| 180 | if ('date' === $blockPrefix) { |
||
| 181 | $property->setType('string'); |
||
| 182 | $property->setFormat('date'); |
||
| 183 | break; |
||
| 184 | } |
||
| 185 | if ('datetime' === $blockPrefix) { |
||
| 186 | $property->setType('string'); |
||
| 187 | $property->setFormat('date-time'); |
||
| 188 | break; |
||
| 189 | } |
||
| 190 | if ('choice' === $blockPrefix) { |
||
| 191 | if ($config->getOption('multiple')) { |
||
| 192 | $property->setType('array'); |
||
| 193 | } else { |
||
| 194 | $property->setType('string'); |
||
| 195 | } |
||
| 196 | if (($choices = $config->getOption('choices')) && is_array($choices) && count($choices)) { |
||
| 197 | $enums = array_values($choices); |
||
| 198 | if ($this->isNumbersArray($enums)) { |
||
| 199 | $type = 'number'; |
||
| 200 | } elseif ($this->isBooleansArray($enums)) { |
||
| 201 | $type = 'boolean'; |
||
| 202 | } else { |
||
| 203 | $type = 'string'; |
||
| 204 | } |
||
| 205 | if ($config->getOption('multiple')) { |
||
| 206 | $property->getItems()->setType($type)->setEnum($enums); |
||
| 207 | } else { |
||
| 208 | $property->setType($type)->setEnum($enums); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | break; |
||
| 212 | } |
||
| 213 | if ('checkbox' === $blockPrefix) { |
||
| 214 | $property->setType('boolean'); |
||
| 215 | break; |
||
| 216 | } |
||
| 217 | if ('password' === $blockPrefix) { |
||
| 218 | $property->setType('string'); |
||
| 219 | $property->setFormat('password'); |
||
| 220 | break; |
||
| 221 | } |
||
| 222 | if ('repeated' === $blockPrefix) { |
||
| 223 | $property->setType('object'); |
||
| 224 | $property->setRequired([$config->getOption('first_name'), $config->getOption('second_name')]); |
||
| 225 | $subType = $config->getOption('type'); |
||
| 226 | foreach (['first', 'second'] as $subField) { |
||
| 227 | $subName = $config->getOption($subField.'_name'); |
||
| 228 | $subForm = $this->formFactory->create($subType, null, array_merge($config->getOption('options'), $config->getOption($subField.'_options'))); |
||
| 229 | $this->findFormType($subForm->getConfig(), $property->getProperties()->get($subName)); |
||
| 230 | } |
||
| 231 | break; |
||
| 232 | } |
||
| 233 | if ('collection' === $blockPrefix) { |
||
| 234 | $subType = $config->getOption('entry_type'); |
||
| 235 | $subOptions = $config->getOption('entry_options'); |
||
| 236 | $subForm = $this->formFactory->create($subType, null, $subOptions); |
||
| 237 | $property->setType('array'); |
||
| 238 | $itemsProp = $property->getItems(); |
||
| 239 | $this->findFormType($subForm->getConfig(), $itemsProp); |
||
| 240 | break; |
||
| 241 | } |
||
| 242 | // The DocumentType is bundled with the DoctrineMongoDBBundle |
||
| 243 | if ('entity' === $blockPrefix || 'document' === $blockPrefix) { |
||
| 244 | $entityClass = $config->getOption('class'); |
||
| 245 | if ($config->getOption('multiple')) { |
||
| 246 | $property->setFormat(sprintf('[%s id]', $entityClass)); |
||
| 247 | $property->setType('array'); |
||
| 248 | $property->getItems()->setType('string'); |
||
| 249 | } else { |
||
| 250 | $property->setType('string'); |
||
| 251 | $property->setFormat(sprintf('%s id', $entityClass)); |
||
| 252 | } |
||
| 253 | break; |
||
| 254 | } |
||
| 255 | } while ($builtinFormType = $builtinFormType->getParent()); |
||
| 256 | } |
||
| 257 | /** |
||
| 258 | * @param array $array |
||
| 259 | * |
||
| 260 | * @return bool true if $array contains only numbers, false otherwise |
||
| 261 | */ |
||
| 262 | private function isNumbersArray(array $array): bool |
||
| 263 | { |
||
| 264 | foreach ($array as $item) { |
||
| 265 | if (!is_numeric($item)) { |
||
| 266 | return false; |
||
| 267 | } |
||
| 268 | } |
||
| 269 | return true; |
||
| 270 | } |
||
| 271 | /** |
||
| 272 | * @param array $array |
||
| 273 | * |
||
| 274 | * @return bool true if $array contains only booleans, false otherwise |
||
| 275 | */ |
||
| 276 | private function isBooleansArray(array $array): bool |
||
| 284 | } |
||
| 285 | /** |
||
| 286 | * @param ResolvedFormTypeInterface $type |
||
| 287 | * |
||
| 288 | * @return ResolvedFormTypeInterface|null |
||
| 289 | */ |
||
| 290 | private function getBuiltinFormType(ResolvedFormTypeInterface $type) |
||
| 305 | } |
||
| 306 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths