ImageViewHelper   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 64
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeArguments() 0 7 1
B render() 0 43 8
1
<?php
2
3
/**
4
 * Override the ViewHelper with ratio.
5
 */
6
7
namespace HDNET\Focuspoint\ViewHelpers;
8
9
use HDNET\Focuspoint\Service\FocusCropService;
10
use TYPO3\CMS\Core\Resource\FileInterface;
11
use TYPO3\CMS\Core\Utility\GeneralUtility;
12
13
/**
14
 * Override the ViewHelper with ratio.
15
 */
16
class ImageViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper
17
{
18
    /**
19
     * Initialize ViewHelper arguments.
20
     */
21
    public function initializeArguments()
22
    {
23
        parent::initializeArguments();
24
        $this->registerArgument('ratio', 'string', 'Ratio of the image', false, '1:1');
25
        $this->registerArgument('realCrop', 'boolean', 'Crop the image in real', false, true);
26
        $this->registerArgument('additionalClassDiv', 'string', 'Additional class for focus point div', false, '');
27
    }
28
29
    /**
30
     * Resize a given image (if required) and renders the respective img tag.
31
     *
32
     * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
33
     *
34
     * @return string Rendered tag
35
     */
36
    public function render()
37
    {
38
        /** @var FocusCropService $service */
39
        $service = GeneralUtility::makeInstance(FocusCropService::class);
40
        $internalImage = null;
41
        try {
42
            $internalImage = $service->getViewHelperImage($this->arguments['src'], $this->arguments['image'], $this->arguments['treatIdAsReference']);
43
            if ($this->arguments['realCrop'] && $internalImage instanceof FileInterface) {
44
                $this->arguments['src'] = $service->getCroppedImageSrcByFile($internalImage, $this->arguments['ratio']);
45
                $this->arguments['treatIdAsReference'] = false;
46
                $this->arguments['image'] = null;
47
            }
48
        } catch (\Exception $ex) {
49
            $this->arguments['realCrop'] = true;
50
        }
51
52
        try {
53
            parent::render();
54
        } catch (\Exception $ex) {
55
            return 'Missing image!';
56
        }
57
58
        if ($this->arguments['realCrop']) {
59
            return $this->tag->render();
60
        }
61
62
        // Ratio calculation
63
        if (null !== $internalImage) {
64
            $focusPointY = $internalImage->getProperty('focus_point_y');
0 ignored issues
show
Bug introduced by
The method getProperty does only exist in TYPO3\CMS\Core\Resource\FileInterface, but not in TYPO3\CMS\Core\Resource\Folder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
65
            $focusPointX = $internalImage->getProperty('focus_point_x');
66
67
            $additionalClassDiv = 'focuspoint';
68
            if (!empty($this->arguments['additionalClassDiv'])) {
69
                $additionalClassDiv .= ' ' . $this->arguments['additionalClassDiv'];
70
            }
71
72
            $focusTag = '<div class="' . $additionalClassDiv . '" data-image-imageSrc="' . $this->tag->getAttribute('src') . '" data-focus-x="' . ($focusPointX / 100) . '" data-focus-y="' . ($focusPointY / 100) . '" data-image-w="' . $this->tag->getAttribute('width') . '" data-image-h="' . $this->tag->getAttribute('height') . '">';
73
74
            return $focusTag . $this->tag->render() . '</div>';
75
        }
76
77
        return 'Missing internal image!';
78
    }
79
}
80