Completed
Push — master ( 4bb4eb...402013 )
by Arthur
03:35
created

FieldContainer::setModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Arthem\GraphQLMapper\Mapping;
4
5
abstract class FieldContainer extends AbstractType
6
{
7
    /**
8
     * @var Field[]
9
     */
10
    private $fields = [];
11
12
    /**
13
     * The model class
14
     *
15
     * @var string
16
     */
17
    private $model;
18
19
    /**
20
     * @var array
21
     */
22
    private $resolveConfig = [];
23
24
    /**
25
     * @return Field[]
26
     */
27
    public function getFields()
28
    {
29
        return $this->fields;
30
    }
31
32
    /**
33
     * @param array $fields
34
     * @return $this
35
     */
36
    public function setFields(array $fields)
37
    {
38
        $this->fields = $fields;
39
40
        return $this;
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    public function getModel()
47
    {
48
        return $this->model;
49
    }
50
51
    /**
52
     * @param string $model
53
     * @return $this
54
     */
55
    public function setModel($model)
56
    {
57
        $this->model = $model;
58
59
        return $this;
60
    }
61
62
    /**
63
     * @return array
64
     */
65
    public function getResolveConfig()
66
    {
67
        return $this->resolveConfig;
68
    }
69
70
    /**
71
     * @param array $resolveConfig
72
     * @return $this
73
     */
74
    public function setResolveConfig(array $resolveConfig)
75
    {
76
        $this->resolveConfig = $resolveConfig;
77
78
        return $this;
79
    }
80
81
    /**
82
     * @param array $resolveConfig
83
     * @return $this
84
     */
85
    public function mergeResolveConfig(array $resolveConfig)
86
    {
87
        $this->resolveConfig = array_merge($resolveConfig, $this->resolveConfig);
88
89
        return $this;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function toMapping()
96
    {
97
        $fieldsMapping = [];
98
        foreach ($this->fields as $field) {
99
            $fieldsMapping[$field->getName()] = $field->toMapping();
100
        }
101
102
        return [
0 ignored issues
show
Best Practice introduced by
The expression return array('fields' =>... + parent::toMapping(); seems to be an array, but some of its elements' types (array) are incompatible with the return type of the parent method Arthem\GraphQLMapper\Map...AbstractType::toMapping of type array<string,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 new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return '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...
103
            'fields' => $fieldsMapping,
104
        ] + parent::toMapping();
105
    }
106
}
107