Complex classes like Field 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 Field, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | abstract class Field extends Component |
||
| 11 | { |
||
| 12 | |||
| 13 | protected |
||
| 14 | $label, |
||
| 15 | $visible = true, |
||
| 16 | $enabled = true, |
||
| 17 | $restored = false, |
||
| 18 | $value, |
||
| 19 | $initialValue, |
||
| 20 | $latestErrorMsg = "", |
||
| 21 | $forceInvalid = false, |
||
| 22 | $validators = array(), |
||
| 23 | $sanitizers = array(), |
||
| 24 | $collectData = true, |
||
| 25 | $storeValueLocally; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Creates a new form field component |
||
| 29 | * |
||
| 30 | * @param string $slug internal slug to use |
||
| 31 | * @param string $label label to display |
||
| 32 | * @param string|string[] $value initial value |
||
| 33 | * @param int $storeValueLocally how long to store last used value in cookie (set to 0 for no cookie) |
||
| 34 | */ |
||
| 35 | public function __construct ($slug, $label = '', $value = '', $storeValueLocally = 0) |
||
| 42 | |||
| 43 | public function reset () |
||
| 48 | |||
| 49 | public function setCollectData ($collectData) |
||
| 53 | |||
| 54 | public function getCollectData () |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Treats the value that was fetched from the request variables. If the control uses a non-scalar to represent its |
||
| 61 | * value, it should be instantiated here |
||
| 62 | * @param string $value |
||
| 63 | * @return string |
||
| 64 | */ |
||
| 65 | public function importValue ($value) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Restores the value from the provided method |
||
| 72 | * @param Method $method |
||
| 73 | * @param bool $sanitize Whether to sanitize the fetched value |
||
| 74 | */ |
||
| 75 | public function restoreValue (Method $method, $sanitize = true) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Condenses multiple values (e.g. from a multiple select box) into a single string |
||
| 94 | * |
||
| 95 | * @param string[] $values |
||
| 96 | * |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | public function condenseMultiple ($values = []) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Extracts multiple values from a single string |
||
| 106 | * |
||
| 107 | * @param string $value |
||
| 108 | * |
||
| 109 | * @return string[] |
||
| 110 | */ |
||
| 111 | public function extractMultiple ($value) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Determines whether this field allows selection of multiple values |
||
| 118 | * @return bool |
||
| 119 | */ |
||
| 120 | public function isMultiple () |
||
| 124 | |||
| 125 | public function submit () |
||
| 130 | |||
| 131 | public function preprocess () |
||
| 136 | |||
| 137 | private function getCookieName () |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Gets the value of the name attribute for this field |
||
| 144 | * |
||
| 145 | * @param bool $asMarkup Whether to retrieve the value used in the markup (if true, [] will by default be appended |
||
| 146 | * to fields that allow multiple values) |
||
| 147 | * |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | public function getName ($asMarkup = false) |
||
| 154 | |||
| 155 | public function getLabel () |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Whether to condense multiple values into a single string |
||
| 162 | * |
||
| 163 | * @param bool $condense |
||
| 164 | * |
||
| 165 | * @return string|string[] |
||
| 166 | */ |
||
| 167 | public function getValue ($condense = true) |
||
| 174 | |||
| 175 | public function setValue ($value) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Sets whether the component is visible |
||
| 183 | * |
||
| 184 | * @param boolean $visible |
||
| 185 | * |
||
| 186 | * @return $this |
||
| 187 | */ |
||
| 188 | public function setVisible ($visible = true) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Checks whether the component is visible |
||
| 196 | * @return boolean |
||
| 197 | */ |
||
| 198 | public function isVisible () |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return array |
||
| 205 | */ |
||
| 206 | public function getJsonData () |
||
| 227 | |||
| 228 | public function getId () |
||
| 232 | |||
| 233 | public function getAttributes () |
||
| 244 | |||
| 245 | public function getClasses () |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Adds validator |
||
| 258 | * |
||
| 259 | * @param FieldValidator $v |
||
| 260 | * @param boolean $unshift Whether to add the validator to the front of the array |
||
| 261 | * |
||
| 262 | * @return Field |
||
| 263 | */ |
||
| 264 | public function addValidator (FieldValidator $v, $unshift = false) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Adds sanitizer |
||
| 276 | * |
||
| 277 | * @param FieldSanitizer $s |
||
| 278 | * |
||
| 279 | * @return Field |
||
| 280 | */ |
||
| 281 | public function addSanitizer (FieldSanitizer $s) |
||
| 286 | |||
| 287 | public function forceInvalid () |
||
| 291 | |||
| 292 | public function isValid () |
||
| 308 | |||
| 309 | public function sanitize () |
||
| 315 | |||
| 316 | public function getLatestErrorMsg () |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Returns the default value used if the POST var is not set |
||
| 323 | * @return string |
||
| 324 | */ |
||
| 325 | protected function getRestoreDefault () |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Sets whether the control is enabled |
||
| 332 | * @param boolean $enabled |
||
| 333 | * @return $this |
||
| 334 | */ |
||
| 335 | public function setEnabled ($enabled) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Gets the script that will instantiate ONLY this field in an anonymous form |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | public function getScript () |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Gets the script tag that will instantiate ONLY this field in an anonymous form |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | public function getScriptHTML () |
||
| 360 | } |
||
| 361 |