| Conditions | 16 |
| Paths | 282 |
| Total Lines | 89 |
| Code Lines | 49 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 36 | function rebuildObjectClass($destdir, $module, $objectname, $newmask) |
||
| 37 | { |
||
| 38 | global $db, $langs; |
||
| 39 | |||
| 40 | if (empty($objectname)) return -1; |
||
| 41 | |||
| 42 | $pathoffiletoeditsrc=$destdir.'/class/'.strtolower($objectname).'.class.php'; |
||
| 43 | $pathoffiletoedittarget=$destdir.'/class/'.strtolower($objectname).'.class.php'; |
||
| 44 | if (! dol_is_file($pathoffiletoeditsrc)) |
||
| 45 | { |
||
| 46 | //$pathoffiletoeditsrc=DOL_DOCUMENT_ROOT.'/modulebuilder/template/class/myobject.class.php'; |
||
| 47 | setEventMessages($langs->trans("ErrorFileNotFound", $pathoffiletoeditsrc), null, 'errors'); |
||
| 48 | return -1; |
||
| 49 | } |
||
| 50 | |||
| 51 | //$pathoffiletoedittmp=$destdir.'/class/'.strtolower($objectname).'.class.php.tmp'; |
||
| 52 | //dol_delete_file($pathoffiletoedittmp, 0, 1, 1); |
||
| 53 | |||
| 54 | try |
||
| 55 | { |
||
| 56 | include_once $pathoffiletoeditsrc; |
||
| 57 | if (class_exists($objectname)) $object=new $objectname($db); |
||
| 58 | else return -1; |
||
| 59 | |||
| 60 | // Backup old file |
||
| 61 | dol_copy($pathoffiletoeditsrc, $pathoffiletoeditsrc.'.back', $newmask, 1); |
||
| 62 | |||
| 63 | // Edit class files |
||
| 64 | $contentclass = file_get_contents(dol_osencode($pathoffiletoeditsrc), 'r'); |
||
| 65 | |||
| 66 | $i=0; |
||
| 67 | $texttoinsert = '// BEGIN MODULEBUILDER PROPERTIES'."\n"; |
||
| 68 | $texttoinsert.= "\t".'/**'."\n"; |
||
| 69 | $texttoinsert.= "\t".' * @var array Array with all fields and their property'."\n"; |
||
| 70 | $texttoinsert.= "\t".' */'."\n"; |
||
| 71 | $texttoinsert.= "\t".'public $fields=array('."\n"; |
||
| 72 | |||
| 73 | if (count($object->fields)) |
||
| 74 | { |
||
| 75 | foreach($object->fields as $key => $val) |
||
| 76 | { |
||
| 77 | $i++; |
||
| 78 | $typephp=''; |
||
| 79 | $texttoinsert.= "\t\t'".$key."' => array('type'=>'".$val['type']."', 'label'=>'".$val['label']."',"; |
||
| 80 | $texttoinsert.= " 'visible'=>".($val['visible']?$val['visible']:0).","; |
||
| 81 | $texttoinsert.= " 'enabled'=>".($val['enabled']?$val['enabled']:0).","; |
||
| 82 | if ($val['position']) $texttoinsert.= " 'position'=>".$val['position'].","; |
||
| 83 | if ($val['notnull']) $texttoinsert.= " 'notnull'=>".$val['notnull'].","; |
||
| 84 | if ($val['index']) $texttoinsert.= " 'index'=>".$val['index'].","; |
||
| 85 | if ($val['searchall']) $texttoinsert.= " 'searchall'=>".$val['searchall'].","; |
||
| 86 | if ($val['comment']) $texttoinsert.= " 'comment'=>'".$val['comment']."',"; |
||
| 87 | $texttoinsert.= "),\n"; |
||
| 88 | } |
||
| 89 | } |
||
| 90 | $texttoinsert.= "\t".');'."\n"; |
||
| 91 | |||
| 92 | $texttoinsert.= "\n"; |
||
| 93 | |||
| 94 | if (count($object->fields)) |
||
| 95 | { |
||
| 96 | foreach($object->fields as $key => $val) |
||
| 97 | { |
||
| 98 | $i++; |
||
| 99 | $typephp=''; |
||
| 100 | $texttoinsert.= "\t".'public $'.$key.$typephp.";"; |
||
| 101 | //if ($key == 'rowid') $texttoinsert.= ' AUTO_INCREMENT PRIMARY KEY'; |
||
| 102 | //if ($key == 'entity') $texttoinsert.= ' DEFAULT 1'; |
||
| 103 | //$texttoinsert.= ($val['notnull']?' NOT NULL':''); |
||
| 104 | //if ($i < count($object->fields)) $texttoinsert.=";"; |
||
| 105 | $texttoinsert.= "\n"; |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | $texttoinsert.= "\t".'// END MODULEBUILDER PROPERTIES'; |
||
| 110 | |||
| 111 | $contentclass = preg_replace('/\/\/ BEGIN MODULEBUILDER PROPERTIES.*END MODULEBUILDER PROPERTIES/ims', $texttoinsert, $contentclass); |
||
| 112 | |||
| 113 | //file_put_contents($pathoffiletoedittmp, $contentclass); |
||
| 114 | file_put_contents(dol_osencode($pathoffiletoedittarget), $contentclass); |
||
| 115 | @chmod($pathoffiletoedittarget, octdec($newmask)); |
||
| 116 | |||
| 117 | return 1; |
||
| 118 | } |
||
| 119 | catch(Exception $e) |
||
| 120 | { |
||
| 121 | print $e->getMessage(); |
||
| 122 | return -1; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 212 |
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: