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