Complex classes like Form 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 Form, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 61 | class Form |
||
| 62 | { |
||
| 63 | /** |
||
| 64 | * Elements of the form |
||
| 65 | * |
||
| 66 | * @access private |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | private $_aElement = array(); |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Increment for form |
||
| 73 | * |
||
| 74 | * @access private |
||
| 75 | * @var int |
||
| 76 | */ |
||
| 77 | private static $_iFormIncrement = 0; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * number of form |
||
| 81 | * |
||
| 82 | * @access private |
||
| 83 | * @var int |
||
| 84 | */ |
||
| 85 | private $_iFormNumber = 0; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Separator between fields of form |
||
| 89 | * |
||
| 90 | * @access private |
||
| 91 | * @var string |
||
| 92 | */ |
||
| 93 | private $_sSeparator = '<br/>'; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * The entity to save with the formular |
||
| 97 | * |
||
| 98 | * @access private |
||
| 99 | * @var string |
||
| 100 | */ |
||
| 101 | private $_sSynchronizeEntity = null; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * The id of entity |
||
| 105 | * |
||
| 106 | * @access private |
||
| 107 | * @var int |
||
| 108 | */ |
||
| 109 | private $_iIdEntity = null; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * The entity to save with the formular |
||
| 113 | * |
||
| 114 | * @access private |
||
| 115 | * @var int |
||
| 116 | */ |
||
| 117 | private $_iIdEntityCreated = null; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * constructor that it increment (static) for all use |
||
| 121 | * |
||
| 122 | * @access public |
||
| 123 | */ |
||
| 124 | public function __construct() |
||
| 125 | { |
||
| 126 | self::$_iFormIncrement++; |
||
| 127 | $this->_iFormNumber = self::$_iFormIncrement; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * add an element in the form |
||
| 132 | * |
||
| 133 | * @access public |
||
| 134 | * @param string $sName name |
||
| 135 | * @param string|\Venus\lib\Form $mType type of field |
||
| 136 | * @param string $sLabel label of field |
||
| 137 | * @param mixed $mValue value of field |
||
| 138 | * @parma mixed $mOptions options (for select) |
||
| 139 | * @return \Venus\lib\Form |
||
| 140 | */ |
||
| 141 | public function add($sName, $mType, $sLabel = null, $mValue = null, $mOptions = null) |
||
| 142 | { |
||
| 143 | if ($mType instanceof Container) { |
||
| 144 | |||
| 145 | $this->_aElement[$sName] = $mType; |
||
| 146 | } else if ($mType === 'text' || $mType === 'submit' || $mType === 'password' || $mType === 'file' || $mType === 'tel' |
||
| 147 | || $mType === 'url' || $mType === 'email' || $mType === 'search' || $mType === 'date' || $mType === 'time' |
||
| 148 | || $mType === 'datetime' || $mType === 'month' || $mType === 'week' || $mType === 'number' || $mType === 'range' |
||
| 149 | || $mType === 'color') { |
||
| 150 | |||
| 151 | $this->_aElement[$sName] = new Input($sName, $mType, $sLabel, $mValue); |
||
| 152 | } elseif ($mType === 'textarea') { |
||
| 153 | |||
| 154 | $this->_aElement[$sName] = new Textarea($sName, $sLabel, $mValue); |
||
| 155 | } else if ($mType === 'select') { |
||
| 156 | |||
| 157 | $this->_aElement[$sName] = new Select($sName, $mOptions, $sLabel, $mValue); |
||
| 158 | } else if ($mType === 'label') { |
||
| 159 | |||
| 160 | $this->_aElement[$sName] = new Label($sName); |
||
| 161 | } else if ($mType === 'list_checkbox') { |
||
| 162 | |||
| 163 | $i = 0; |
||
| 164 | |||
| 165 | $this->_aElement[$sName.'_'.$i++] = new Label($sLabel); |
||
| 166 | |||
| 167 | foreach ($mValue as $mKey => $sValue) { |
||
| 168 | |||
| 169 | $this->_aElement[$sName.'_'.$i++] = new Checkbox($sName, $sValue, $mKey, $mOptions); |
||
| 170 | } |
||
| 171 | } else if ($mType === 'checkbox') { |
||
| 172 | |||
| 173 | $this->_aElement[$sName] = new Checkbox($sName, $sLabel, $mValue, $mOptions); |
||
| 174 | } else if ($mType === 'radio') { |
||
| 175 | |||
| 176 | $this->_aElement[$sName.rand(100000, 999999)] = new Radio($sName, $sLabel, $mValue, $mOptions); |
||
| 177 | } else if ($mType === 'date') { |
||
| 178 | |||
| 179 | $aDay = array(); |
||
| 180 | |||
| 181 | for ($i = 1; $i <= 31; $i++) { |
||
| 182 | |||
| 183 | if ($i < 10) { $aDay['0'.$i] = '0'.$i; } |
||
| 184 | else { $aDay[$i] = $i; } |
||
| 185 | } |
||
| 186 | |||
| 187 | $this->_aElement[$sName.'_day'] = new Select($sName, $aDay); |
||
| 188 | |||
| 189 | $aMonth = array( |
||
| 190 | '01' => 'Jan', |
||
| 191 | '02' => 'Feb', |
||
| 192 | '03' => 'Mar', |
||
| 193 | '04' => 'Apr', |
||
| 194 | '05' => 'May', |
||
| 195 | '06' => 'Jun', |
||
| 196 | '07' => 'Jui', |
||
| 197 | '08' => 'Aug', |
||
| 198 | '09' => 'Sep', |
||
| 199 | '10' => 'Oct', |
||
| 200 | '11' => 'Nov', |
||
| 201 | '12' => 'Dec', |
||
| 202 | ); |
||
| 203 | |||
| 204 | $this->_aElement[$sName.'_month'] = new Select($sName, $aMonth); |
||
| 205 | |||
| 206 | $aYear = array(); |
||
| 207 | |||
| 208 | for ($i = 1900; $i <= 2013; $i++) { |
||
| 209 | |||
| 210 | $aYear[$i] = $i; |
||
| 211 | } |
||
| 212 | |||
| 213 | $this->_aElement[$sName.'_year'] = new Select($sName, $aMonth); |
||
| 214 | } |
||
| 215 | |||
| 216 | return $this; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * get id entity created by the formular |
||
| 221 | * |
||
| 222 | * @access public |
||
| 223 | * @return int |
||
| 224 | */ |
||
| 225 | public function getIdEntityCreated() : int |
||
| 226 | { |
||
| 227 | return $this->_iIdEntityCreated; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * set id entity created by the formular |
||
| 232 | * |
||
| 233 | * @access public |
||
| 234 | * @param int $iIdEntityCreated |
||
| 235 | * @return Form |
||
| 236 | */ |
||
| 237 | public function setIdEntityCreated(int $iIdEntityCreated) : Form |
||
| 242 | |||
| 243 | /** |
||
| 244 | * get form number |
||
| 245 | * |
||
| 246 | * @access public |
||
| 247 | * @return int |
||
| 248 | */ |
||
| 249 | public function getFormNumber() : int |
||
| 253 | |||
| 254 | /** |
||
| 255 | * set id entity created by the formular |
||
| 256 | * |
||
| 257 | * @access public |
||
| 258 | * @param int $iFormNumber |
||
| 259 | * @return Form |
||
| 260 | */ |
||
| 261 | public function setFormNumber(int $iFormNumber) : Form |
||
| 266 | |||
| 267 | /** |
||
| 268 | * get global form |
||
| 269 | * |
||
| 270 | * @access public |
||
| 271 | * @return \Venus\lib\Form\Container |
||
| 272 | */ |
||
| 273 | public function getForm() |
||
| 292 | |||
| 293 | |||
| 294 | /** |
||
| 295 | * get global object form |
||
| 296 | * |
||
| 297 | * @access public |
||
| 298 | * @return \stdClass |
||
| 299 | */ |
||
| 300 | public function getFormInObject() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * get an element of formular |
||
| 366 | * |
||
| 367 | * @access public |
||
| 368 | * @param string $sName name |
||
| 369 | * @return object |
||
| 370 | */ |
||
| 371 | public function get($sName) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * get the form separator |
||
| 378 | * |
||
| 379 | * @access public |
||
| 380 | * @return string |
||
| 381 | */ |
||
| 382 | public function getSeparator() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * set the form separator |
||
| 389 | * |
||
| 390 | * @access public |
||
| 391 | * @param string $sSeparator separator between the fields |
||
| 392 | * @return \Venus\lib\Form |
||
| 393 | */ |
||
| 394 | public function setSeparator($sSeparator) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * set the entity to synchronize with the formular |
||
| 402 | * |
||
| 403 | * @access public |
||
| 404 | * @param $sSynchronizeEntity |
||
| 405 | * @param int $iId id of the primary key |
||
| 406 | * @return Form |
||
| 407 | * @internal param string $sSeparator separator between the fields |
||
| 408 | */ |
||
| 409 | public function synchronizeEntity($sSynchronizeEntity, $iId = null) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * add constraint |
||
| 419 | * |
||
| 420 | * @access public |
||
| 421 | * @param string $sName field name |
||
| 422 | * @param object $oConstraint constraint on the field |
||
| 423 | * @return \Venus\lib\Form |
||
| 424 | */ |
||
| 425 | public function addConstraint($sName, $oConstraint) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * get all elements |
||
| 437 | * |
||
| 438 | * @access public |
||
| 439 | * @return array |
||
| 440 | */ |
||
| 441 | public function getElement() { |
||
| 445 | |||
| 446 | /** |
||
| 447 | * get all elements |
||
| 448 | * |
||
| 449 | * @access public |
||
| 450 | * @return int |
||
| 451 | */ |
||
| 452 | public function getIdEntity() { |
||
| 456 | |||
| 457 | /** |
||
| 458 | * get all elements |
||
| 459 | * |
||
| 460 | * @access public |
||
| 461 | * @return string |
||
| 462 | */ |
||
| 463 | public function getSynchronizeEntity() { |
||
| 467 | } |
||
| 468 |