| Total Complexity | 40 |
| Total Lines | 332 |
| Duplicated Lines | 0 % |
| Changes | 10 | ||
| Bugs | 0 | Features | 0 |
Complex classes like mod_barcode_product_standard 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.
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 mod_barcode_product_standard, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class mod_barcode_product_standard extends ModeleNumRefBarCode |
||
| 35 | { |
||
| 36 | public $name = 'Standard'; // Model Name |
||
| 37 | |||
| 38 | public $code_modifiable; // Editable code |
||
| 39 | |||
| 40 | public $code_modifiable_invalide; // Modified code if it is invalid |
||
| 41 | |||
| 42 | public $code_modifiable_null; // Modified code if it is null |
||
| 43 | |||
| 44 | public $code_null; // Optional code |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Dolibarr version of the loaded document |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr' |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var int Automatic numbering |
||
| 54 | */ |
||
| 55 | public $code_auto; |
||
| 56 | |||
| 57 | public $searchcode; // Search string |
||
| 58 | |||
| 59 | public $numbitcounter; // Number of digits the counter |
||
| 60 | |||
| 61 | public $prefixIsRequired; // The prefix field of third party must be filled when using {pre} |
||
| 62 | |||
| 63 | |||
| 64 | /** |
||
| 65 | * Constructor |
||
| 66 | */ |
||
| 67 | public function __construct() |
||
| 75 | } |
||
| 76 | |||
| 77 | |||
| 78 | /** Return description of module |
||
| 79 | * |
||
| 80 | * @param Translate $langs Object langs |
||
| 81 | * @return string Description of module |
||
| 82 | */ |
||
| 83 | public function info($langs) |
||
| 84 | { |
||
| 85 | global $conf, $mc; |
||
| 86 | global $form; |
||
| 87 | |||
| 88 | $langs->load("products"); |
||
| 89 | |||
| 90 | $disabled = ((!empty($mc->sharings['referent']) && $mc->sharings['referent'] != $conf->entity) ? ' disabled' : ''); |
||
| 91 | |||
| 92 | $texte = $langs->trans('GenericNumRefModelDesc')."<br>\n"; |
||
| 93 | $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">'; |
||
| 94 | $texte .= '<input type="hidden" name="token" value="'.newToken().'">'; |
||
| 95 | $texte .= '<input type="hidden" name="action" value="setModuleOptions">'; |
||
| 96 | $texte .= '<input type="hidden" name="param1" value="BARCODE_STANDARD_PRODUCT_MASK">'; |
||
| 97 | $texte .= '<table class="nobordernopadding" width="100%">'; |
||
| 98 | |||
| 99 | $tooltip = $langs->trans("GenericMaskCodes", $langs->transnoentities("BarCode"), $langs->transnoentities("BarCode")); |
||
| 100 | $tooltip .= $langs->trans("GenericMaskCodes3"); |
||
| 101 | $tooltip .= '<strong>'.$langs->trans("Example").':</strong><br>'; |
||
| 102 | $tooltip .= '020{000000000}? (for internal use)<br>'; |
||
| 103 | $tooltip .= '9771234{00000}? (example of ISSN code with prefix 1234)<br>'; |
||
| 104 | $tooltip .= '9791234{00000}? (example of ISMN code with prefix 1234)<br>'; |
||
| 105 | //$tooltip.=$langs->trans("GenericMaskCodes5"); |
||
| 106 | |||
| 107 | // Mask parameter |
||
| 108 | //$texte.= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("BarCodeModel").'):</td>'; |
||
| 109 | $texte .= '<tr><td>'.$langs->trans("Mask").':</td>'; |
||
| 110 | $texte .= '<td class="right">'.$form->textwithpicto('<input type="text" class="flat" size="24" name="value1" value="'.(!empty($conf->global->BARCODE_STANDARD_PRODUCT_MASK) ? $conf->global->BARCODE_STANDARD_PRODUCT_MASK : '').'"'.$disabled.'>', $tooltip, 1, 1).'</td>'; |
||
| 111 | $texte .= '<td class="left" rowspan="2"> <input type="submit" class="button" value="'.$langs->trans("Modify").'" name="Button"'.$disabled.'></td>'; |
||
| 112 | $texte .= '</tr>'; |
||
| 113 | |||
| 114 | $texte .= '</table>'; |
||
| 115 | $texte .= '</form>'; |
||
| 116 | |||
| 117 | return $texte; |
||
| 118 | } |
||
| 119 | |||
| 120 | |||
| 121 | /** |
||
| 122 | * Return an example of result returned by getNextValue |
||
| 123 | * |
||
| 124 | * @param Translate $langs Object langs |
||
| 125 | * @param Product $objproduct Object product |
||
| 126 | * @return string Return string example |
||
| 127 | */ |
||
| 128 | public function getExample($langs, $objproduct = 0) |
||
| 142 | } |
||
| 143 | /** |
||
| 144 | * Return literal barcode type code from numerical rowid type of barcode |
||
| 145 | * |
||
| 146 | * @param Database $db Database |
||
| 147 | * @param int $type Type of barcode (EAN, ISBN, ...) as rowid |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | public function literalBarcodeType($db, $type = '') |
||
| 151 | { |
||
| 152 | global $conf; |
||
| 153 | $out = ''; |
||
| 154 | |||
| 155 | $sql = "SELECT rowid, code, libelle as label"; |
||
| 156 | $sql .= " FROM ".MAIN_DB_PREFIX."c_barcode_type"; |
||
| 157 | $sql .= " WHERE rowid = '".$db->escape($type)."'"; |
||
| 158 | $sql .= " AND entity = ".((int) $conf->entity); |
||
| 159 | $result = $db->query($sql); |
||
| 160 | if ($result) { |
||
| 161 | $num = $db->num_rows($result); |
||
| 162 | |||
| 163 | if ($num > 0) { |
||
| 164 | $obj = $db->fetch_object($result); |
||
| 165 | $out .= $obj->label; //take the label corresponding to the type rowid in the database |
||
| 166 | } |
||
| 167 | } |
||
| 168 | else { |
||
| 169 | dol_print_error($db); |
||
| 170 | } |
||
| 171 | |||
| 172 | return $out; |
||
| 173 | } |
||
| 174 | /** |
||
| 175 | * Return next value |
||
| 176 | * |
||
| 177 | * @param Product $objproduct Object product |
||
| 178 | * @param string $type Type of barcode (EAN, ISBN, ...) |
||
| 179 | * @return string Value if OK, '' if module not configured, <0 if KO |
||
| 180 | */ |
||
| 181 | public function getNextValue($objproduct = null, $type = '') |
||
| 232 | } |
||
| 233 | |||
| 234 | |||
| 235 | /** |
||
| 236 | * Check validity of code according to its rules |
||
| 237 | * |
||
| 238 | * @param DoliDB $db Database handler |
||
| 239 | * @param string $code Code to check/correct |
||
| 240 | * @param Product $product Object product |
||
| 241 | * @param int $thirdparty_type 0 = customer/prospect , 1 = supplier |
||
| 242 | * @param string $type type of barcode (EAN, ISBN, ...) |
||
| 243 | * @return int 0 if OK |
||
| 244 | * -1 ErrorBadCustomerCodeSyntax |
||
| 245 | * -2 ErrorCustomerCodeRequired |
||
| 246 | * -3 ErrorCustomerCodeAlreadyUsed |
||
| 247 | * -4 ErrorPrefixRequired |
||
| 248 | */ |
||
| 249 | public function verif($db, &$code, $product, $thirdparty_type, $type) |
||
| 288 | } |
||
| 289 | |||
| 290 | |||
| 291 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 292 | /** |
||
| 293 | * Return if a code is used (by other element) |
||
| 294 | * |
||
| 295 | * @param DoliDB $db Handler acces base |
||
| 296 | * @param string $code Code to check |
||
| 297 | * @param Product $product Objet product |
||
| 298 | * @return int 0 if available, <0 if KO |
||
| 299 | */ |
||
| 300 | public function verif_dispo($db, $code, $product) |
||
| 318 | } |
||
| 319 | } |
||
| 320 | |||
| 321 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 322 | /** |
||
| 323 | * Return if a barcode value match syntax |
||
| 324 | * |
||
| 325 | * @param string $codefortest Code to check syntax |
||
| 326 | * @param string $typefortest Type of barcode (ISBN, EAN, ...) |
||
| 327 | * @return int 0 if OK, <0 if KO |
||
| 328 | */ |
||
| 329 | public function verif_syntax($codefortest, $typefortest) |
||
| 368 |