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