Conditions | 8 |
Paths | 10 |
Total Lines | 55 |
Code Lines | 36 |
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 |
||
96 | public function create($src) { |
||
97 | $image = new ResponsiveImage($src); |
||
98 | $src = $image->src(); |
||
99 | $sourceImage = $this->getImageFile($this->sourcePath, $src); |
||
100 | |||
101 | if (!$sourceImage) { |
||
102 | throw new FileNotFoundException("{$this->sourcePath}{$src}"); |
||
103 | } |
||
104 | |||
105 | $publicImagePath = "{$this->publicPath}/{$src}"; |
||
106 | |||
107 | if (!$this->enableCache || !$this->fs->exists($publicImagePath)) { |
||
108 | if ($this->fs->exists($publicImagePath)) { |
||
109 | $this->fs->remove($publicImagePath); |
||
110 | } |
||
111 | |||
112 | $this->fs->dumpFile($publicImagePath, $sourceImage->getContents()); |
||
113 | } |
||
114 | |||
115 | $extension = $sourceImage->getExtension(); |
||
116 | $name = str_replace(".{$extension}", '', $sourceImage->getFilename()); |
||
117 | |||
118 | $urlParts = explode('/', $src); |
||
119 | array_pop($urlParts); |
||
120 | $urlPath = implode('/', $urlParts); |
||
121 | |||
122 | $imageObject = $this->engine->make($sourceImage->getPathname()); |
||
123 | $width = $imageObject->getWidth(); |
||
124 | $height = $imageObject->getHeight(); |
||
125 | $image->addSource($src, $width); |
||
126 | |||
127 | $stepWidth = (int) ($width * $this->stepModifier); |
||
128 | $stepHeight = (int) ($height * $this->stepModifier); |
||
129 | $width -= $stepWidth; |
||
130 | $height -= $stepHeight; |
||
131 | |||
132 | while ($width >= $this->minSize) { |
||
133 | $scaledName = "{$name}-{$width}.{$extension}"; |
||
134 | $scaledSrc = "{$urlPath}/{$scaledName}"; |
||
135 | $image->addSource($scaledSrc, $width); |
||
136 | |||
137 | $publicScaledPath = "{$this->publicPath}/{$urlPath}/{$scaledName}"; |
||
138 | if (!$this->enableCache || !$this->fs->exists($publicScaledPath)) { |
||
139 | $this->fs->dumpFile( |
||
140 | $publicScaledPath, |
||
141 | $imageObject->resize($width, $height)->encode($extension) |
||
142 | ); |
||
143 | } |
||
144 | |||
145 | $width -= $stepWidth; |
||
146 | $height -= $stepHeight; |
||
147 | } |
||
148 | |||
149 | return $image; |
||
150 | } |
||
151 | |||
232 |