| Conditions | 14 |
| Paths | 514 |
| Total Lines | 88 |
| Code Lines | 57 |
| 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 |
||
| 16 | static function generateFormConfig($table, $coloms) |
||
|
|
|||
| 17 | { |
||
| 18 | $formArrayString = []; |
||
| 19 | foreach ($coloms as $i => $colName) { |
||
| 20 | //$attribute = []; |
||
| 21 | $placeholder = ''; |
||
| 22 | $help = ''; |
||
| 23 | |||
| 24 | $label = self::getLabel($colName); |
||
| 25 | |||
| 26 | if (FieldDetector::isExceptional($colName)) { |
||
| 27 | continue; |
||
| 28 | } |
||
| 29 | |||
| 30 | $typeData = DbInspector::getFieldTypes($table, $colName); |
||
| 31 | |||
| 32 | $validation = []; |
||
| 33 | $validation[] = 'required'; |
||
| 34 | list($type, $rule, $options) = self::parseFieldType($typeData); |
||
| 35 | $validation[] = $rule; |
||
| 36 | |||
| 37 | if (FieldDetector::isForeignKey($colName)) { |
||
| 38 | list($type, $options) = self::handleForeignKey($colName); |
||
| 39 | } |
||
| 40 | |||
| 41 | if (substr($colName, 0, 3) == 'is_') { |
||
| 42 | $type = 'radio_dataenum'; |
||
| 43 | $label = ucwords(substr($colName, 3)); |
||
| 44 | $validation = ['required|integer']; |
||
| 45 | $options = [ |
||
| 46 | 'enum' => ['In '.$label, $label], |
||
| 47 | 'value' => [0, 1], |
||
| 48 | ]; |
||
| 49 | } |
||
| 50 | |||
| 51 | if (FieldDetector::isPassword($colName)) { |
||
| 52 | $type = 'password'; |
||
| 53 | $validation = ['min:3', 'max:32']; |
||
| 54 | $help = cbTrans("text_default_help_password"); |
||
| 55 | } |
||
| 56 | |||
| 57 | if (FieldDetector::isImage($colName)) { |
||
| 58 | $type = 'upload'; |
||
| 59 | $validation = ['required|image']; |
||
| 60 | $help = cbTrans('text_default_help_upload'); |
||
| 61 | } |
||
| 62 | |||
| 63 | if (FieldDetector::isGeographical($colName)) { |
||
| 64 | $type = 'hidden'; |
||
| 65 | } |
||
| 66 | |||
| 67 | if (FieldDetector::isPhone($colName)) { |
||
| 68 | $type = 'number'; |
||
| 69 | $validation = ['required', 'numeric']; |
||
| 70 | $placeholder = cbTrans('text_default_help_number'); |
||
| 71 | } |
||
| 72 | |||
| 73 | if (FieldDetector::isEmail($colName)) { |
||
| 74 | $type = 'email'; |
||
| 75 | $validation[] = 'email|unique:'.$table; |
||
| 76 | $placeholder = cbTrans('text_default_help_email'); |
||
| 77 | } |
||
| 78 | |||
| 79 | if ($type == 'text' && FieldDetector::isNameField($colName)) { |
||
| 80 | $validation = ['required', 'string', 'min:3', 'max:70']; |
||
| 81 | $placeholder = cbTrans('text_default_help_text'); |
||
| 82 | } |
||
| 83 | |||
| 84 | if ($type == 'text' && FieldDetector::isUrlField($colName)) { |
||
| 85 | $validation = ['required', 'url']; |
||
| 86 | $placeholder = cbTrans('text_default_help_url'); |
||
| 87 | } |
||
| 88 | |||
| 89 | $validation = implode('|', $validation); |
||
| 90 | |||
| 91 | $formArray = []; |
||
| 92 | $formArray['label'] = $label; |
||
| 93 | $formArray['name'] = $colName; |
||
| 94 | $formArray['type'] = $type; |
||
| 95 | $formArray['options'] = $options; |
||
| 96 | $formArray['required'] = true; |
||
| 97 | $formArray['validation'] = $validation; |
||
| 98 | $formArray['help'] = $help; |
||
| 99 | $formArray['placeholder'] = $placeholder; |
||
| 100 | $formArrayString[] = min_var_export($formArray, " "); |
||
| 101 | } |
||
| 102 | |||
| 103 | return $formArrayString; |
||
| 104 | } |
||
| 164 | } |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.