AbstractField::prepareAttributesCollection()   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
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 *
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 *
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Component\Form\Elements;
14
15
use Symfony\Component\OptionsResolver\OptionsResolver;
16
use Symfony\Component\PropertyAccess\PropertyPath;
17
use WellCommerce\Component\Form\DataTransformer\DataTransformerInterface;
18
use WellCommerce\Component\Form\Filters\FilterInterface;
19
20
/**
21
 * Class AbstractField
22
 *
23
 * @author Adam Piotrowski <[email protected]>
24
 */
25
abstract class AbstractField extends AbstractContainer
26
{
27
    /**
28
     * @var mixed
29
     */
30
    protected $value;
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function configureOptions(OptionsResolver $resolver)
36
    {
37
        parent::configureOptions($resolver);
38
39
        $resolver->setDefaults([
40
            'comment'      => '',
41
            'error'        => [],
42
            'default'      => null,
43
            'dependencies' => [],
44
            'rules'        => [],
45
            'filters'      => [],
46
            'transformer'  => null,
47
        ]);
48
49
        $resolver->setNormalizer('property_path', function ($options) {
50
            return new PropertyPath($options['name']);
51
        });
52
53
        $resolver->setAllowedTypes('comment', 'string');
54
        $resolver->setAllowedTypes('error', 'array');
55
        $resolver->setAllowedTypes('dependencies', 'array');
56
        $resolver->setAllowedTypes('rules', 'array');
57
        $resolver->setAllowedTypes('filters', 'array');
58
        $resolver->setAllowedTypes('transformer', ['null', DataTransformerInterface::class]);
59
    }
60
61
    protected function getFilters()
62
    {
63
        return $this->options['filters'];
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function addChild(ElementInterface $element)
70
    {
71
        throw new \BadMethodCallException(
72
            sprintf('Cannot add child "%s" as it is allowed only for element of type fieldset', get_class($element))
73
        );
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function addFilter(FilterInterface $filter)
80
    {
81
        $this->options['filters'][] = $filter;
82
    }
83
84
    public function setValue($value)
85
    {
86
        $this->value = $value;
87
    }
88
89
    public function getValue()
90
    {
91
        return $this->value;
92
    }
93
94
    public function getError()
95
    {
96
        if (is_array($this->error)) {
97
            return implode('.', array_values($this->error));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return implode('.', array_values($this->error)); (string) is incompatible with the return type of the parent method WellCommerce\Component\F...ractContainer::getError of type array.

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...
98
        }
99
100
        return '';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return ''; (string) is incompatible with the return type of the parent method WellCommerce\Component\F...ractContainer::getError of type array.

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...
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function prepareAttributesCollection(AttributeCollection $collection)
107
    {
108
        parent::prepareAttributesCollection($collection);
109
        $collection->add(new Attribute('sValue', $this->value));
110
        $collection->add(new Attribute('sComment', $this->getOption('comment')));
111
    }
112
}
113