Completed
Pull Request — master (#311)
by Łukasz
06:56
created

PropertyAccessDisplay::createPropertyAccessor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * (c) FSi sp. z o.o. <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FSi\Bundle\AdminBundle\Display;
13
14
use InvalidArgumentException;
15
use Symfony\Component\PropertyAccess\PropertyAccess;
16
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
17
18
class PropertyAccessDisplay implements Display
19
{
20
    /**
21
     * @var Property[]
22
     */
23
    private $data = [];
24
25
    /**
26
     * @var object|array
27
     */
28
    private $object;
29
30
    /**
31
     * @var PropertyAccessorInterface
32
     */
33
    private $accessor;
34
35
    /**
36
     * @param object|array $object
37
     */
38
    public function __construct($object)
39
    {
40
        $this->validateObject($object);
41
42
        $this->object = $object;
43
        $this->accessor = $this->createPropertyAccessor();
44
    }
45
46
    public function add($path, ?string $label = null, array $valueFormatters = []): Display
47
    {
48
        $this->data[] = new Property(
49
            $this->accessor->getValue($this->object, $path),
50
            $label,
51
            $valueFormatters
52
        );
53
54
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (FSi\Bundle\AdminBundle\D...y\PropertyAccessDisplay) is incompatible with the return type declared by the interface FSi\Bundle\AdminBundle\Display\Display::add of type self.

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...
55
    }
56
57
    public function getData(): array
58
    {
59
        return $this->data;
60
    }
61
62
    private function validateObject($object): void
63
    {
64
        if (!is_object($object) && !is_array($object)) {
65
            throw new InvalidArgumentException(sprintf(
66
                'Argument used to create "%s" must be an object or an array, got "%s" instead.',
67
                get_class($this),
68
                gettype($object)
69
            ));
70
        }
71
    }
72
73
    private function createPropertyAccessor(): PropertyAccessorInterface
74
    {
75
        $accessorBuilder = PropertyAccess::createPropertyAccessorBuilder();
76
        $accessorBuilder->enableMagicCall();
77
78
        return $accessorBuilder->getPropertyAccessor();
79
    }
80
}
81