| Conditions | 27 |
| Paths | 4120 |
| Total Lines | 92 |
| Code Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 97 | private function resolveImageSize(string $tag): array |
||
| 98 | { |
||
| 99 | $width = $height = null; |
||
| 100 | $crop = false; |
||
| 101 | |||
| 102 | // First, let's check the tag attributes for width and height |
||
| 103 | if (preg_match('#width=["|\']?([\d%]+)["|\']?#i', $tag, $matches)) { |
||
| 104 | $width = array_pop($matches); |
||
| 105 | } else { |
||
| 106 | unset($matches); |
||
| 107 | } |
||
| 108 | |||
| 109 | if (preg_match('#height=["|\']?([\d%]+)["|\']?#i', $tag, $matches)) { |
||
| 110 | $height = array_pop($matches); |
||
| 111 | } else { |
||
| 112 | unset($matches); |
||
| 113 | } |
||
| 114 | |||
| 115 | $isRelativeWidth = strpos($width, '%') !== false; |
||
| 116 | $isRelativeHeight = strpos($height, '%') !== false; |
||
| 117 | |||
| 118 | if ($isRelativeWidth && $isRelativeHeight) { |
||
| 119 | $width = $height = null; |
||
| 120 | } |
||
| 121 | |||
| 122 | // Second, let's check tag classes for a size |
||
| 123 | if (preg_match('#class=["|\']?[^"\']*size-([^"\'\s]+)[^"\']*["|\']?#i', $tag, $size)) { |
||
| 124 | $size = array_pop($size); |
||
| 125 | |||
| 126 | $isUnsetWidth = $width === null; |
||
| 127 | $isUnsetHeight = $height === null; |
||
| 128 | $isFullSize = $size === 'full'; |
||
| 129 | |||
| 130 | if ($isUnsetWidth && $isUnsetHeight && !$isFullSize) { |
||
| 131 | if (in_array($size, ['thumbnail', 'medium', 'medium_large', 'large'], true)) { |
||
| 132 | $width = (int)get_option($size . '_size_w'); |
||
| 133 | $height = (int)get_option($size . '_size_h'); |
||
| 134 | $crop = (bool)get_option($size . '_crop'); |
||
| 135 | } elseif (isset($GLOBALS['_wp_additional_image_sizes'][$size])) { |
||
| 136 | $width = $GLOBALS['_wp_additional_image_sizes'][$size]['width']; |
||
| 137 | $height = $GLOBALS['_wp_additional_image_sizes'][$size]['height']; |
||
| 138 | $crop = $GLOBALS['_wp_additional_image_sizes'][$size]['crop']; |
||
| 139 | } |
||
| 140 | } |
||
| 141 | } else { |
||
| 142 | unset($size); |
||
| 143 | } |
||
| 144 | |||
| 145 | // Third, let's check for the attachment |
||
| 146 | if (preg_match('#class=["|\']?[^"\']*wp-image-([\d]+)[^"\']*["|\']?#i', $tag, $attachmentId)) { |
||
| 147 | $attachmentId = (int)array_pop($attachmentId); |
||
| 148 | |||
| 149 | $attachment = get_post($attachmentId); |
||
| 150 | |||
| 151 | if ($attachment && !is_wp_error($attachment) && $attachment->post_type === 'attachment') { |
||
| 152 | [$attachmentUrl, $attachmentWidth, $attachmentHeight] = wp_get_attachment_image_src( |
||
| 153 | $attachmentId, |
||
| 154 | $size ?? 'full' |
||
| 155 | ); |
||
| 156 | |||
| 157 | if (is_processable_image_url($attachmentUrl)) { |
||
| 158 | $hasBiggerWidth = $width !== null && $width > $attachmentWidth; |
||
| 159 | $hasBiggerHeight = $height !== null && $height > $attachmentHeight; |
||
| 160 | |||
| 161 | if ($hasBiggerWidth || $hasBiggerHeight) { |
||
| 162 | $width = $width === null ? null : min($width, $attachmentWidth); |
||
| 163 | $height = $height === null ? null : min($height, $attachmentHeight); |
||
| 164 | } |
||
| 165 | |||
| 166 | $isUnsetWidth = $width === null; |
||
| 167 | $isUnsetHeight = $height === null; |
||
| 168 | |||
| 169 | if ($isUnsetWidth && $isUnsetHeight) { |
||
| 170 | $width = $attachmentWidth; |
||
| 171 | $height = $attachmentHeight; |
||
| 172 | $crop = false; |
||
| 173 | } elseif (isset($size)) { |
||
| 174 | if (in_array($size, ['thumbnail', 'medium', 'medium_large', 'large'], true)) { |
||
| 175 | $crop = (bool)get_option($size . '_crop'); |
||
| 176 | } elseif (isset($GLOBALS['_wp_additional_image_sizes'][$size])) { |
||
| 177 | $crop = $GLOBALS['_wp_additional_image_sizes'][$size]['crop']; |
||
| 178 | } |
||
| 179 | } |
||
| 180 | } |
||
| 181 | } else { |
||
| 182 | unset($attachmentId, $attachment); |
||
| 183 | } |
||
| 184 | } else { |
||
| 185 | unset($attachmentId); |
||
| 186 | } |
||
| 187 | |||
| 188 | return compact('width', 'height', 'crop'); |
||
| 189 | } |
||
| 277 |