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