ErrorClassViewHelper::initializeArguments()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace DERHANSEN\SfEventMgt\ViewHelpers\Validation;
13
14
use DERHANSEN\SfEventMgt\Domain\Model\Registration\Field;
15
use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
17
18
/**
19
 * ViewHelper to render a given class when a field has validation errors
20
 */
21
class ErrorClassViewHelper extends AbstractViewHelper
22
{
23
    public function initializeArguments(): void
24
    {
25
        $this->registerArgument('fieldname', 'string', 'A fieldname to be checked', false);
26
        $this->registerArgument('registrationField', 'object', 'A registration field record', false);
27
        $this->registerArgument('class', 'string', 'Classname if the field has an error', false, 'error-class');
28
        parent::initializeArguments();
29
    }
30
31
    public function render(): string
32
    {
33
        $result = '';
34
        $validationErrors = $this->getValidationErrors();
35
36
        if (isset($this->arguments['fieldname'])) {
37
            $fieldname = 'registration.' . $this->arguments['fieldname'];
38
        } elseif (isset($this->arguments['registrationField']) &&
39
            $this->arguments['registrationField'] instanceof Field) {
40
            $fieldname = 'registration.fields.' . $this->arguments['registrationField']->getUid();
41
        } else {
42
            return '';
43
        }
44
45
        foreach ($validationErrors as $validationFieldName => $fieldValidationErrors) {
46
            if ($validationFieldName === $fieldname) {
47
                $result = $this->arguments['class'];
48
                break;
49
            }
50
        }
51
52
        return $result;
53
    }
54
55
    protected function getValidationErrors(): array
56
    {
57
        /** @var ExtbaseRequestParameters $extbaseRequestParameters */
58
        $extbaseRequestParameters = $this->renderingContext->getRequest()->getAttribute('extbase');
0 ignored issues
show
Bug introduced by
The method getRequest() does not exist on TYPO3Fluid\Fluid\Core\Re...nderingContextInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
        $extbaseRequestParameters = $this->renderingContext->/** @scrutinizer ignore-call */ getRequest()->getAttribute('extbase');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
        $validationResults = $extbaseRequestParameters->getOriginalRequestMappingResults();
60
61
        return $validationResults->getFlattenedErrors();
62
    }
63
}
64