Completed
Push — unit-tests-validation ( 34c29c...a12b55 )
by Romain
02:46
created

FieldIsValidCondition::checkConditionConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 7
nc 2
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\Condition\Items;
15
16
use Romm\Formz\AssetHandler\Html\DataAttributesAssetHandler;
17
use Romm\Formz\Condition\Exceptions\InvalidConditionException;
18
use Romm\Formz\Condition\Processor\DataObject\PhpConditionDataObject;
19
20
/**
21
 * This condition will match when a field is valid (its validation returned no
22
 * error).
23
 */
24
class FieldIsValidCondition extends AbstractConditionItem
25
{
26
    const CONDITION_NAME = 'fieldIsValid';
27
28
    /**
29
     * @inheritdoc
30
     * @var array
31
     */
32
    protected static $javaScriptFiles = [
33
        'EXT:formz/Resources/Public/JavaScript/Conditions/Formz.Condition.FieldIsValid.js'
34
    ];
35
36
    /**
37
     * @var string
38
     * @validate NotEmpty
39
     */
40
    protected $fieldName;
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function getCssResult()
46
    {
47
        return '[' . DataAttributesAssetHandler::getFieldDataValidKey($this->fieldName) . '="1"]';
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function getJavaScriptResult()
54
    {
55
        return $this->getDefaultJavaScriptCall(['fieldName' => $this->fieldName]);
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public function getPhpResult(PhpConditionDataObject $dataObject)
62
    {
63
        $field = $this->formObject
64
            ->getConfiguration()
65
            ->getField($this->fieldName);
66
        $result = $dataObject->getFormValidator()
67
            ->validateField($field);
0 ignored issues
show
Bug introduced by
It seems like $field defined by $this->formObject->getCo...Field($this->fieldName) on line 63 can be null; however, Romm\Formz\Validation\Va...ecutor::validateField() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
68
69
        return false === $result->forProperty($this->fieldName)->hasErrors()
70
            && false === $result->fieldIsDeactivated($field);
0 ignored issues
show
Bug introduced by
It seems like $field defined by $this->formObject->getCo...Field($this->fieldName) on line 63 can be null; however, Romm\Formz\Error\FormResult::fieldIsDeactivated() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
71
    }
72
73
    /**
74
     * @see validateConditionConfiguration()
75
     * @throws InvalidConditionException
76
     * @return bool
77
     */
78
    protected function checkConditionConfiguration()
79
    {
80
        $configuration = $this->formObject->getConfiguration();
81
82
        if (false === $configuration->hasField($this->fieldName)) {
83
            throw new InvalidConditionException(
84
                'The field "' . $this->fieldName . '" does not exist.',
85
                1488183577
86
            );
87
        }
88
89
        return true;
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function getFieldName()
96
    {
97
        return $this->fieldName;
98
    }
99
100
    /**
101
     * @param string $fieldName
102
     * @return $this
103
     */
104
    public function setFieldName($fieldName)
105
    {
106
        $this->fieldName = $fieldName;
107
108
        return $this;
109
    }
110
}
111