Completed
Push — development ( 6b83c1...4f1043 )
by Torben
11:42
created

ErrorClassViewHelper::getValidationErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace DERHANSEN\SfEventMgt\ViewHelpers\Validation;
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 DERHANSEN\SfEventMgt\Domain\Model\Registration\Field;
12
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
13
14
/**
15
 * ViewHelper to render a given class when a field has validation errors
16
 */
17
class ErrorClassViewHelper extends AbstractViewHelper
18
{
19
    /**
20
     * Initialize arguments
21
     */
22
    public function initializeArguments()
23
    {
24
        $this->registerArgument('fieldname', 'string', 'A fieldname to be checked', false);
25
        $this->registerArgument('registrationField', 'object', 'A registration field record', false);
26
        $this->registerArgument('class', 'string', 'Classname if the field has an error', false, 'error-class');
27
        parent::initializeArguments();
28
    }
29
30
    /**
31
     * @return string
32
     */
33
    public function render()
34
    {
35
        $result = '';
36
        $validationErrors = $this->getValidationErrors();
37
38
        if (isset($this->arguments['fieldname'])) {
39
            $fieldname = 'registration.' . $this->arguments['fieldname'];
40
        } elseif (isset($this->arguments['registrationField']) &&
41
            $this->arguments['registrationField'] instanceof Field) {
42
            $fieldname = 'registration.fields.' . $this->arguments['registrationField']->getUid();
43
        } else {
44
            return '';
45
        }
46
47
        foreach ($validationErrors as $validationFieldName => $fieldValidationErrors) {
48
            if ($validationFieldName === $fieldname) {
49
                $result = $this->arguments['class'];
50
                break;
51
            }
52
        }
53
        return $result;
54
    }
55
56
    /**
57
     * @return array
58
     */
59
    protected function getValidationErrors()
60
    {
61
        $validationResults = $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.

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...
62
            ->getRequest()->getOriginalRequestMappingResults();
63
        return $validationResults->getFlattenedErrors();
64
    }
65
}
66