| Conditions | 19 |
| Paths | 301 |
| Total Lines | 60 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 3 | 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 |
||
| 16 | public function parse() { |
||
| 17 | if (is_null($this->data)) { |
||
| 18 | return; |
||
| 19 | } |
||
| 20 | $value = $this->data; |
||
| 21 | if (!is_array($value)) { |
||
| 22 | $value = [$value]; |
||
| 23 | } |
||
| 24 | $ids = []; |
||
| 25 | $dir = pathinfo($this->object->walker->migtarionLog->source, PATHINFO_DIRNAME); |
||
| 26 | $this->model->image_file_id = 0; |
||
| 27 | foreach ($value as $key => $imagePath) { |
||
| 28 | if (!$imagePath || !file_exists($dir . '/' . $imagePath)) { |
||
| 29 | continue; |
||
| 30 | } |
||
| 31 | $notEq = true; |
||
| 32 | $md5Cur = md5_file($dir . '/' . $imagePath); |
||
| 33 | foreach ($this->model->images as $imageId => $image) { |
||
| 34 | if (!$image->file) { |
||
| 35 | $image->delete(); |
||
| 36 | continue; |
||
| 37 | } |
||
| 38 | $file = $image->file; |
||
| 39 | $md5File = ''; |
||
| 40 | if ($file->md5) { |
||
| 41 | $md5File = $file->md5; |
||
| 42 | } elseif (file_exists($file->getRealPath())) { |
||
| 43 | $md5File = $file->md5 = md5_file($file->getRealPath()); |
||
| 44 | $file->save(); |
||
| 45 | } |
||
| 46 | |||
| 47 | if ($file && file_exists($file->getRealPath()) && $md5Cur == $md5File) { |
||
| 48 | $notEq = false; |
||
| 49 | $ids[] = $imageId; |
||
| 50 | break; |
||
| 51 | } |
||
| 52 | } |
||
| 53 | if ($notEq) { |
||
| 54 | $file_id = \App::$primary->files->uploadFromUrl($dir . '/' . $imagePath, ['accept_group' => 'image', 'upload_code' => 'MigrationUpload']); |
||
| 55 | if ($file_id) { |
||
| 56 | $image = new \Ecommerce\Item\Image([ |
||
| 57 | 'item_id' => $this->model->pk(), |
||
| 58 | 'file_id' => $file_id |
||
| 59 | ]); |
||
| 60 | $image->save(); |
||
| 61 | $ids[] = $image->id; |
||
| 62 | } |
||
| 63 | } else { |
||
| 64 | $image->weight = $key; |
||
| 65 | } |
||
| 66 | if ($image && !$this->model->image_file_id) { |
||
| 67 | $this->model->image_file_id = $image->file_id; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | foreach ($this->model->images as $imageId => $image) { |
||
| 71 | if (!in_array($imageId, $ids)) { |
||
| 72 | $image->delete(); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 78 |