| Conditions | 12 |
| Paths | 120 |
| Total Lines | 64 |
| Code Lines | 30 |
| 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 |
||
| 101 | private static function getUrlBuilderForTransform(Asset $asset, $transform, array $params = []): UrlBuilder |
||
| 102 | { |
||
| 103 | $assetUri = self::getAssetUri($asset); |
||
| 104 | $baseUrl = $params['baseUrl']; |
||
| 105 | $securityKey = $params['securityKey'] ?: null; |
||
| 106 | $builder = UrlBuilder::construct($baseUrl, $securityKey, $assetUri); |
||
| 107 | $settings = ImageOptimize::$plugin->getSettings(); |
||
| 108 | |||
| 109 | if ($transform->mode === 'fit') { |
||
| 110 | |||
| 111 | // https://thumbor.readthedocs.io/en/latest/usage.html#fit-in |
||
| 112 | $builder->fitIn($transform->width, $transform->height); |
||
| 113 | } elseif ($transform->mode === 'stretch') { |
||
| 114 | |||
| 115 | // AFAIK, this isn't possible with Thumbor…throw exception? |
||
| 116 | // https://github.com/thumbor/thumbor/issues/1123 |
||
| 117 | $builder |
||
| 118 | ->resize($transform->width, $transform->height) |
||
| 119 | ->addFilter('upscale'); |
||
| 120 | } else { |
||
| 121 | |||
| 122 | // https://thumbor.readthedocs.io/en/latest/usage.html#image-size |
||
| 123 | $builder->resize($transform->width, $transform->height); |
||
| 124 | |||
| 125 | if ($focalPoint = self::getFocalPoint($asset)) { |
||
| 126 | |||
| 127 | // https://thumbor.readthedocs.io/en/latest/focal.html |
||
| 128 | $builder->addFilter('focal', $focalPoint); |
||
| 129 | } elseif (preg_match('/(top|center|bottom)-(left|center|right)/', $transform->position, $matches)) { |
||
| 130 | $v = str_replace('center', 'middle', $matches[1]); |
||
| 131 | $h = $matches[2]; |
||
| 132 | |||
| 133 | // https://thumbor.readthedocs.io/en/latest/usage.html#horizontal-align |
||
| 134 | $builder->valign($v)->halign($h); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | // https://thumbor.readthedocs.io/en/latest/format.html |
||
| 139 | if ($format = self::getFormat($transform)) { |
||
| 140 | $builder->addFilter('format', $format); |
||
| 141 | } |
||
| 142 | |||
| 143 | // https://thumbor.readthedocs.io/en/latest/quality.html |
||
| 144 | if ($quality = self::getQuality($transform)) { |
||
| 145 | $builder->addFilter('quality', $quality); |
||
| 146 | } |
||
| 147 | |||
| 148 | if (property_exists($transform, 'interlace')) { |
||
| 149 | // Progressive JPEGs are configured on the server level, |
||
| 150 | // not as an option: https://thumbor.readthedocs.io/en/latest/jpegtran.html |
||
| 151 | } |
||
| 152 | |||
| 153 | if ($settings->autoSharpenScaledImages) { |
||
| 154 | // See if the image has been scaled >= 50% |
||
| 155 | $widthScale = $asset->getWidth() / ($transform->width ?? $asset->getWidth()); |
||
| 156 | $heightScale = $asset->getHeight() / ($transform->height ?? $asset->getHeight()); |
||
| 157 | if (($widthScale >= 2.0) || ($heightScale >= 2.0)) { |
||
| 158 | |||
| 159 | // https://thumbor.readthedocs.io/en/latest/sharpen.html |
||
| 160 | $builder->addFilter('sharpen', .5, .5, 'true'); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | return $builder; |
||
| 165 | } |
||
| 222 |