| Total Complexity | 63 |
| Total Lines | 314 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 1 | Features | 0 |
Complex classes like FieldFactory 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 FieldFactory, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | final class FieldFactory |
||
| 43 | { |
||
| 44 | /** |
||
| 45 | * @param array[] $fieldConfigs |
||
| 46 | */ |
||
| 47 | public function __construct( |
||
| 48 | private ?string $containerTag = null, |
||
| 49 | private array $containerAttributes = [], |
||
| 50 | private string|array|null $containerClass = null, |
||
| 51 | private ?bool $useContainer = null, |
||
| 52 | private ?string $template = null, |
||
| 53 | private ?string $templateBegin = null, |
||
| 54 | private ?string $templateEnd = null, |
||
| 55 | private ?bool $setInputId = null, |
||
| 56 | private array $inputAttributes = [], |
||
| 57 | private string|array|null $inputClass = null, |
||
| 58 | string|array|null $labelClass = null, |
||
| 59 | private array $labelConfig = [], |
||
| 60 | string|array|null $hintClass = null, |
||
| 61 | private array $hintConfig = [], |
||
| 62 | string|array|null $errorClass = null, |
||
| 63 | private array $errorConfig = [], |
||
| 64 | private ?bool $usePlaceholder = null, |
||
| 65 | private ?string $validClass = null, |
||
| 66 | private ?string $invalidClass = null, |
||
| 67 | private ?bool $enrichmentFromRules = null, |
||
| 68 | private array $fieldConfigs = [], |
||
| 69 | ) { |
||
| 70 | if ($labelClass !== null) { |
||
| 71 | $this->labelConfig['class()'] = is_array($labelClass) ? $labelClass : [$labelClass]; |
||
|
|
|||
| 72 | } |
||
| 73 | if ($hintClass !== null) { |
||
| 74 | $this->hintConfig['class()'] = is_array($hintClass) ? $hintClass : [$hintClass]; |
||
| 75 | } |
||
| 76 | if ($errorClass !== null) { |
||
| 77 | $this->errorConfig['class()'] = is_array($errorClass) ? $errorClass : [$errorClass]; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | public function button(array $config = []): Button |
||
| 82 | { |
||
| 83 | return $this->field(Button::class, $config); |
||
| 84 | } |
||
| 85 | |||
| 86 | public function buttonGroup(array $config = []): ButtonGroup |
||
| 87 | { |
||
| 88 | return $this->field(ButtonGroup::class, $config); |
||
| 89 | } |
||
| 90 | |||
| 91 | public function checkbox(FormModelInterface $formModel, string $attribute, array $config = []): Checkbox |
||
| 92 | { |
||
| 93 | return $this->input(Checkbox::class, $formModel, $attribute, $config); |
||
| 94 | } |
||
| 95 | |||
| 96 | public function checkboxList(FormModelInterface $formModel, string $attribute, array $config = []): CheckboxList |
||
| 97 | { |
||
| 98 | return $this |
||
| 99 | ->field(CheckboxList::class, $config) |
||
| 100 | ->formAttribute($formModel, $attribute); |
||
| 101 | } |
||
| 102 | |||
| 103 | public function date(FormModelInterface $formModel, string $attribute, array $config = []): Date |
||
| 104 | { |
||
| 105 | return $this->input(Date::class, $formModel, $attribute, $config); |
||
| 106 | } |
||
| 107 | |||
| 108 | public function dateTime(FormModelInterface $formModel, string $attribute, array $config = []): DateTime |
||
| 109 | { |
||
| 110 | return $this->input(DateTime::class, $formModel, $attribute, $config); |
||
| 111 | } |
||
| 112 | |||
| 113 | public function dateTimeLocal(FormModelInterface $formModel, string $attribute, array $config = []): DateTimeLocal |
||
| 114 | { |
||
| 115 | return $this->input(DateTimeLocal::class, $formModel, $attribute, $config); |
||
| 116 | } |
||
| 117 | |||
| 118 | public function email(FormModelInterface $formModel, string $attribute, array $config = []): Email |
||
| 119 | { |
||
| 120 | return $this->input(Email::class, $formModel, $attribute, $config); |
||
| 121 | } |
||
| 122 | |||
| 123 | public function errorSummary(FormModelInterface $formModel, array $config = []): ErrorSummary |
||
| 124 | { |
||
| 125 | return $this |
||
| 126 | ->field(ErrorSummary::class, $config) |
||
| 127 | ->formModel($formModel); |
||
| 128 | } |
||
| 129 | |||
| 130 | public function fieldset(array $config = []): Fieldset |
||
| 131 | { |
||
| 132 | return $this->field(Fieldset::class, $config); |
||
| 133 | } |
||
| 134 | |||
| 135 | public function file(FormModelInterface $formModel, string $attribute, array $config = []): File |
||
| 136 | { |
||
| 137 | return $this->input(File::class, $formModel, $attribute, $config); |
||
| 138 | } |
||
| 139 | |||
| 140 | public function hidden(FormModelInterface $formModel, string $attribute, array $config = []): Hidden |
||
| 141 | { |
||
| 142 | return $this->input(Hidden::class, $formModel, $attribute, $config); |
||
| 143 | } |
||
| 144 | |||
| 145 | public function image(array $config = []): Image |
||
| 146 | { |
||
| 147 | return $this->field(Image::class, $config); |
||
| 148 | } |
||
| 149 | |||
| 150 | public function number(FormModelInterface $formModel, string $attribute, array $config = []): Number |
||
| 151 | { |
||
| 152 | return $this->input(Number::class, $formModel, $attribute, $config); |
||
| 153 | } |
||
| 154 | |||
| 155 | public function password(FormModelInterface $formModel, string $attribute, array $config = []): Password |
||
| 156 | { |
||
| 157 | return $this->input(Password::class, $formModel, $attribute, $config); |
||
| 158 | } |
||
| 159 | |||
| 160 | public function radioList(FormModelInterface $formModel, string $attribute, array $config = []): RadioList |
||
| 161 | { |
||
| 162 | return $this |
||
| 163 | ->field(RadioList::class, $config) |
||
| 164 | ->formAttribute($formModel, $attribute); |
||
| 165 | } |
||
| 166 | |||
| 167 | public function range(FormModelInterface $formModel, string $attribute, array $config = []): Range |
||
| 168 | { |
||
| 169 | return $this->input(Range::class, $formModel, $attribute, $config); |
||
| 170 | } |
||
| 171 | |||
| 172 | public function resetButton(array $config = []): ResetButton |
||
| 175 | } |
||
| 176 | |||
| 177 | public function select(FormModelInterface $formModel, string $attribute, array $config = []): Select |
||
| 178 | { |
||
| 179 | return $this->input(Select::class, $formModel, $attribute, $config); |
||
| 180 | } |
||
| 181 | |||
| 182 | public function submitButton(array $config = []): SubmitButton |
||
| 183 | { |
||
| 184 | return $this->field(SubmitButton::class, $config); |
||
| 185 | } |
||
| 186 | |||
| 187 | public function telephone(FormModelInterface $formModel, string $attribute, array $config = []): Telephone |
||
| 188 | { |
||
| 189 | return $this->input(Telephone::class, $formModel, $attribute, $config); |
||
| 190 | } |
||
| 191 | |||
| 192 | public function text(FormModelInterface $formModel, string $attribute, array $config = []): Text |
||
| 193 | { |
||
| 194 | return $this->input(Text::class, $formModel, $attribute, $config); |
||
| 195 | } |
||
| 196 | |||
| 197 | public function textarea(FormModelInterface $formModel, string $attribute, array $config = []): Textarea |
||
| 198 | { |
||
| 199 | return $this->input(Textarea::class, $formModel, $attribute, $config); |
||
| 200 | } |
||
| 201 | |||
| 202 | public function url(FormModelInterface $formModel, string $attribute, array $config = []): Url |
||
| 203 | { |
||
| 204 | return $this->input(Url::class, $formModel, $attribute, $config); |
||
| 205 | } |
||
| 206 | |||
| 207 | public function label(FormModelInterface $formModel, string $attribute, array $config = []): Label |
||
| 214 | } |
||
| 215 | |||
| 216 | public function hint(FormModelInterface $formModel, string $attribute, array $config = []): Hint |
||
| 217 | { |
||
| 218 | $widgetConfig = array_merge( |
||
| 219 | $this->hintConfig, |
||
| 220 | $config, |
||
| 221 | ); |
||
| 222 | return Hint::widget($widgetConfig)->formAttribute($formModel, $attribute); |
||
| 223 | } |
||
| 224 | |||
| 225 | public function error(FormModelInterface $formModel, string $attribute, array $config = []): Error |
||
| 226 | { |
||
| 227 | $widgetConfig = array_merge( |
||
| 228 | $this->errorConfig, |
||
| 229 | $config, |
||
| 230 | ); |
||
| 231 | return Error::widget($widgetConfig)->formAttribute($formModel, $attribute); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @psalm-template T |
||
| 236 | * @psalm-param class-string<T> $class |
||
| 237 | * @psalm-return T |
||
| 238 | */ |
||
| 239 | public function input(string $class, FormModelInterface $formModel, string $attribute, array $config = []): object |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @psalm-template T |
||
| 256 | * @psalm-param class-string<T> $class |
||
| 257 | * @psalm-return T |
||
| 258 | */ |
||
| 259 | public function field(string $class, array $config = []): object |
||
| 260 | { |
||
| 261 | $config = array_merge( |
||
| 262 | $this->makeFieldConfig($class), |
||
| 263 | $this->fieldConfigs[$class] ?? [], |
||
| 264 | $config, |
||
| 265 | ['class' => $class], |
||
| 266 | ); |
||
| 267 | |||
| 268 | /** @psalm-var T */ |
||
| 269 | return WidgetFactory::createWidget($config); |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @psalm-param class-string $class |
||
| 274 | */ |
||
| 275 | private function makeFieldConfig(string $class): array |
||
| 356 | } |
||
| 357 | } |
||
| 358 |