Total Complexity | 42 |
Total Lines | 242 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Pulling 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 Pulling, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Pulling extends BaseManager |
||
9 | { |
||
10 | /** |
||
11 | * @return void|mixed |
||
12 | * |
||
13 | * @throws FileNotFoundException |
||
14 | */ |
||
15 | public function get() |
||
44 | |||
45 | /**if(substr($dbtable,-1)=='s'){ |
||
46 | app()->command('model create','model:'.strtolower(substr($dbtable,0,-1))); |
||
47 | } |
||
48 | else{ |
||
49 | app()->command('model create','model:'.strtolower($dbtable)); |
||
50 | }**/ |
||
51 | |||
52 | //} |
||
53 | } |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * get table informations |
||
58 | * |
||
59 | * @param $table |
||
60 | * @return string |
||
61 | */ |
||
62 | private function tableInformations($table) |
||
63 | { |
||
64 | $foreignKeys = $this->schema->getConnection()->getForeignKeys($table); |
||
65 | |||
66 | $columns = $this->schema->getConnection()->showColumnsFrom($table); |
||
67 | |||
68 | $status = $this->schema->getConnection()->getTableStatus($table); |
||
69 | |||
70 | $indexes = $this->schema->getConnection()->showIndexes($table); |
||
71 | $multipleIndexes = $this->getMultipleIndex($indexes); |
||
72 | |||
73 | |||
74 | $list = []; |
||
75 | |||
76 | foreach ($columns as $key=>$data){ |
||
77 | |||
78 | $data['Type'] = rtrim(str_replace('unsigned','',$data['Type'])); |
||
79 | |||
80 | $field = $data['Field']; |
||
81 | $list[] = '$wizard->name(\''.$field.'\')'; |
||
82 | $list[] = '->'.$this->getColumnTransformers($data['Type']).''; |
||
83 | |||
84 | //default block |
||
85 | if(!is_null($data['Default'])){ |
||
86 | $default = $data['Default']; |
||
87 | $list[] = '->default(\''.$default.'\')'; |
||
88 | } |
||
89 | |||
90 | $getIndex = $this->getIndexInformation($indexes,$data['Field']); |
||
91 | |||
92 | //unique block |
||
93 | if($getIndex['Non_unique']=='0' && $getIndex['Key_name']!=='PRIMARY'){ |
||
94 | $indexName = $getIndex['Key_name']; |
||
95 | $list[] = '->unique(\''.$indexName.'\')'; |
||
96 | } |
||
97 | |||
98 | //index block |
||
99 | if($getIndex['Non_unique']=='1' && $getIndex['Key_name']!=='PRIMARY'){ |
||
100 | $columnName = $getIndex['Column_name']; |
||
|
|||
101 | $indexName = $getIndex['Key_name']; |
||
102 | |||
103 | if(count($multipleIndexes[$indexName])==1){ |
||
104 | $list[] = '->index(\''.$indexName.'\')'; |
||
105 | } |
||
106 | } |
||
107 | |||
108 | //comment block |
||
109 | if(strlen($data['Comment'])>0){ |
||
110 | $comment = $data['Comment']; |
||
111 | $list[] = '->comment(\''.$comment.'\')'; |
||
112 | } |
||
113 | |||
114 | //auto increment block |
||
115 | if($data['Extra']=='auto_increment'){ |
||
116 | $list[] = '->auto_increment()'; |
||
117 | } |
||
118 | |||
119 | $list[] = '; |
||
120 | '; |
||
121 | } |
||
122 | |||
123 | //table collation |
||
124 | $charset = $this->schema->getConnection()->getCharsetForCollation(''.$status['Collation'].''); |
||
125 | $list[] = '$wizard->table()->collation(\''.$charset.'\'); |
||
126 | '; |
||
127 | |||
128 | //table indexes |
||
129 | foreach ($multipleIndexes as $indexName=>$values) { |
||
130 | if(count($values)>1){ |
||
131 | $values = '\''.implode('\',\'',$values).'\''; |
||
132 | $list[] = ' $wizard->table()->indexes(\''.$indexName.'\',['.$values.']); |
||
133 | '; |
||
134 | } |
||
135 | } |
||
136 | |||
137 | if(count($foreignKeys)){ |
||
138 | |||
139 | $constraintName = $foreignKeys['CONSTRAINT_NAME']; |
||
140 | $key = $foreignKeys['COLUMN_NAME']; |
||
141 | $referenceTable = $foreignKeys['REFERENCED_TABLE_NAME']; |
||
142 | $referenceColumn = $foreignKeys['REFERENCED_COLUMN_NAME']; |
||
143 | |||
144 | if($foreignKeys['UPDATE_RULE']=='RESTRICT' && $foreignKeys['DELETE_RULE']=='RESTRICT'){ |
||
145 | $list[] = ' $wizard->table()->foreign()->constraint(\''.$constraintName.'\')->key(\''.$key.'\')->references(\''.$referenceTable.'\',\''.$referenceColumn.'\'); |
||
146 | '; |
||
147 | } |
||
148 | |||
149 | if($foreignKeys['UPDATE_RULE']!=='RESTRICT' && $foreignKeys['DELETE_RULE']=='RESTRICT'){ |
||
150 | $rule = $foreignKeys['UPDATE_RULE']; |
||
151 | $list[] = ' $wizard->table()->foreign()->constraint(\''.$constraintName.'\')->key(\''.$key.'\')->references(\''.$referenceTable.'\',\''.$referenceColumn.'\')->onUpdate()->'.strtolower($rule).'(); |
||
152 | '; |
||
153 | } |
||
154 | |||
155 | if($foreignKeys['UPDATE_RULE']=='RESTRICT' && $foreignKeys['DELETE_RULE']!=='RESTRICT'){ |
||
156 | $rule = $foreignKeys['DELETE_RULE']; |
||
157 | $list[] = ' $wizard->table()->foreign()->constraint(\''.$constraintName.'\')->key(\''.$key.'\')->references(\''.$referenceTable.'\',\''.$referenceColumn.'\')->onDelete()->'.strtolower($rule).'(); |
||
158 | '; |
||
159 | } |
||
160 | } |
||
161 | |||
162 | return implode('',$list); |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * get column transformers |
||
167 | * |
||
168 | * @param $column |
||
169 | * @return string |
||
170 | */ |
||
171 | private function getColumnTransformers($column) |
||
172 | { |
||
173 | if($column=='datetime'){ |
||
174 | return 'datetime()'; |
||
175 | } |
||
176 | elseif($column=='longtext'){ |
||
177 | return 'longtext()'; |
||
178 | } |
||
179 | elseif($column=='date'){ |
||
180 | return 'date()'; |
||
181 | } |
||
182 | elseif($column=='text'){ |
||
183 | return 'text()'; |
||
184 | } |
||
185 | elseif($column=='timestamp'){ |
||
186 | return 'timestamp'; |
||
187 | } |
||
188 | elseif($column=='mediumint'){ |
||
189 | return 'mediumint()'; |
||
190 | } |
||
191 | elseif($column=='tinyint'){ |
||
192 | return 'tinyint()'; |
||
193 | } |
||
194 | elseif($column=='float'){ |
||
195 | return 'float()'; |
||
196 | } |
||
197 | elseif($column=='mediumtext'){ |
||
198 | return 'mediumtext()'; |
||
199 | } |
||
200 | elseif($column=='mediumblob'){ |
||
201 | return 'mediumblob()'; |
||
202 | } |
||
203 | elseif($column=='blob'){ |
||
204 | return 'blob()'; |
||
205 | } |
||
206 | elseif(preg_match('@enum.*\((.*?)\)@',$column,$enum)){ |
||
207 | return 'enum(['.$enum[1].'])'; |
||
208 | } |
||
209 | else{ |
||
210 | return $column; |
||
211 | } |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * get index information |
||
216 | * |
||
217 | * @param $index |
||
218 | * @param $field |
||
219 | * @return void|mixed |
||
220 | */ |
||
221 | private function getIndexInformation($index,$field) |
||
222 | { |
||
223 | foreach ($index as $key=>$item) { |
||
224 | |||
225 | if($item['Column_name'] == $field){ |
||
226 | return $index[$key]; |
||
227 | } |
||
228 | } |
||
229 | |||
230 | return null; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * get index information |
||
235 | * |
||
236 | * @param $index |
||
237 | * @param $field |
||
238 | * @return void|mixed |
||
239 | */ |
||
240 | private function getMultipleIndex($index) |
||
250 | } |
||
251 | } |