Completed
Pull Request — master (#554)
by
unknown
02:15
created

AbstractActionViewHelper   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeArguments() 0 8 1
A renderExtbaseLink() 0 33 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace HDNET\Calendarize\ViewHelpers\Link;
6
7
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
8
9
class AbstractActionViewHelper extends AbstractLinkViewHelper
10
{
11
    protected $extensionName = 'Calendarize';
12
    protected $pluginName = 'Calendar';
13
    protected $controllerName = 'Calendar';
14
    protected $actionName;
15
16
    public function initializeArguments()
17
    {
18
        parent::initializeArguments();
19
20
        $this->registerArgument('section', 'string', 'The anchor to be added to the URI', false);
21
        $this->registerArgument('pageUid', 'int', 'Target page', false);
22
        $this->registerArgument('absolute', 'bool', 'If set, the URI of the rendered link is absolute', false);
23
    }
24
25
    /**
26
     * Render a link with action and controller.
27
     *
28
     * @param array $controllerArguments
29
     * @param null  $pageUid
30
     *
31
     * @return mixed|string
32
     */
33
    public function renderExtbaseLink(array $controllerArguments = [], $pageUid = null)
34
    {
35
        $absolute = $this->arguments['absolute'] ?? false;
36
        $pageUid = $pageUid ?? $this->getPageUid($this->arguments['pageUid'] ?? '');
37
38
        $section = $this->arguments['section'] ?? '';
39
40
        /** @var UriBuilder $uriBuilder */
41
        $uriBuilder = $this->renderingContext->getControllerContext()->getUriBuilder();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TYPO3Fluid\Fluid\Core\Re...nderingContextInterface as the method getControllerContext() does only exist in the following implementations of said interface: TYPO3\CMS\Fluid\Core\Rendering\RenderingContext.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
42
        // $uriBuilder = $this->renderingContext->getUriBuilder(); // Typo3 11 and later
43
        $this->lastHref = $uriBuilder->reset()
44
            ->setTargetPageUid($pageUid)
45
            ->setSection($section)
46
            ->setCreateAbsoluteUri($absolute)
47
            ->uriFor(
48
                $this->actionName,
49
                $controllerArguments,
50
                $this->controllerName,
51
                $this->extensionName,
52
                $this->pluginName
53
            );
54
55
        if ('' !== $this->lastHref) {
56
            $this->tag->addAttribute('href', $this->lastHref);
57
            $this->tag->setContent($this->renderChildren());
58
            $this->tag->forceClosingTag(true);
59
            $result = $this->tag->render();
60
        } else {
61
            $result = $this->renderChildren();
62
        }
63
64
        return $result;
65
    }
66
}
67