| Conditions | 32 |
| Paths | 1545 |
| Total Lines | 133 |
| 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 |
||
| 213 | function rebuildObjectSql($destdir, $module, $objectname, $newmask, $readdir = '', $object = null) |
||
| 214 | { |
||
| 215 | global $db, $langs; |
||
| 216 | |||
| 217 | $error = 0; |
||
| 218 | |||
| 219 | if (empty($objectname)) return -1; |
||
| 220 | if (empty($readdir)) $readdir=$destdir; |
||
| 221 | |||
| 222 | $pathoffiletoclasssrc=$readdir.'/class/'.strtolower($objectname).'.class.php'; |
||
| 223 | |||
| 224 | // Edit .sql file |
||
| 225 | $pathoffiletoeditsrc=$readdir.'/sql/llx_'.strtolower($module).'_'.strtolower($objectname).'.sql'; |
||
| 226 | $pathoffiletoedittarget=$destdir.'/sql/llx_'.strtolower($module).'_'.strtolower($objectname).'.sql'.($readdir != $destdir ? '.new' : ''); |
||
| 227 | if (! dol_is_file($pathoffiletoeditsrc)) |
||
| 228 | { |
||
| 229 | $langs->load("errors"); |
||
| 230 | setEventMessages($langs->trans("ErrorFileNotFound", $pathoffiletoeditsrc), null, 'errors'); |
||
| 231 | return -1; |
||
| 232 | } |
||
| 233 | |||
| 234 | // Load object from myobject.class.php |
||
| 235 | try |
||
| 236 | { |
||
| 237 | if (! is_object($object)) |
||
| 238 | { |
||
| 239 | include_once $pathoffiletoclasssrc; |
||
| 240 | if (class_exists($objectname)) $object=new $objectname($db); |
||
| 241 | else return -1; |
||
| 242 | } |
||
| 243 | } |
||
| 244 | catch(Exception $e) |
||
| 245 | { |
||
| 246 | print $e->getMessage(); |
||
| 247 | } |
||
| 248 | |||
| 249 | // Backup old file |
||
| 250 | dol_copy($pathoffiletoedittarget, $pathoffiletoedittarget.'.back', $newmask, 1); |
||
| 251 | |||
| 252 | $contentsql = file_get_contents(dol_osencode($pathoffiletoeditsrc), 'r'); |
||
| 253 | |||
| 254 | $i=0; |
||
| 255 | $texttoinsert = '-- BEGIN MODULEBUILDER FIELDS'."\n"; |
||
| 256 | if (count($object->fields)) |
||
| 257 | { |
||
| 258 | foreach($object->fields as $key => $val) |
||
| 259 | { |
||
| 260 | $i++; |
||
| 261 | |||
| 262 | $type = $val['type']; |
||
| 263 | $type = preg_replace('/:.*$/', '', $type); // For case type = 'integer:Societe:societe/class/societe.class.php' |
||
| 264 | |||
| 265 | if ($type == 'html') $type = 'text'; // html modulebuilder type is a text type in database |
||
| 266 | elseif ($type == 'price') $type = 'double'; // html modulebuilder type is a text type in database |
||
| 267 | elseif ($type == 'link' || $type == 'sellist') $type = 'integer'; |
||
| 268 | $texttoinsert.= "\t".$key." ".$type; |
||
| 269 | if ($key == 'rowid') $texttoinsert.= ' AUTO_INCREMENT PRIMARY KEY'; |
||
| 270 | if ($key == 'entity') $texttoinsert.= ' DEFAULT 1'; |
||
| 271 | else |
||
| 272 | { |
||
| 273 | if ($val['default'] != '') |
||
| 274 | { |
||
| 275 | if (preg_match('/^null$/i', $val['default'])) $texttoinsert.= " DEFAULT NULL"; |
||
| 276 | elseif (preg_match('/varchar/', $type)) $texttoinsert.= " DEFAULT '".$db->escape($val['default'])."'"; |
||
| 277 | else $texttoinsert.= (($val['default'] > 0)?' DEFAULT '.$val['default']:''); |
||
| 278 | } |
||
| 279 | } |
||
| 280 | $texttoinsert.= (($val['notnull'] > 0)?' NOT NULL':''); |
||
| 281 | if ($i < count($object->fields)) $texttoinsert.=", "; |
||
| 282 | $texttoinsert.= "\n"; |
||
| 283 | } |
||
| 284 | } |
||
| 285 | $texttoinsert.= "\t".'-- END MODULEBUILDER FIELDS'; |
||
| 286 | |||
| 287 | $contentsql = preg_replace('/-- BEGIN MODULEBUILDER FIELDS.*END MODULEBUILDER FIELDS/ims', $texttoinsert, $contentsql); |
||
| 288 | |||
| 289 | $result = file_put_contents($pathoffiletoedittarget, $contentsql); |
||
| 290 | if ($result) |
||
| 291 | { |
||
| 292 | @chmod($pathoffiletoedittarget, octdec($newmask)); |
||
|
1 ignored issue
–
show
|
|||
| 293 | } |
||
| 294 | else |
||
| 295 | { |
||
| 296 | $error++; |
||
| 297 | } |
||
| 298 | |||
| 299 | // Edit .key.sql file |
||
| 300 | $pathoffiletoeditsrc=$destdir.'/sql/llx_'.strtolower($module).'_'.strtolower($objectname).'.key.sql'; |
||
| 301 | $pathoffiletoedittarget=$destdir.'/sql/llx_'.strtolower($module).'_'.strtolower($objectname).'.key.sql'.($readdir != $destdir ? '.new' : ''); |
||
| 302 | |||
| 303 | $contentsql = file_get_contents(dol_osencode($pathoffiletoeditsrc), 'r'); |
||
| 304 | |||
| 305 | $i=0; |
||
| 306 | $texttoinsert = '-- BEGIN MODULEBUILDER INDEXES'."\n"; |
||
| 307 | if (count($object->fields)) |
||
| 308 | { |
||
| 309 | foreach($object->fields as $key => $val) |
||
| 310 | { |
||
| 311 | $i++; |
||
| 312 | if (! empty($val['index'])) |
||
| 313 | { |
||
| 314 | $texttoinsert.= "ALTER TABLE llx_".strtolower($module).'_'.strtolower($objectname)." ADD INDEX idx_".strtolower($module).'_'.strtolower($objectname)."_".$key." (".$key.");"; |
||
| 315 | $texttoinsert.= "\n"; |
||
| 316 | } |
||
| 317 | if (! empty($val['foreignkey'])) |
||
| 318 | { |
||
| 319 | $tmp=explode('.', $val['foreignkey']); |
||
| 320 | if (! empty($tmp[0]) && ! empty($tmp[1])) |
||
| 321 | { |
||
| 322 | $texttoinsert.= "ALTER TABLE llx_".strtolower($module).'_'.strtolower($objectname)." ADD CONSTRAINT llx_".strtolower($module).'_'.strtolower($objectname)."_".$key." FOREIGN KEY (".$key.") REFERENCES ".$tmp[0]."(".$tmp[1].");"; |
||
| 323 | $texttoinsert.= "\n"; |
||
| 324 | } |
||
| 325 | } |
||
| 326 | } |
||
| 327 | } |
||
| 328 | $texttoinsert.= '-- END MODULEBUILDER INDEXES'; |
||
| 329 | |||
| 330 | $contentsql = preg_replace('/-- BEGIN MODULEBUILDER INDEXES.*END MODULEBUILDER INDEXES/ims', $texttoinsert, $contentsql); |
||
| 331 | |||
| 332 | dol_mkdir(dirname($pathoffiletoedittarget)); |
||
| 333 | |||
| 334 | $result2 = file_put_contents($pathoffiletoedittarget, $contentsql); |
||
| 335 | if ($result) |
||
| 336 | { |
||
| 337 | @chmod($pathoffiletoedittarget, octdec($newmask)); |
||
| 338 | } |
||
| 339 | else |
||
| 340 | { |
||
| 341 | $error++; |
||
| 342 | } |
||
| 343 | |||
| 344 | return $error ? -1 : 1; |
||
| 345 | } |
||
| 346 |
If you suppress an error, we recommend checking for the error condition explicitly: