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