Complex classes like Type 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 Type, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | abstract class Type extends FieldType |
||
| 25 | { |
||
| 26 | /** @var \eZ\Publish\SPI\FieldType\ValueSerializerInterface */ |
||
| 27 | protected $serializer; |
||
| 28 | |||
| 29 | /** @var \Symfony\Component\Validator\Validator\ValidatorInterface */ |
||
| 30 | protected $validator; |
||
| 31 | |||
| 32 | public function __construct(ValueSerializerInterface $serializer, ValidatorInterface $validator) |
||
| 37 | |||
| 38 | public function getName(Value $value, FieldDefinition $fieldDefinition, string $languageCode): string |
||
| 42 | |||
| 43 | public function getEmptyValue(): Value |
||
| 49 | |||
| 50 | public function fromHash($hash): Value |
||
| 58 | |||
| 59 | public function toHash(Value $value): ?array |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @see https://symfony.com/doc/current/validation/raw_values.html |
||
| 70 | */ |
||
| 71 | protected function getFieldSettingsConstraints(): ?Assert\Collection |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @see https://symfony.com/doc/current/validation/raw_values.html |
||
| 78 | */ |
||
| 79 | protected function getFieldValueConstraints(FieldDefinition $fieldDefinition): ?Assert\Collection |
||
| 83 | |||
| 84 | protected function mapConstraintViolationList(ConstraintViolationListInterface $constraintViolationList): array |
||
| 95 | |||
| 96 | public function getSettingsSchema(): array |
||
| 100 | |||
| 101 | public function getValidatorConfigurationSchema(): array |
||
| 105 | |||
| 106 | public function validate(FieldDefinition $fieldDefinition, Value $value): array |
||
| 116 | |||
| 117 | public function validateValidatorConfiguration($validatorConfiguration): array |
||
| 130 | |||
| 131 | public function applyDefaultValidatorConfiguration(&$validatorConfiguration): void |
||
| 151 | |||
| 152 | public function validateFieldSettings($fieldSettings): array |
||
| 168 | |||
| 169 | public function applyDefaultSettings(&$fieldSettings): void |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Returns information for FieldValue->$sortKey relevant to the field type. |
||
| 185 | * |
||
| 186 | * Return value is mixed. It should be something which is sensible for |
||
| 187 | * sorting. |
||
| 188 | * |
||
| 189 | * It is up to the persistence implementation to handle those values. |
||
| 190 | * Common string and integer values are safe. |
||
| 191 | * |
||
| 192 | * For the legacy storage it is up to the field converters to set this |
||
| 193 | * value in either sort_key_string or sort_key_int. |
||
| 194 | * |
||
| 195 | * In case of multi value, values should be string and separated by "-" or ",". |
||
| 196 | * |
||
| 197 | * @param \eZ\Publish\Core\FieldType\Value $value |
||
| 198 | * |
||
| 199 | * @return mixed |
||
| 200 | */ |
||
| 201 | protected function getSortInfo(Value $value) |
||
| 205 | |||
| 206 | public function toPersistenceValue(Value $value): PersistenceValue |
||
| 216 | |||
| 217 | public function fromPersistenceValue(PersistenceValue $fieldValue) |
||
| 221 | |||
| 222 | public function isSearchable(): bool |
||
| 226 | |||
| 227 | public function isSingular(): bool |
||
| 231 | |||
| 232 | public function onlyEmptyInstance(): bool |
||
| 236 | |||
| 237 | public function isEmptyValue(Value $value): bool |
||
| 241 | |||
| 242 | final public function acceptValue($inputValue): Value |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Inspects given $inputValue and potentially converts it into a dedicated value object. |
||
| 261 | * |
||
| 262 | * If given $inputValue could not be converted or is already an instance of dedicate value object, |
||
| 263 | * the method should simply return it. |
||
| 264 | * |
||
| 265 | * This is an operation method for {@see acceptValue()}. |
||
| 266 | * |
||
| 267 | * Example implementation: |
||
| 268 | * <code> |
||
| 269 | * protected function createValueFromInput( $inputValue ) |
||
| 270 | * { |
||
| 271 | * if ( is_array( $inputValue ) ) |
||
| 272 | * { |
||
| 273 | * $inputValue = \eZ\Publish\Core\FieldType\CookieJar\Value( $inputValue ); |
||
| 274 | * } |
||
| 275 | * |
||
| 276 | * return $inputValue; |
||
| 277 | * } |
||
| 278 | * </code> |
||
| 279 | * |
||
| 280 | * @param mixed $inputValue |
||
| 281 | * |
||
| 282 | * @return mixed The potentially converted input value. |
||
| 283 | */ |
||
| 284 | protected function createValueFromInput($inputValue) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Returns FQN of class representing Field Type Value. |
||
| 298 | * |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | protected function getValueClass(): string |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Throws an exception if the given $value is not an instance of the supported value subtype. |
||
| 308 | * |
||
| 309 | * This is an operation method for {@see acceptValue()}. |
||
| 310 | * |
||
| 311 | * Default implementation expects the value class to reside in the same namespace as its |
||
| 312 | * FieldType class and is named "Value". |
||
| 313 | * |
||
| 314 | * Example implementation: |
||
| 315 | * <code> |
||
| 316 | * protected function checkValueType($value): void |
||
| 317 | * { |
||
| 318 | * if ( !$inputValue instanceof \eZ\Publish\Core\FieldType\CookieJar\Value ) ) |
||
| 319 | * { |
||
| 320 | * throw new InvalidArgumentException( "Given value type is not supported." ); |
||
| 321 | * } |
||
| 322 | * } |
||
| 323 | * </code> |
||
| 324 | * |
||
| 325 | * @param mixed $value A value returned by {@see createValueFromInput()}. |
||
| 326 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If the parameter is not an instance of the supported value subtype. |
||
| 327 | */ |
||
| 328 | protected function checkValueType($value): void |
||
| 335 | |||
| 336 | public function fieldSettingsToHash($fieldSettings) |
||
| 340 | |||
| 341 | public function fieldSettingsFromHash($fieldSettingsHash) |
||
| 345 | |||
| 346 | public function validatorConfigurationToHash($validatorConfiguration) |
||
| 350 | |||
| 351 | public function validatorConfigurationFromHash($validatorConfiguration) |
||
| 355 | |||
| 356 | public function getRelations(Value $value): array |
||
| 360 | } |
||
| 361 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.