| Conditions | 28 |
| Paths | 2114 |
| Total Lines | 80 |
| Code Lines | 54 |
| 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 |
||
| 136 | public function render() |
||
| 137 | { |
||
| 138 | $src = (string)$this->arguments['src']; |
||
| 139 | if (($src === '' && $this->arguments['image'] === null) || ($src !== '' && $this->arguments['image'] !== null)) { |
||
| 140 | throw new Exception('You must either specify a string src or a File object.', 1382284106); |
||
| 141 | } |
||
| 142 | |||
| 143 | if ((string)$this->arguments['fileExtension'] && !GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], (string)$this->arguments['fileExtension'])) { |
||
| 144 | throw new Exception('The extension ' . $this->arguments['fileExtension'] . ' is not specified in $GLOBALS[\'TYPO3_CONF_VARS\'][\'GFX\'][\'imagefile_ext\'] as a valid image file extension and can not be processed.', 1618989190); |
||
| 145 | } |
||
| 146 | |||
| 147 | // A URL was given as src, this is kept as is, and we can only scale |
||
| 148 | if ($src !== '' && preg_match('/^(https?:)?\/\//', $src)) { |
||
| 149 | $this->tag->addAttribute('src', $src); |
||
| 150 | if (isset($this->arguments['width'])) { |
||
| 151 | $this->tag->addAttribute('width', $this->arguments['width']); |
||
| 152 | } |
||
| 153 | if (isset($this->arguments['height'])) { |
||
| 154 | $this->tag->addAttribute('height', $this->arguments['height']); |
||
| 155 | } |
||
| 156 | } else { |
||
| 157 | try { |
||
| 158 | $imageService = $this->getImageService(); |
||
| 159 | $image = $imageService->getImage($src, $this->arguments['image'], (bool)$this->arguments['treatIdAsReference']); |
||
| 160 | $cropString = $this->arguments['crop']; |
||
| 161 | if ($cropString === null && $image->hasProperty('crop') && $image->getProperty('crop')) { |
||
| 162 | $cropString = $image->getProperty('crop'); |
||
| 163 | } |
||
| 164 | $cropVariantCollection = CropVariantCollection::create((string)$cropString); |
||
| 165 | $cropVariant = $this->arguments['cropVariant'] ?: 'default'; |
||
| 166 | $cropArea = $cropVariantCollection->getCropArea($cropVariant); |
||
| 167 | $processingInstructions = [ |
||
| 168 | 'width' => $this->arguments['width'], |
||
| 169 | 'height' => $this->arguments['height'], |
||
| 170 | 'minWidth' => $this->arguments['minWidth'], |
||
| 171 | 'minHeight' => $this->arguments['minHeight'], |
||
| 172 | 'maxWidth' => $this->arguments['maxWidth'], |
||
| 173 | 'maxHeight' => $this->arguments['maxHeight'], |
||
| 174 | 'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image), |
||
| 175 | ]; |
||
| 176 | if (!empty($this->arguments['fileExtension'] ?? '')) { |
||
| 177 | $processingInstructions['fileExtension'] = $this->arguments['fileExtension']; |
||
| 178 | } |
||
| 179 | $processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions); |
||
| 180 | $imageUri = $imageService->getImageUri($processedImage, $this->arguments['absolute']); |
||
| 181 | |||
| 182 | if (!$this->tag->hasAttribute('data-focus-area')) { |
||
| 183 | $focusArea = $cropVariantCollection->getFocusArea($cropVariant); |
||
| 184 | if (!$focusArea->isEmpty()) { |
||
| 185 | $this->tag->addAttribute('data-focus-area', $focusArea->makeAbsoluteBasedOnFile($image)); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | $this->tag->addAttribute('src', $imageUri); |
||
| 189 | $this->tag->addAttribute('width', $processedImage->getProperty('width')); |
||
| 190 | $this->tag->addAttribute('height', $processedImage->getProperty('height')); |
||
| 191 | |||
| 192 | // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty |
||
| 193 | if (empty($this->arguments['alt'])) { |
||
| 194 | $this->tag->addAttribute('alt', $image->hasProperty('alternative') ? $image->getProperty('alternative') : ''); |
||
| 195 | } |
||
| 196 | // Add title-attribute from property if not already set and the property is not an empty string |
||
| 197 | $title = (string)($image->hasProperty('title') ? $image->getProperty('title') : ''); |
||
| 198 | if (empty($this->arguments['title']) && $title !== '') { |
||
| 199 | $this->tag->addAttribute('title', $title); |
||
| 200 | } |
||
| 201 | } catch (ResourceDoesNotExistException $e) { |
||
| 202 | // thrown if file does not exist |
||
| 203 | throw new Exception($e->getMessage(), 1509741911, $e); |
||
| 204 | } catch (\UnexpectedValueException $e) { |
||
| 205 | // thrown if a file has been replaced with a folder |
||
| 206 | throw new Exception($e->getMessage(), 1509741912, $e); |
||
| 207 | } catch (\RuntimeException $e) { |
||
| 208 | // RuntimeException thrown if a file is outside of a storage |
||
| 209 | throw new Exception($e->getMessage(), 1509741913, $e); |
||
| 210 | } catch (\InvalidArgumentException $e) { |
||
| 211 | // thrown if file storage does not exist |
||
| 212 | throw new Exception($e->getMessage(), 1509741914, $e); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | return $this->tag->render(); |
||
| 216 | } |
||
| 223 |