Completed
Branch unit-test-asset-handler-connec... (029d08)
by Romain
01:55
created

AbstractActivation::getCondition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/*
3
 * 2016 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\Configuration\Form\Condition\Activation;
15
16
use Romm\ConfigurationObject\Service\Items\Parents\ParentsTrait;
17
use Romm\Formz\Configuration\AbstractFormzConfiguration;
18
use Romm\Formz\Configuration\Form\Condition\ConditionItemResolver;
19
use Romm\Formz\Configuration\Form\Form;
20
use TYPO3\CMS\Extbase\Utility\ArrayUtility;
21
22
abstract class AbstractActivation extends AbstractFormzConfiguration implements ActivationInterface
23
{
24
25
    use ParentsTrait;
26
27
    /**
28
     * @var string
29
     */
30
    protected $condition;
31
32
    /**
33
     * @var \ArrayObject<Romm\Formz\Configuration\Form\Condition\ConditionItemResolver>
34
     * @validate NotEmpty
35
     */
36
    protected $items = [];
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public function getCondition()
42
    {
43
        return $this->condition;
44
    }
45
46
    /**
47
     * Will merge the items with the ones from the `$activationCondition`
48
     * property of the root form configuration.
49
     *
50
     * @return ConditionItemResolver[]
51
     */
52
    public function getItems()
53
    {
54
        $activationCondition = $this->withFirstParent(
55
            Form::class,
56
            function (Form $formConfiguration) {
57
                return $formConfiguration->getActivationCondition();
58
            }
59
        );
60
        $activationCondition = ($activationCondition) ?: [];
61
62
        return ArrayUtility::arrayMergeRecursiveOverrule($activationCondition, $this->items);
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68
    public function hasItem($itemName)
69
    {
70
        $items = $this->getItems();
71
72
        return (true === isset($items[$itemName]));
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78
    public function getItem($itemName)
79
    {
80
        if (true === $this->hasItem($itemName)) {
81
            $items = $this->getItems();
82
83
            return $items[$itemName];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $items[$itemName]; (Romm\Formz\Configuration...n\ConditionItemResolver) is incompatible with the return type declared by the interface Romm\Formz\Configuration...ationInterface::getItem of type Romm\Formz\Condition\Items\AbstractConditionItem.

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...
84
        }
85
86
        return null;
87
    }
88
}
89