| Conditions | 13 |
| Paths | 24 |
| Total Lines | 60 |
| Code Lines | 25 |
| 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 |
||
| 115 | function crop_file_url_alter(&$uri) { |
||
| 116 | // Process only files that are stored in "styles" directory. |
||
| 117 | if (strpos($uri, '/styles/') !== FALSE && preg_match('/\/styles\/(.*?)\/(.*?)\/(.+)/', $uri, $match)) { |
||
| 118 | // Match image style, schema, file subdirectory and file name. |
||
| 119 | // Get the image style ID. |
||
| 120 | $image_style = $match[1]; |
||
| 121 | // Get the file path without query parameter. |
||
| 122 | $parsed_uri = UrlHelper::parse($match[3]); |
||
| 123 | // Get the file URI using parsed schema and file path. |
||
| 124 | $file_uri = $match[2] . '://' . $parsed_uri['path']; |
||
| 125 | |||
| 126 | // Prevent double hashing, if there is a hash argument already, do not add |
||
| 127 | // it again. |
||
| 128 | if (!empty($parsed_uri['query']['h'])) { |
||
| 129 | return; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** @var \Drupal\image\Entity\ImageStyle $image_style */ |
||
| 133 | if (!$image_style = ImageStyle::load($image_style)) { |
||
| 134 | return; |
||
| 135 | } |
||
| 136 | |||
| 137 | $crop_type = NULL; |
||
| 138 | // Find whether matched image style uses "crop_type" effect. |
||
| 139 | /* @var \Drupal\image\ImageEffectInterface $effect */ |
||
| 140 | foreach ($image_style->getEffects() as $uuid => $effect) { |
||
| 141 | $data_effect = $image_style->getEffect($uuid)->getConfiguration()['data']; |
||
| 142 | if (isset($data_effect['crop_type'])) { |
||
| 143 | $crop_type = $data_effect['crop_type']; |
||
| 144 | break; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | // In case the image style uses "crop_type" effect, load the crop entity. |
||
| 149 | if ($crop_type && $crop = Crop::findCrop($file_uri, $crop_type)) { |
||
| 150 | // Found a crop for this image, append a hash of it to the URL, |
||
| 151 | // so that browsers reload the image and CDNs and proxies can be bypassed. |
||
| 152 | $shortened_hash = substr(md5(implode($crop->position()) . implode($crop->anchor())), 0, 8); |
||
| 153 | |||
| 154 | // If the URI has a schema and that is not http, https or data, convert |
||
| 155 | // the URI to the external URL. Otherwise the appended query argument |
||
| 156 | // will be encoded. |
||
| 157 | // @see file_create_url() |
||
| 158 | $scheme = \Drupal::service('file_system')->uriScheme($uri); |
||
| 159 | if ($scheme && !in_array($scheme, ['http', 'https', 'data'])) { |
||
| 160 | if ($wrapper = \Drupal::service('stream_wrapper_manager')->getViaUri($uri)) { |
||
| 161 | $uri = $wrapper->getExternalUrl(); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | // Append either with a ? or a & if there are existing query arguments. |
||
| 166 | if (strpos($uri, '?') === FALSE) { |
||
| 167 | $uri .= '?h=' . $shortened_hash; |
||
| 168 | } |
||
| 169 | else { |
||
| 170 | $uri .= '&h=' . $shortened_hash; |
||
| 171 | } |
||
| 172 | } |
||
| 173 | } |
||
| 174 | } |
||
| 175 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.