Conditions | 12 |
Paths | 120 |
Total Lines | 60 |
Code Lines | 32 |
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 |
||
124 | private function getUrlBuilderForTransform(Asset $asset, $transform, array $params = []): UrlBuilder |
||
125 | { |
||
126 | $assetUri = $this->getAssetUri($asset); |
||
127 | $baseUrl = $params['baseUrl']; |
||
128 | $securityKey = $params['securityKey'] ?: null; |
||
129 | $builder = UrlBuilder::construct($baseUrl, $securityKey, $assetUri); |
||
130 | $settings = ImageOptimize::$plugin->getSettings(); |
||
131 | |||
132 | if ($transform->mode === 'fit') { |
||
133 | // https://thumbor.readthedocs.io/en/latest/usage.html#fit-in |
||
134 | $builder->fitIn($transform->width, $transform->height); |
||
135 | } elseif ($transform->mode === 'stretch') { |
||
136 | $builder |
||
137 | ->resize($transform->width, $transform->height) |
||
138 | ->addFilter('upscale'); |
||
139 | |||
140 | // https://github.com/thumbor/thumbor/issues/1123 |
||
141 | Craft::warning('Thumbor has no equivalent to the "stretch" transform mode. The resulting image will be resized and cropped, but not stretched.', __METHOD__); |
||
142 | } else { |
||
143 | |||
144 | // https://thumbor.readthedocs.io/en/latest/usage.html#image-size |
||
145 | $builder->resize($transform->width, $transform->height); |
||
146 | |||
147 | if ($focalPoint = $this->getFocalPoint($asset)) { |
||
148 | // https://thumbor.readthedocs.io/en/latest/focal.html |
||
149 | $builder->addFilter('focal', $focalPoint); |
||
150 | } elseif (preg_match('/(top|center|bottom)-(left|center|right)/', $transform->position, $matches)) { |
||
151 | $v = str_replace('center', 'middle', $matches[1]); |
||
152 | $h = $matches[2]; |
||
153 | |||
154 | // https://thumbor.readthedocs.io/en/latest/usage.html#horizontal-align |
||
155 | $builder->valign($v)->halign($h); |
||
156 | } |
||
157 | } |
||
158 | |||
159 | // https://thumbor.readthedocs.io/en/latest/format.html |
||
160 | if ($format = $this->getFormat($transform)) { |
||
161 | $builder->addFilter('format', $format); |
||
162 | } |
||
163 | |||
164 | // https://thumbor.readthedocs.io/en/latest/quality.html |
||
165 | if ($quality = $this->getQuality($transform)) { |
||
166 | $builder->addFilter('quality', $quality); |
||
167 | } |
||
168 | |||
169 | if (property_exists($transform, 'interlace')) { |
||
170 | Craft::warning('Thumbor enables progressive JPEGs on the server-level, not as a request option. See https://thumbor.readthedocs.io/en/latest/jpegtran.html', __METHOD__); |
||
171 | } |
||
172 | |||
173 | if ($settings->autoSharpenScaledImages) { |
||
174 | // See if the image has been scaled >= 50% |
||
175 | $widthScale = $asset->getWidth() / ($transform->width ?? $asset->getWidth()); |
||
176 | $heightScale = $asset->getHeight() / ($transform->height ?? $asset->getHeight()); |
||
177 | if (($widthScale >= 2.0) || ($heightScale >= 2.0)) { |
||
178 | // https://thumbor.readthedocs.io/en/latest/sharpen.html |
||
179 | $builder->addFilter('sharpen', .5, .5, 'true'); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | return $builder; |
||
184 | } |
||
251 |