Complex classes like AbstractType 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 AbstractType, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | abstract class AbstractType |
||
| 20 | { |
||
| 21 | |||
| 22 | /** @var array */ |
||
| 23 | protected $definition = []; |
||
| 24 | |||
| 25 | /** @var string */ |
||
| 26 | protected $type = ''; |
||
| 27 | |||
| 28 | /** @var string */ |
||
| 29 | protected $label = ''; |
||
| 30 | |||
| 31 | /** @var string */ |
||
| 32 | protected $value = ''; |
||
| 33 | |||
| 34 | /** @var string */ |
||
| 35 | protected $name = ''; |
||
| 36 | |||
| 37 | /** @var string */ |
||
| 38 | protected $outputPattern = ''; |
||
| 39 | |||
| 40 | /** @var string */ |
||
| 41 | protected $element = ''; |
||
| 42 | |||
| 43 | /** @var array */ |
||
| 44 | protected $errorMessages = []; |
||
| 45 | |||
| 46 | /** @var string */ |
||
| 47 | protected $formIdentifier = ''; |
||
| 48 | |||
| 49 | /** @var AbstractValidator|null */ |
||
| 50 | protected $defaultValidator = null; |
||
| 51 | |||
| 52 | /** @var ValidatorChain */ |
||
| 53 | protected $validatorChain = null; |
||
| 54 | |||
| 55 | /** @var array */ |
||
| 56 | protected $formErrorDecoration = []; |
||
| 57 | |||
| 58 | /** @var ServiceLocatorInterface */ |
||
| 59 | protected $serviceLocator; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * AbstractType constructor. |
||
| 63 | * |
||
| 64 | * @param array $definition |
||
| 65 | * @param array $formErrorDecoration |
||
| 66 | * @param string $formIdentifier |
||
| 67 | */ |
||
| 68 | public function __construct(array $definition, array $formErrorDecoration = [], string $formIdentifier = '') |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Get default validator |
||
| 77 | * |
||
| 78 | * @return AbstractValidator|null |
||
| 79 | */ |
||
| 80 | public function getDefaultValidator() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Set default validator |
||
| 87 | * |
||
| 88 | * @param AbstractValidator $validator |
||
| 89 | */ |
||
| 90 | public function setDefaultValidator(AbstractValidator $validator) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Set validator chain |
||
| 97 | * |
||
| 98 | * @param ValidatorChain $validatorChain |
||
| 99 | */ |
||
| 100 | public function setValidatorChain(ValidatorChain $validatorChain) |
||
| 104 | |||
| 105 | public function getValidatorChain():? ValidatorChain |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Validate field by validator chain or default validator |
||
| 112 | * |
||
| 113 | * @return bool|null |
||
| 114 | */ |
||
| 115 | public function isValid() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Remove bound validators |
||
| 128 | * |
||
| 129 | * @return void |
||
| 130 | */ |
||
| 131 | public function removeValidators() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Get error messages |
||
| 139 | * |
||
| 140 | * @return array|string |
||
| 141 | */ |
||
| 142 | public function getErrorMessages() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Set error messages |
||
| 174 | * |
||
| 175 | * @param array $messages |
||
| 176 | */ |
||
| 177 | public function setErrorMessages(array $messages) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Get field type |
||
| 184 | * |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | public function getType() :string |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param string $type |
||
| 194 | * |
||
| 195 | * @return self |
||
| 196 | */ |
||
| 197 | public function setType(string $type) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public function getLabel() :string |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $label |
||
| 213 | * |
||
| 214 | * @return self |
||
| 215 | */ |
||
| 216 | public function setLabel(string $label) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @return string |
||
| 224 | */ |
||
| 225 | public function getValue() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param mixed $value |
||
| 232 | * |
||
| 233 | * @return self |
||
| 234 | */ |
||
| 235 | public function setValue($value) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @return string |
||
| 243 | */ |
||
| 244 | public function getName() :string |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param string $name |
||
| 251 | * |
||
| 252 | * @return self |
||
| 253 | */ |
||
| 254 | public function setName(string $name) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Add field attribute |
||
| 262 | * |
||
| 263 | * @param string $key |
||
| 264 | * @param string $value |
||
| 265 | * |
||
| 266 | * @return self |
||
| 267 | */ |
||
| 268 | public function addAttribute(string $key, string $value) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @return bool |
||
| 281 | */ |
||
| 282 | protected function isPost() :bool |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @return self |
||
| 289 | */ |
||
| 290 | public function create() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | public function __toString() :string |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @return bool |
||
| 324 | */ |
||
| 325 | private function _translateLabelsAndPlaceholders() :bool |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param string $type |
||
| 340 | * |
||
| 341 | * @return bool |
||
| 342 | */ |
||
| 343 | private function _translateType(string $type) :bool |
||
| 373 | |||
| 374 | public function getFormIdentifier() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @return ServiceLocatorInterface |
||
| 381 | */ |
||
| 382 | private function _getServiceLocator() |
||
| 386 | |||
| 387 | } |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: