Completed
Push — master ( 5db45c...f208af )
by Arthur
02:39
created

Field::toMapping()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 17
rs 9.4286
cc 3
eloc 11
nc 2
nop 0
1
<?php
2
3
namespace Arthem\GraphQLMapper\Mapping;
4
5
use GraphQL\Type\Definition\Type as GQLType;
6
7
class Field extends AbstractType
8
{
9
    /**
10
     * The model field name
11
     *
12
     * @var string
13
     */
14
    private $field;
15
16
    /**
17
     * @var string
18
     */
19
    private $type;
20
21
    /**
22
     * @var GQLType
23
     */
24
    private $resolvedType;
25
26
    /**
27
     * @var callable
28
     */
29
    private $resolve;
30
31
    /**
32
     * @var array
33
     */
34
    private $resolveConfig = [];
35
36
    /**
37
     * @var Field[]
38
     */
39
    private $arguments = [];
40
41
    /**
42
     * @return string
43
     */
44
    public function getField()
45
    {
46
        return $this->field;
47
    }
48
49
    /**
50
     * @param string $field
51
     * @return $this
52
     */
53
    public function setField($field)
54
    {
55
        $this->field = $field;
56
57
        return $this;
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    public function getType()
64
    {
65
        return $this->type;
66
    }
67
68
    /**
69
     * @param string $type
70
     * @return $this
71
     */
72
    public function setType($type)
73
    {
74
        $this->type = $type;
75
76
        return $this;
77
    }
78
79
    /**
80
     * @return callable|GQLType
81
     */
82
    public function getResolvedType()
83
    {
84
        return $this->resolvedType;
85
    }
86
87
    /**
88
     * @param callable|GQLType $resolvedType
89
     * @return $this
90
     */
91
    public function setResolvedType($resolvedType)
92
    {
93
        $this->resolvedType = $resolvedType;
0 ignored issues
show
Documentation Bug introduced by
It seems like $resolvedType of type callable is incompatible with the declared type object<GraphQL\Type\Definition\Type> of property $resolvedType.

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...
94
95
        return $this;
96
    }
97
98
    /**
99
     * @return callable
100
     */
101
    public function getResolve()
102
    {
103
        return $this->resolve;
104
    }
105
106
    /**
107
     * @param callable $resolve
108
     * @return $this
109
     */
110
    public function setResolve($resolve)
111
    {
112
        $this->resolve = $resolve;
113
114
        return $this;
115
    }
116
117
    /**
118
     * @return array
119
     */
120
    public function getResolveConfig()
121
    {
122
        return $this->resolveConfig;
123
    }
124
125
    /**
126
     * @param array $resolveConfig
127
     * @return $this
128
     */
129
    public function setResolveConfig($resolveConfig)
130
    {
131
        $this->resolveConfig = $resolveConfig;
132
133
        return $this;
134
    }
135
136
    /**
137
     * @param array $config
138
     * @return $this
139
     */
140
    public function mergeRevolveConfig(array $config)
141
    {
142
        $this->resolveConfig = array_merge($this->resolveConfig, $config);
143
144
        return $this;
145
    }
146
147
    /**
148
     * @return Field[]
149
     */
150
    public function getArguments()
151
    {
152
        return $this->arguments;
153
    }
154
155
    /**
156
     * @param Field[] $arguments
157
     * @return $this
158
     */
159
    public function setArguments(array $arguments)
160
    {
161
        $this->arguments = $arguments;
162
163
        return $this;
164
    }
165
166
    /**
167
     * @return array
168
     */
169
    public function toMapping()
170
    {
171
        $mapping = [
172
                'type'           => $this->resolvedType,
173
                'resolve'        => $this->resolve,
174
                'resolve_config' => $this->resolveConfig,
175
            ] + parent::toMapping();
176
177
        if (!empty($this->arguments)) {
178
            $mapping['args'] = [];
179
            foreach ($this->arguments as $argument) {
180
                $mapping['args'][$argument->getName()] = $argument->toMapping();
181
            }
182
        }
183
184
        return $mapping;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $mapping; (array<string,callable>) is 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 '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...
185
    }
186
}
187