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()  | 
            ||
| 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)  | 
            ||
| 218 | |||
| 219 | /**  | 
            ||
| 220 | * get id entity created by the formular  | 
            ||
| 221 | *  | 
            ||
| 222 | * @access public  | 
            ||
| 223 | * @return int  | 
            ||
| 224 | */  | 
            ||
| 225 | public function getIdEntityCreated() : int  | 
            ||
| 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()  | 
            ||
| 361 | |||
| 362 | /**  | 
            ||
| 363 | * get an element of formular  | 
            ||
| 364 | *  | 
            ||
| 365 | * @access public  | 
            ||
| 366 | * @param string $sName name  | 
            ||
| 367 | * @return object  | 
            ||
| 368 | */  | 
            ||
| 369 | public function get($sName)  | 
            ||
| 373 | |||
| 374 | /**  | 
            ||
| 375 | * get the form separator  | 
            ||
| 376 | *  | 
            ||
| 377 | * @access public  | 
            ||
| 378 | * @return string  | 
            ||
| 379 | */  | 
            ||
| 380 | public function getSeparator()  | 
            ||
| 384 | |||
| 385 | /**  | 
            ||
| 386 | * set the form separator  | 
            ||
| 387 | *  | 
            ||
| 388 | * @access public  | 
            ||
| 389 | * @param string $sSeparator separator between the fields  | 
            ||
| 390 | * @return \Venus\lib\Form  | 
            ||
| 391 | */  | 
            ||
| 392 | public function setSeparator($sSeparator)  | 
            ||
| 397 | |||
| 398 | /**  | 
            ||
| 399 | * set the entity to synchronize with the formular  | 
            ||
| 400 | *  | 
            ||
| 401 | * @access public  | 
            ||
| 402 | * @param $sSynchronizeEntity  | 
            ||
| 403 | * @param int $iId id of the primary key  | 
            ||
| 404 | * @return Form  | 
            ||
| 405 | * @internal param string $sSeparator separator between the fields  | 
            ||
| 406 | */  | 
            ||
| 407 | public function synchronizeEntity($sSynchronizeEntity, $iId = null)  | 
            ||
| 414 | |||
| 415 | /**  | 
            ||
| 416 | * add constraint  | 
            ||
| 417 | *  | 
            ||
| 418 | * @access public  | 
            ||
| 419 | * @param string $sName field name  | 
            ||
| 420 | * @param object $oConstraint constraint on the field  | 
            ||
| 421 | * @return \Venus\lib\Form  | 
            ||
| 422 | */  | 
            ||
| 423 | public function addConstraint($sName, $oConstraint)  | 
            ||
| 432 | |||
| 433 | /**  | 
            ||
| 434 | * get all elements  | 
            ||
| 435 | *  | 
            ||
| 436 | * @access public  | 
            ||
| 437 | * @return array  | 
            ||
| 438 | */  | 
            ||
| 439 |     public function getElement() { | 
            ||
| 443 | |||
| 444 | /**  | 
            ||
| 445 | * get all elements  | 
            ||
| 446 | *  | 
            ||
| 447 | * @access public  | 
            ||
| 448 | * @return int  | 
            ||
| 449 | */  | 
            ||
| 450 |     public function getIdEntity() { | 
            ||
| 454 | |||
| 455 | /**  | 
            ||
| 456 | * get all elements  | 
            ||
| 457 | *  | 
            ||
| 458 | * @access public  | 
            ||
| 459 | * @return string  | 
            ||
| 460 | */  | 
            ||
| 461 |     public function getSynchronizeEntity() { | 
            ||
| 465 | }  | 
            ||
| 466 | 
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: