FieldContainer   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 2
dl 0
loc 113
c 0
b 0
f 0
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getFields() 0 4 1
A setFields() 0 6 1
A addFields() 0 6 1
A getModel() 0 4 1
A setModel() 0 6 1
A getResolveConfig() 0 4 1
A setResolveConfig() 0 6 1
A mergeResolveConfig() 0 6 1
A toMapping() 0 11 2
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
     * @param array $fields
45
     * @return $this
46
     */
47
    public function addFields(array $fields)
48
    {
49
        $this->fields = array_merge($this->fields, $fields);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->fields, $fields) of type array is incompatible with the declared type array<integer,object<Art...LMapper\Mapping\Field>> of property $fields.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
50
51
        return $this;
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    public function getModel()
58
    {
59
        return $this->model;
60
    }
61
62
    /**
63
     * @param string $model
64
     * @return $this
65
     */
66
    public function setModel($model)
67
    {
68
        $this->model = $model;
69
70
        return $this;
71
    }
72
73
    /**
74
     * @return array
75
     */
76
    public function getResolveConfig()
77
    {
78
        return $this->resolveConfig;
79
    }
80
81
    /**
82
     * @param array $resolveConfig
83
     * @return $this
84
     */
85
    public function setResolveConfig(array $resolveConfig)
86
    {
87
        $this->resolveConfig = $resolveConfig;
88
89
        return $this;
90
    }
91
92
    /**
93
     * @param array $resolveConfig
94
     * @return $this
95
     */
96
    public function mergeResolveConfig(array $resolveConfig)
97
    {
98
        $this->resolveConfig = array_merge($resolveConfig, $this->resolveConfig);
99
100
        return $this;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function toMapping()
107
    {
108
        $fieldsMapping = [];
109
        foreach ($this->fields as $field) {
110
            $fieldsMapping[$field->getName()] = $field->toMapping();
111
        }
112
113
        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...
114
            'fields' => $fieldsMapping,
115
        ] + parent::toMapping();
116
    }
117
}
118