| Conditions | 17 |
| Paths | 137 |
| Total Lines | 89 |
| Code Lines | 51 |
| 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 |
||
| 140 | function rebuildObjectSql($destdir, $module, $objectname, $newmask, $readdir='') |
||
| 141 | { |
||
| 142 | global $db, $langs; |
||
| 143 | |||
| 144 | if (empty($objectname)) return -1; |
||
| 145 | if (empty($readdir)) $readdir=$destdir; |
||
| 146 | |||
| 147 | $pathoffiletoclasssrc=$readdir.'/class/'.strtolower($objectname).'.class.php'; |
||
| 148 | |||
| 149 | // Edit .sql file |
||
| 150 | $pathoffiletoeditsrc=$readdir.'/sql/llx_'.strtolower($objectname).'.sql'; |
||
| 151 | $pathoffiletoedittarget=$destdir.'/sql/llx_'.strtolower($objectname).'.sql'.($readdir != $destdir ? '.new' : ''); |
||
| 152 | if (! dol_is_file($pathoffiletoeditsrc)) |
||
| 153 | { |
||
| 154 | $langs->load("errors"); |
||
| 155 | setEventMessages($langs->trans("ErrorFileNotFound", $pathoffiletoeditsrc), null, 'errors'); |
||
| 156 | return -1; |
||
| 157 | } |
||
| 158 | |||
| 159 | try |
||
| 160 | { |
||
| 161 | include_once $pathoffiletoclasssrc; |
||
| 162 | if (class_exists($objectname)) $object=new $objectname($db); |
||
| 163 | else return -1; |
||
| 164 | } |
||
| 165 | catch(Exception $e) |
||
| 166 | { |
||
| 167 | print $e->getMessage(); |
||
| 168 | } |
||
| 169 | |||
| 170 | // Backup old file |
||
| 171 | dol_copy($pathoffiletoedittarget, $pathoffiletoedittarget.'.back', $newmask, 1); |
||
| 172 | |||
| 173 | $contentsql = file_get_contents(dol_osencode($pathoffiletoeditsrc), 'r'); |
||
| 174 | |||
| 175 | $i=0; |
||
| 176 | $texttoinsert = '-- BEGIN MODULEBUILDER FIELDS'."\n"; |
||
| 177 | if (count($object->fields)) |
||
| 178 | { |
||
| 179 | foreach($object->fields as $key => $val) |
||
| 180 | { |
||
| 181 | $i++; |
||
| 182 | $texttoinsert.= "\t".$key." ".$val['type']; |
||
| 183 | if ($key == 'rowid') $texttoinsert.= ' AUTO_INCREMENT PRIMARY KEY'; |
||
| 184 | if ($key == 'entity') $texttoinsert.= ' DEFAULT 1'; |
||
| 185 | $texttoinsert.= ($val['notnull']?' NOT NULL':''); |
||
| 186 | if ($i < count($object->fields)) $texttoinsert.=", "; |
||
|
|
|||
| 187 | $texttoinsert.= "\n"; |
||
| 188 | } |
||
| 189 | } |
||
| 190 | $texttoinsert.= "\t".'-- END MODULEBUILDER FIELDS'; |
||
| 191 | |||
| 192 | $contentsql = preg_replace('/-- BEGIN MODULEBUILDER FIELDS.*END MODULEBUILDER FIELDS/ims', $texttoinsert, $contentsql); |
||
| 193 | |||
| 194 | file_put_contents($pathoffiletoedittarget, $contentsql); |
||
| 195 | @chmod($pathoffiletoedittarget, octdec($newmask)); |
||
| 196 | |||
| 197 | |||
| 198 | // Edit .key.sql file |
||
| 199 | $pathoffiletoeditsrc=$destdir.'/sql/llx_'.strtolower($objectname).'.key.sql'; |
||
| 200 | $pathoffiletoedittarget=$destdir.'/sql/llx_'.strtolower($objectname).'.key.sql'.($readdir != $destdir ? '.new' : ''); |
||
| 201 | |||
| 202 | $contentsql = file_get_contents(dol_osencode($pathoffiletoeditsrc), 'r'); |
||
| 203 | |||
| 204 | $i=0; |
||
| 205 | $texttoinsert = '-- BEGIN MODULEBUILDER INDEXES'."\n"; |
||
| 206 | if (count($object->fields)) |
||
| 207 | { |
||
| 208 | foreach($object->fields as $key => $val) |
||
| 209 | { |
||
| 210 | $i++; |
||
| 211 | if ($val['index']) |
||
| 212 | { |
||
| 213 | $texttoinsert.= "ALTER TABLE llx_".strtolower($objectname)." ADD INDEX idx_".strtolower($objectname)."_".$key." (".$key.");"; |
||
| 214 | $texttoinsert.= "\n"; |
||
| 215 | } |
||
| 216 | } |
||
| 217 | } |
||
| 218 | $texttoinsert.= '-- END MODULEBUILDER INDEXES'; |
||
| 219 | |||
| 220 | $contentsql = preg_replace('/-- BEGIN MODULEBUILDER INDEXES.*END MODULEBUILDER INDEXES/ims', $texttoinsert, $contentsql); |
||
| 221 | |||
| 222 | dol_mkdir(dirname($pathoffiletoedittarget)); |
||
| 223 | |||
| 224 | file_put_contents($pathoffiletoedittarget, $contentsql); |
||
| 225 | @chmod($pathoffiletoedittarget, octdec($newmask)); |
||
| 226 | |||
| 227 | return 1; |
||
| 228 | } |
||
| 229 | |||
| 231 |
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: