| Conditions | 25 |
| Paths | 669 |
| Total Lines | 130 |
| Code Lines | 81 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 29 | public function run($request) |
||
| 30 | { |
||
| 31 | $this->request = $request; |
||
| 32 | |||
| 33 | $list = $this->getValidDataObjects(); |
||
| 34 | $this->addOption("model", "Which model to generate", null, $list); |
||
| 35 | $this->addOption("how_many", "How many records to generate", 20); |
||
| 36 | $this->addOption("member_from_api", "Use https://randomuser.me to generate members", true); |
||
| 37 | $this->addOption("clear_existing", "Clear existing data", false); |
||
| 38 | |||
| 39 | $options = $this->askOptions(); |
||
| 40 | |||
| 41 | $model = $options['model']; |
||
| 42 | $how_many = $options['how_many']; |
||
| 43 | $member_from_api = $options['member_from_api']; |
||
| 44 | $clear_existing = $options['clear_existing']; |
||
| 45 | |||
| 46 | if ($model) { |
||
| 47 | $sing = singleton($model); |
||
| 48 | |||
| 49 | if ($clear_existing) { |
||
| 50 | $this->message("Clearing existing data", "warning"); |
||
| 51 | $cleared = 0; |
||
| 52 | foreach ($model::get() as $rec) { |
||
| 53 | $rec->delete(); |
||
| 54 | $cleared++; |
||
| 55 | } |
||
| 56 | $this->message("Cleared $cleared records"); |
||
| 57 | } |
||
| 58 | |||
| 59 | if ($model == Member::class && $member_from_api) { |
||
| 60 | $this->createMembersFromApi($how_many); |
||
| 61 | } else { |
||
| 62 | for ($i = 0; $i < $how_many; $i++) { |
||
| 63 | $this->message("Generating record $i"); |
||
| 64 | |||
| 65 | try { |
||
| 66 | $rec = $model::create(); |
||
| 67 | |||
| 68 | $fields = $rec->getCMSFields(); |
||
| 69 | |||
| 70 | // Fill according to type |
||
| 71 | $db = $model::config()->db; |
||
| 72 | $owns = $model::config()->owns; |
||
| 73 | $has_one = $model::config()->has_one; |
||
| 74 | $has_many = $model::config()->has_many; |
||
| 75 | $many_many = $model::config()->many_many; |
||
| 76 | |||
| 77 | foreach ($db as $name => $type) { |
||
| 78 | $rec->$name = $this->getRandomValueFromType($type, $name, $rec); |
||
| 79 | |||
| 80 | $field = $fields->dataFieldByName($name); |
||
| 81 | if (!$field) { |
||
| 82 | continue; |
||
| 83 | } |
||
| 84 | |||
| 85 | // For dropdown fields and the likes, we might use the getSource thing |
||
| 86 | if ($field->hasMethod('getSource')) { |
||
| 87 | $source = $field->getSource(); |
||
| 88 | if (is_array($source)) { |
||
| 89 | $source = array_keys($source); |
||
| 90 | $value = $source[array_rand($source)]; |
||
| 91 | |||
| 92 | // Use save into to ensure consistency |
||
| 93 | $field->setValue($value); |
||
| 94 | $field->saveInto($rec); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | $hasFillFake = $rec->hasMethod('fillFake'); |
||
| 100 | |||
| 101 | // Only populate relations for record without fillFake |
||
| 102 | if (!$hasFillFake) { |
||
| 103 | foreach ($has_one as $name => $class) { |
||
| 104 | $rel = null; |
||
| 105 | $nameID = $name . 'ID'; |
||
| 106 | $isOwned = isset($owns[$name]) ? true : false; |
||
| 107 | |||
| 108 | if ($isOwned) { |
||
| 109 | if ($class == Image::class) { |
||
| 110 | $rel = FakeDataProvider::ownImage($rec, $name); |
||
| 111 | } |
||
| 112 | } else { |
||
| 113 | if ($class == Image::class) { |
||
| 114 | $rel = FakeDataProvider::image(); |
||
| 115 | } else { |
||
| 116 | $rel = FakeDataProvider::record($class); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | if ($rel) { |
||
| 121 | $rec->$nameID = $rel->ID; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | foreach ($many_many as $name => $class) { |
||
| 125 | if (is_array($class)) { |
||
| 126 | continue; |
||
| 127 | } |
||
| 128 | |||
| 129 | $rel = null; |
||
| 130 | if ($isOwned) { |
||
| 131 | if ($class == Image::class) { |
||
| 132 | $rel = FakeDataProvider::ownImage($rec); |
||
| 133 | } |
||
| 134 | } else { |
||
| 135 | $rel = FakeDataProvider::record($class); |
||
| 136 | } |
||
| 137 | if ($rel) { |
||
| 138 | $rec->$name()->add($rel->ID); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | $id = $rec->write(); |
||
| 144 | |||
| 145 | if ($hasFillFake) { |
||
| 146 | $rec->fillFake(); |
||
| 147 | } |
||
| 148 | |||
| 149 | $id = $rec->write(); |
||
| 150 | |||
| 151 | $this->message("New record with id $id", "created"); |
||
| 152 | } catch (Exception $ex) { |
||
| 153 | $this->message($ex->getMessage(), "error"); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | } |
||
| 157 | } else { |
||
| 158 | $this->message("Implement 'fillFake' method to create your own fakes"); |
||
| 159 | } |
||
| 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