Total Complexity | 60 |
Total Lines | 240 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like FakeRecordGeneratorTask 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 FakeRecordGeneratorTask, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class FakeRecordGeneratorTask extends BuildTask |
||
22 | { |
||
23 | use BuildTaskTools; |
||
24 | |||
25 | protected $title = "Fake Record Generator"; |
||
26 | protected $description = 'Generate fake records for a given class'; |
||
27 | private static $segment = 'FakeRecordGeneratorTask'; |
||
28 | |||
29 | public function run($request) |
||
159 | } |
||
160 | } |
||
161 | |||
162 | protected function getRandomValueFromType($type, $name, $record) |
||
163 | { |
||
164 | $type = explode('(', $type); |
||
165 | switch ($type[0]) { |
||
166 | case 'Varchar': |
||
167 | case DBVarchar::class: |
||
168 | $length = 50; |
||
169 | if (count($type) > 1) { |
||
170 | $length = (int) $type[1]; |
||
171 | } |
||
172 | if ($name == 'CountryCode' || $name == 'Nationality') { |
||
173 | return FakeDataProvider::countryCode(); |
||
174 | } elseif ($name == 'PostalCode' || $name == 'Postcode') { |
||
175 | $addr = FakeDataProvider::address(); |
||
176 | return $addr['Postcode']; |
||
177 | } elseif ($name == 'Locality' || $name == 'City') { |
||
178 | $addr = FakeDataProvider::address(); |
||
179 | return $addr['City']; |
||
180 | } elseif ($name == 'URLSegment' || $name == 'Slug') { |
||
181 | return null; // let autogeneration happen |
||
182 | } |
||
183 | return FakeDataProvider::words(3, 7); |
||
184 | case 'Date': |
||
185 | case 'DateTime': |
||
186 | case DBDate::class: |
||
187 | return FakeDataProvider::date(strtotime('-1 year'), strtotime('+1 year')); |
||
188 | case 'Boolean': |
||
189 | case DBBoolean::class: |
||
190 | return FakeDataProvider::boolean(); |
||
191 | case 'Enum': |
||
192 | case 'NiceEnum': |
||
193 | case DBEnum::class: |
||
194 | /* @var $enum Enum */ |
||
195 | $enum = $record->dbObject($name); |
||
196 | return FakeDataProvider::pick(array_values($enum->enumValues())); |
||
197 | case 'Int': |
||
198 | case DBInt::class: |
||
199 | return rand(1, 10); |
||
200 | case 'Currency': |
||
201 | case DBCurrency::class: |
||
202 | return FakeDataProvider::fprand(20, 100, 2); |
||
203 | case 'HTMLText': |
||
204 | case DBHTMLText::class: |
||
205 | return FakeDataProvider::paragraphs(3, 7); |
||
206 | case 'Text': |
||
207 | case DBText::class: |
||
208 | return FakeDataProvider::sentences(3, 7); |
||
209 | default: |
||
210 | $dbObject = $record->dbObject($name); |
||
211 | if ($dbObject && $dbObject->hasMethod('fillFake')) { |
||
212 | return $dbObject->fillFake(); |
||
213 | } |
||
214 | return null; |
||
215 | } |
||
216 | } |
||
217 | |||
218 | protected function createMembersFromApi($how_many) |
||
219 | { |
||
220 | $data = FakeDataProvider::randomUser(['result' => $how_many]); |
||
221 | foreach ($data as $res) { |
||
222 | try { |
||
223 | $rec = Member::create(); |
||
224 | $rec->Gender = $res['gender']; |
||
225 | $rec->FirstName = ucwords($res['name']['first']); |
||
226 | $rec->Surname = ucwords($res['name']['last']); |
||
227 | $rec->Salutation = ucwords($res['name']['title']); |
||
228 | $rec->Address = $res['location']['street']; |
||
229 | $rec->Locality = $res['location']['city']; |
||
230 | $rec->PostalCode = $res['location']['postcode']; |
||
231 | $rec->BirthDate = $res['dob']; |
||
232 | $rec->Created = $res['registered']; |
||
233 | $rec->Phone = $res['phone']; |
||
234 | $rec->Cell = $res['cell']; |
||
235 | $rec->Nationality = $res['nat']; |
||
236 | $rec->Email = $res['email']; |
||
237 | |||
238 | $image_data = file_get_contents($res['picture']['large']); |
||
239 | $image = FakeDataProvider::storeFakeImage($image_data, basename($res['picture']['large']), 'Avatars'); |
||
240 | $rec->AvatarID = $image->ID; |
||
241 | |||
242 | $id = $rec->write(); |
||
243 | |||
244 | $rec->changePassword($res['login']['password']); |
||
245 | |||
246 | if ($rec->hasMethod('fillFake')) { |
||
247 | $rec->fillFake(); |
||
248 | } |
||
249 | $id = $rec->write(); |
||
250 | |||
251 | $this->message("New record with id $id", "created"); |
||
252 | } catch (Exception $ex) { |
||
253 | $this->message($ex->getMessage(), "error"); |
||
254 | } |
||
255 | } |
||
256 | } |
||
257 | |||
258 | public function isEnabled() |
||
261 | } |
||
262 | } |
||
263 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths