FieldIsEmptyCondition   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 0
loc 63
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCssResult() 0 7 1
A getJavaScriptResult() 0 4 1
A getPhpResult() 0 6 1
A checkConditionConfiguration() 0 10 2
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
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
20
21
/**
22
 * This condition will match when a field is valid (its validation returned no
23
 * error).
24
 */
25
class FieldIsEmptyCondition extends AbstractConditionItem
26
{
27
    const CONDITION_NAME = 'fieldIsEmpty';
28
29
    /**
30
     * @inheritdoc
31
     * @var array
32
     */
33
    protected static $javaScriptFiles = [
34
        'EXT:formz/Resources/Public/JavaScript/Conditions/Formz.Condition.FieldIsEmpty.js'
35
    ];
36
37
    /**
38
     * @var string
39
     * @validate NotEmpty
40
     */
41
    protected $fieldName;
42
43
    /**
44
     * @inheritdoc
45
     */
46
    public function getCssResult()
47
    {
48
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('[' . \Romm...is->fieldName) . '])'); (string[]) is incompatible with the return type declared by the interface Romm\Formz\Condition\Ite...Interface::getCssResult of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
49
            '[' . DataAttributesAssetHandler::getFieldDataValueKey($this->fieldName) . '=""]',
50
            ':not([' . DataAttributesAssetHandler::getFieldDataValueKey($this->fieldName) . '])'
51
        ];
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function getJavaScriptResult()
58
    {
59
        return $this->getDefaultJavaScriptCall(['fieldName' => $this->fieldName]);
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65
    public function getPhpResult(PhpConditionDataObject $dataObject)
66
    {
67
        $value = ObjectAccess::getProperty($dataObject->getForm(), $this->fieldName);
68
69
        return empty($value);
70
    }
71
72
    /**
73
     * @see validateConditionConfiguration()
74
     * @throws InvalidConditionException
75
     * @return bool
76
     */
77
    protected function checkConditionConfiguration()
78
    {
79
        $configuration = $this->formObject->getConfiguration();
80
81
        if (false === $configuration->hasField($this->fieldName)) {
82
            throw InvalidConditionException::conditionFieldIsEmptyFieldNotFound($this->fieldName);
83
        }
84
85
        return true;
86
    }
87
}
88