Completed
Push — wip/steps-v9-bis ( 28f03f )
by Romain
05:32
created

PreviousLinkViewHelper::render()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 8.6417
c 0
b 0
f 0
cc 6
nc 12
nop 0
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 FormZ project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\Formz\ViewHelpers\Step;
15
16
use Romm\Formz\Middleware\Item\Step\Service\StepMiddlewareService;
17
use Romm\Formz\Service\ViewHelper\Form\FormViewHelperService;
18
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
19
use TYPO3\CMS\Extbase\Mvc\Web\Request;
20
use TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper;
21
22
class PreviousLinkViewHelper extends AbstractFormFieldViewHelper
23
{
24
    const PREVIOUS_LINK_PARAMETER = 'substepsPrevious';
25
26
    /**
27
     * @var string
28
     */
29
    protected $tagName = 'input';
30
31
    /**
32
     * @var FormViewHelperService
33
     */
34
    protected $formService;
35
36
    /**
37
     * @var ControllerContext
38
     */
39
    protected $controllerContext;
40
41
    public function initializeArguments()
42
    {
43
        parent::initializeArguments();
44
        $this->registerUniversalTagAttributes();
45
    }
46
47
    public function render()
48
    {
49
        if (!$this->controllerContext) {
50
            $this->controllerContext = $this->renderingContext->getControllerContext();
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: Nimut\TestingFramework\R...RenderingContextFixture, TYPO3\CMS\Fluid\Core\Rendering\RenderingContext, TYPO3\CMS\Fluid\Tests\Un...RenderingContextFixture.

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...
51
        }
52
53
        /*
54
         * First, we check if this view helper is called from within the
55
         * `FormViewHelper`, because it would not make sense anywhere else.
56
         */
57
        if (false === $this->formService->formContextExists()) {
58
            // @todo
59
//            throw ContextNotFoundException::substepViewHelperFormContextNotFound();
60
        }
61
62
        $formObject = $this->formService->getFormObject();
63
        $formDefinition = $formObject->getDefinition();
64
65
        if (false === $formDefinition->hasSteps()) {
66
            throw new \Exception('todo'); // @todo
67
        }
68
69
        /** @var Request $request */
70
        $request = $this->controllerContext->getRequest();
71
        $currentStep = $formObject->fetchCurrentStep($request)->getCurrentStep();
72
        $stepDefinition = StepMiddlewareService::get()->getStepDefinition($currentStep);
73
74
        // @todo handle previous steps depth
75
        if (!$stepDefinition->hasPreviousDefinition()
76
            && !$currentStep->hasSubsteps()
77
        ) {
78
            return null;
79
        }
80
81
        $this->tag->addAttribute('type', 'submit');
82
        $this->tag->addAttribute('value', $this->getValueAttribute());
83
        $this->tag->addAttribute('name', $this->prefixFieldName(PreviousLinkViewHelper::PREVIOUS_LINK_PARAMETER));
84
        $this->tag->addAttribute('fz-previous-link', true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
85
86
        return $this->tag->render();
87
    }
88
89
    /**
90
     * @param FormViewHelperService $service
91
     */
92
    public function injectFormService(FormViewHelperService $service)
93
    {
94
        $this->formService = $service;
95
    }
96
}
97