Conditions | 8 |
Paths | 24 |
Total Lines | 55 |
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 |
||
55 | public function save($image, $name = null) |
||
56 | { |
||
57 | if (is_string($image)) { |
||
58 | @[$type, $image] = explode(';', $image); |
||
59 | @[, $image] = explode(',', $image); |
||
60 | if (! $name) { |
||
61 | $name = md5(uniqid(mt_rand(), true)).'.'.\Illuminate\Http\Testing\MimeType::search($type); |
||
62 | } |
||
63 | } |
||
64 | |||
65 | if (! $name) { |
||
66 | $name = md5(uniqid(mt_rand(), true)).'.'.$image->getClientOriginalExtension(); |
||
67 | } |
||
68 | |||
69 | $imageData = new SavedImageData($name); |
||
70 | foreach ($this->imageSizes() as $image_size_class) { |
||
71 | $image_size = new $image_size_class(); |
||
72 | if (! ($image_size instanceof ImageSizeInterface)) { |
||
73 | throw new NotValidImageSizeException('image size except implement ImageSizeInterface'); |
||
74 | } |
||
75 | |||
76 | /** @var \Intervention\Image\Image $img */ |
||
77 | $img = Image::make($image); |
||
78 | |||
79 | $img->resize( |
||
80 | $image_size->getWidth(), |
||
81 | $image_size->getHeight(), |
||
82 | function (Constraint $constraint) use ($image_size) { |
||
83 | if (! $image_size->getForceCrop()) { |
||
84 | $constraint->aspectRatio(); |
||
85 | } |
||
86 | $constraint->upsize(); |
||
87 | } |
||
88 | ); |
||
89 | |||
90 | $is_saved = Storage::disk($image_size->getDisk())->put($this->getPathForImageSize($image_size, $name), (string) $img->encode()); |
||
91 | |||
92 | if ($is_saved) { |
||
93 | $imageData->addSize( |
||
94 | $image_size::getSlug(), |
||
95 | [ |
||
96 | 'size' => $img->width().'/'.$img->height(), |
||
97 | 'width' => $img->width(), |
||
98 | 'height' => $img->height(), |
||
99 | ] |
||
100 | ); |
||
101 | } else { |
||
102 | $imageData->addError( |
||
103 | $image_size::getSlug() |
||
104 | ); |
||
105 | } |
||
106 | } |
||
107 | |||
108 | return $imageData; |
||
109 | } |
||
110 | |||
212 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.