Completed
Push — development ( ef0625...f79737 )
by Torben
04:11
created

PrefillViewHelper::initializeArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
namespace DERHANSEN\SfEventMgt\ViewHelpers;
3
4
/*
5
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.txt file that was distributed with this source code.
9
 */
10
11
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
12
13
/**
14
 * Prefill ViewHelper
15
 *
16
 * @author Torben Hansen <[email protected]>
17
 */
18
class PrefillViewHelper extends AbstractViewHelper
19
{
20
    /**
21
     * Initialize arguments
22
     */
23
    public function initializeArguments()
24
    {
25
        parent::initializeArguments();
26
        $this->registerArgument('fieldname', 'string', 'FieldName', true);
27
        $this->registerArgument('prefillSettings', 'array', 'PrefillSettings', false, []);
28
    }
29
30
    /**
31
     * Returns a property from fe_user (if logged in and if the given field is
32
     * configured to be prefilled)
33
     *
34
     * @return string
35
     */
36 8
    public function render()
37
    {
38 8
        $fieldname = $this->arguments['fieldname'];
39 8
        $prefillSettings = $this->arguments['prefillSettings'];
40 1
        $piVars = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('tx_sfeventmgt_pievent');
41
        if (isset($piVars['registration'][$fieldname]) && $piVars['registration'][$fieldname] !== '') {
42 7
            return $piVars['registration'][$fieldname];
43 4
        }
44 7
        if (!isset($GLOBALS['TSFE']) || !$GLOBALS['TSFE']->fe_user->user || empty($prefillSettings) ||
45 4
            !array_key_exists($fieldname, $prefillSettings)
46
        ) {
47
            return '';
48 3
        }
49 3
        // If mapping errors occured for form, return value that has been submitted
50 1
        $originalRequest = $this->getRequest()->getOriginalRequest();
51 1
        if ($originalRequest) {
52
            $submittedValues = $originalRequest->getArguments();
53 2
            return $submittedValues['registration'][$fieldname];
54
        }
55
56
        return strval($GLOBALS['TSFE']->fe_user->user[$prefillSettings[$fieldname]]);
57
    }
58
59
    /**
60
     * Shortcut for retrieving the request from the controller context
61
     *
62
     * @return \TYPO3\CMS\Extbase\Mvc\Request
63
     */
64
    protected function getRequest()
65
    {
66
        return $this->renderingContext->getControllerContext()->getRequest();
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.

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...
67
    }
68
}
69