Conditions | 46 |
Paths | 1 |
Total Lines | 176 |
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 namespace Savannabits\JetstreamInertiaGenerator\Generators\Traits; |
||
79 | protected function getVisibleColumns($tableName, $modelVariableName): Collection |
||
80 | { |
||
81 | $relationships = $this->setBelongsToRelations(); |
||
82 | $columns = $this->readColumnsFromTable($tableName); |
||
83 | $hasSoftDelete = ($columns->filter(function($column) { |
||
84 | return $column['name'] == "deleted_at"; |
||
85 | })->count() > 0); |
||
86 | $filtered = $columns->filter(function($column) { |
||
87 | return !($column['name'] == "id" || $column['name'] == "created_at" || $column['name'] == "updated_at" || $column['name'] == "deleted_at" || $column['name'] == "remember_token"); |
||
88 | }); |
||
89 | return $filtered->map(function($column) use ($tableName, $hasSoftDelete, $modelVariableName,$filtered,$relationships){ |
||
90 | $serverStoreRules = collect([]); |
||
91 | $serverUpdateRules = collect([]); |
||
92 | $frontendRules = collect([]); |
||
93 | if ($column['required']) { |
||
94 | $serverStoreRules->push('\'required\''); |
||
95 | $serverUpdateRules->push('\'sometimes\''); |
||
96 | if($column['type'] != 'boolean' && $column['name'] != 'password') { |
||
97 | $frontendRules->push('required'); |
||
98 | } |
||
99 | } else { |
||
100 | $serverStoreRules->push('\'nullable\''); |
||
101 | $serverUpdateRules->push('\'nullable\''); |
||
102 | } |
||
103 | |||
104 | if ($column['name'] == 'email') { |
||
105 | $serverStoreRules->push('\'email\''); |
||
106 | $serverUpdateRules->push('\'email\''); |
||
107 | $frontendRules->push('email'); |
||
108 | } |
||
109 | |||
110 | if ($column['name'] == 'password') { |
||
111 | $serverStoreRules->push('\'confirmed\''); |
||
112 | $serverUpdateRules->push('\'confirmed\''); |
||
113 | $frontendRules->push('confirmed:password'); |
||
114 | |||
115 | $serverStoreRules->push('\'min:7\''); |
||
116 | $serverUpdateRules->push('\'min:7\''); |
||
117 | $frontendRules->push('min:7'); |
||
118 | |||
119 | $serverStoreRules->push('\'regex:/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9]).*$/\''); |
||
120 | $serverUpdateRules->push('\'regex:/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9]).*$/\''); |
||
121 | //TODO not working, need fixing |
||
122 | // $frontendRules->push(''regex:/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[!$#%]).*$/g''); |
||
123 | } |
||
124 | |||
125 | if($column['unique'] || $column['name'] == 'slug') { |
||
126 | if($column['type'] == 'json') { |
||
127 | if (count($column['unique_indexes']) == 1) { |
||
128 | $storeRule = 'Rule::unique(\''.$tableName.'\', \''.$column['name'].'->\'.$locale)'; |
||
129 | $updateRule = 'Rule::unique(\''.$tableName.'\', \''.$column['name'].'->\'.$locale)->ignore($this->'.$modelVariableName.'->getKey(), $this->'.$modelVariableName.'->getKeyName())'; |
||
130 | if($hasSoftDelete && $column['unique_deleted_at_condition']) { |
||
131 | $storeRule .= '->whereNull(\'deleted_at\')'; |
||
132 | $updateRule .= '->whereNull(\'deleted_at\')'; |
||
133 | } |
||
134 | $cols = $column['unique_indexes'][0]->getColumns(); |
||
135 | if (count($cols) > 1) { |
||
136 | $otherCols = collect($cols)->reject(function ($col) use ($column) { return $col ===$column['name'];}); |
||
137 | foreach ($otherCols as $otherCol) { |
||
138 | $storeRule .= '->where('.$otherCol.',$this->'.$otherCol.')'; |
||
139 | $updateRule .= '->where('.$otherCol.',$this->'.$otherCol.')'; |
||
140 | } |
||
141 | } |
||
142 | $serverStoreRules->push($storeRule); |
||
143 | $serverUpdateRules->push($updateRule); |
||
144 | } |
||
145 | } else { |
||
146 | if (count($column['unique_indexes']) == 1) { |
||
147 | $storeRule = 'Rule::unique(\''.$tableName.'\', \''.$column['name'].'\')'; |
||
148 | $updateRule = 'Rule::unique(\''.$tableName.'\', \''.$column['name'].'\')'; |
||
149 | if($hasSoftDelete && $column['unique_deleted_at_condition']) { |
||
150 | $storeRule .= '->whereNull(\'deleted_at\')'; |
||
151 | $updateRule .= '->whereNull(\'deleted_at\')'; |
||
152 | } |
||
153 | $cols = $column['unique_indexes']->first()->getColumns(); |
||
154 | if (count($cols) > 1) { |
||
155 | $otherCols = collect($cols)->reject(function ($col) use ($column) { return $col ===$column['name'];}); |
||
156 | foreach ($otherCols as $otherCol) { |
||
157 | if ($filtered->keyBy("name")->has($otherCol)) { |
||
158 | $otherRel = $otherCol; |
||
159 | $otherKey = ''; |
||
160 | } elseif ($relationships->keyBy("name")->has($otherCol)) { |
||
161 | $otherRel = $relationships->get($otherCol)['relationship_variable']; |
||
162 | $ownerKey = $relationships->get($otherCol)["owner_key"]; |
||
163 | $otherKey = $ownerKey; |
||
164 | } else { |
||
165 | $otherRel = null; |
||
166 | $otherKey = null; |
||
167 | } |
||
168 | if ($otherRel) { |
||
169 | if ($otherKey) { |
||
170 | $where = 'collect($this->'.$otherRel.')->get(\''.$otherKey.'\')'; |
||
171 | } else { |
||
172 | $where = $otherRel; |
||
173 | } |
||
174 | $storeRule .= '->where("'.$otherCol.'", $this->'.$where.')'; |
||
175 | $updateRule .= '->where("'.$otherCol.'",$this->'.$where.')'; |
||
176 | |||
177 | } |
||
178 | } |
||
179 | } |
||
180 | $updateRule .= '->ignore($this->'.$modelVariableName.'->getKey(), $this->'.$modelVariableName.'->getKeyName())'; |
||
181 | $serverStoreRules->push($storeRule); |
||
182 | $serverUpdateRules->push($updateRule); |
||
183 | } |
||
184 | } |
||
185 | } |
||
186 | |||
187 | switch ($column['type']) { |
||
188 | case 'datetime': |
||
189 | $serverStoreRules->push('\'date\''); |
||
190 | $serverUpdateRules->push('\'date\''); |
||
191 | $frontendRules->push('date_format:yyyy-MM-dd HH:mm:ss'); |
||
192 | break; |
||
193 | case 'date': |
||
194 | $serverStoreRules->push('\'date\''); |
||
195 | $serverUpdateRules->push('\'date\''); |
||
196 | $frontendRules->push('date_format:yyyy-MM-dd'); |
||
197 | break; |
||
198 | case 'time': |
||
199 | $serverStoreRules->push('\'date_format:H:i:s\''); |
||
200 | $serverUpdateRules->push('\'date_format:H:i:s\''); |
||
201 | $frontendRules->push('date_format:HH:mm:ss'); |
||
202 | break; |
||
203 | |||
204 | case 'tinyInteger': |
||
205 | case 'smallInteger': |
||
206 | case 'mediumInteger': |
||
207 | case 'bigInteger': |
||
208 | case 'bigint': |
||
209 | case 'unsignedInteger': |
||
210 | case 'unsignedTinyInteger': |
||
211 | case 'unsignedSmallInteger': |
||
212 | case 'unsignedMediumInteger': |
||
213 | case 'unsignedBigInteger': |
||
214 | case 'integer': |
||
215 | $serverStoreRules->push('\'integer\''); |
||
216 | $serverUpdateRules->push('\'integer\''); |
||
217 | $frontendRules->push('integer'); |
||
218 | break; |
||
219 | |||
220 | case 'boolean': |
||
221 | $serverStoreRules->push('\'boolean\''); |
||
222 | $serverUpdateRules->push('\'boolean\''); |
||
223 | $frontendRules->push(''); |
||
224 | break; |
||
225 | case 'float': |
||
226 | $serverStoreRules->push('\'numeric\''); |
||
227 | $serverUpdateRules->push('\'numeric\''); |
||
228 | $frontendRules->push('decimal'); |
||
229 | break; |
||
230 | case 'decimal': |
||
231 | $serverStoreRules->push('\'numeric\''); |
||
232 | $serverUpdateRules->push('\'numeric\''); |
||
233 | $frontendRules->push('decimal'); // FIXME?? I'm not sure about this one |
||
234 | break; |
||
235 | case 'text': |
||
236 | case 'string': |
||
237 | $serverStoreRules->push('\'string\''); |
||
238 | $serverUpdateRules->push('\'string\''); |
||
239 | break; |
||
240 | default: |
||
241 | $serverStoreRules->push('\'string\''); |
||
242 | $serverUpdateRules->push('\'string\''); |
||
243 | } |
||
244 | |||
245 | return [ |
||
246 | 'name' => $column['name'], |
||
247 | 'type' => $column['type'], |
||
248 | 'label' => Str::title(str_replace("-"," ",Str::slug($column["name"]))), |
||
249 | 'serverStoreRules' => $serverStoreRules->toArray(), |
||
250 | 'serverUpdateRules' => $serverUpdateRules->toArray(), |
||
251 | 'frontendRules' => $frontendRules->toArray(), |
||
252 | ]; |
||
253 | }); |
||
254 | } |
||
255 | |||
294 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.