| Conditions | 23 |
| Paths | 2308 |
| Total Lines | 94 |
| Code Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 39 | public function make() { |
||
| 40 | $name = trim(($this->name)?:ucwords(str_replace("_"," ",$this->table))); |
||
| 41 | |||
| 42 | $template = file_get_contents(__DIR__."/../templates/FooBarController.stub"); |
||
| 43 | |||
| 44 | //Replace table |
||
| 45 | $template = str_replace("{table}",'"'.$this->table.'"', $template); |
||
| 46 | |||
| 47 | //Replace permalink |
||
| 48 | $permalink = strtolower(Str::slug($name,"_")); |
||
| 49 | $template = str_replace("{permalink}", '"'.$permalink.'"', $template); |
||
| 50 | |||
| 51 | //Replace Page title |
||
| 52 | $template = str_replace("{page_title}", '"'.$name.'"', $template); |
||
| 53 | |||
| 54 | //Replace scaffolding |
||
| 55 | $scaffold = ""; |
||
| 56 | foreach($this->columns as $field) { |
||
| 57 | $label = $field['column_label']; |
||
| 58 | $column = $field['column_field']; |
||
| 59 | $help = isset($field['column_help'])?"->help(\"".$field['column_help']."\")":""; |
||
| 60 | $required = ($field['column_mandatory']=="on")?"":"->required(false)"; |
||
| 61 | $indexShow = ($field['column_browse']=="on")?"":"->showIndex(false)"; |
||
| 62 | $detailShow = ($field['column_detail']=="on")?"":"->showDetail(false)"; |
||
| 63 | $editShow = ($field['column_edit']=="on")?"":"->showEdit(false)"; |
||
| 64 | $addShow = ($field['column_add']=="on")?"":"->showAdd(false)"; |
||
| 65 | $optionTable = $field['column_option_table']; |
||
| 66 | $optionValue = $field['column_option_value']; |
||
| 67 | $optionDisplay = $field['column_option_display']; |
||
| 68 | $optionSqlCondition = $field['column_option_sql_condition']; |
||
| 69 | $optionSqlCondition = str_replace('"',"'", $optionSqlCondition); |
||
| 70 | $sqlRawQuery = $field['column_sql_query']; |
||
| 71 | $options = $field['column_options']; |
||
| 72 | |||
| 73 | $methodName = Str::studly($field['column_type']); |
||
| 74 | if($label && $column) { |
||
| 75 | if(in_array($field['column_type'],['radio','select_option','checkbox'])) { |
||
| 76 | if($options) { |
||
| 77 | $optResult = []; |
||
| 78 | foreach($options as $opt) { |
||
| 79 | $optResult[$opt['key']] = $opt['label']; |
||
| 80 | } |
||
| 81 | $scaffold .= '$this->add' . $methodName . '("' . $label . '","' . $column . '")->options('.min_var_export($optResult).')' . $required . $indexShow . $detailShow . $addShow . $editShow . $help . ';' . "\n\t\t"; |
||
| 82 | } |
||
| 83 | }elseif (in_array($field['column_type'],['radio_table','select_table'])) { |
||
| 84 | if ($optionTable && $optionValue && $optionDisplay) { |
||
| 85 | $scaffold .= '$this->add' . $methodName . '("' . $label . '","' . $column . '",["table"=>"' . $optionTable . '","value_option"=>"' . $optionValue . '","display_option"=>"' . $optionDisplay . '","sql_condition"=>"' . $optionSqlCondition . '"])' . $required . $indexShow . $detailShow . $addShow . $editShow . $help . ';' . "\n\t\t"; |
||
| 86 | } |
||
| 87 | }elseif ($field['column_type'] == "select_query") { |
||
| 88 | if($sqlRawQuery && Str::contains($sqlRawQuery,["as `key`","as `label`"])) { |
||
| 89 | $scaffold .= '$this->add' . $methodName . '("' . $label . '","' . $column . '","'.$sqlRawQuery.'")' . $required . $indexShow . $detailShow . $addShow . $editShow . $help . ';' . "\n\t\t"; |
||
| 90 | } |
||
| 91 | }else{ |
||
| 92 | $scaffold .= '$this->add'.$methodName.'("'.$label.'","'.$column.'")'.$required.$indexShow.$detailShow.$addShow.$editShow.$help.';'."\n\t\t"; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | } |
||
| 96 | $template = str_replace("{scaffolding}", $scaffold, $template); |
||
| 97 | |||
| 98 | $filename = $this->makeControllerName($name); |
||
| 99 | |||
| 100 | //Replace Controller Name |
||
| 101 | $template = str_replace("FooBarController", $filename, $template); |
||
| 102 | |||
| 103 | //Create a controller file |
||
| 104 | file_put_contents(app_path("Http/Controllers/".$filename.".php"), $template); |
||
| 105 | |||
| 106 | //Save to database |
||
| 107 | $module = []; |
||
| 108 | $module['name'] = $name; |
||
| 109 | $module['icon'] = $this->icon; |
||
| 110 | $module['table_name'] = $this->table; |
||
| 111 | $module['controller'] = $filename; |
||
| 112 | $module['last_column_build'] = json_encode($this->columns); |
||
| 113 | |||
| 114 | if($moduleData = DB::table("cb_modules")->where("name", $name)->where("table_name",$this->table)->first()) { |
||
| 115 | DB::table("cb_modules")->where("id",$moduleData->id)->update($module); |
||
| 116 | $id_modules = $moduleData->id; |
||
| 117 | }else{ |
||
| 118 | $id_modules = DB::table('cb_modules')->insertGetId($module); |
||
| 119 | } |
||
| 120 | |||
| 121 | //Save menu |
||
| 122 | $menu = []; |
||
| 123 | $menu['name'] = $module['name']; |
||
| 124 | $menu['type'] = 'module'; |
||
| 125 | $menu['cb_modules_id'] = $id_modules; |
||
| 126 | if(isset($moduleData)) { |
||
| 127 | DB::table("cb_menus")->where("cb_modules_id",$moduleData->id)->update($menu); |
||
| 128 | }else{ |
||
| 129 | DB::table('cb_menus')->insertGetId($menu); |
||
| 130 | } |
||
| 131 | |||
| 132 | return $module; |
||
| 133 | } |
||
| 135 | } |