Field   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 1
dl 0
loc 195
c 0
b 0
f 0
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A getProperty() 0 4 1
A setProperty() 0 6 1
A getType() 0 4 1
A setType() 0 6 1
A getResolvedType() 0 4 1
A setResolvedType() 0 10 3
A mergeResolveConfig() 0 6 1
A getResolve() 0 4 1
A setResolve() 0 6 1
A getResolveConfig() 0 4 1
A setResolveConfig() 0 6 1
A mergeRevolveConfig() 0 6 1
A getArguments() 0 4 1
A setArguments() 0 6 1
A toMapping() 0 17 3
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 property name if different from the name
11
     *
12
     * @var string
13
     */
14
    private $property;
15
16
    /**
17
     * @var string
18
     */
19
    private $type;
20
21
    /**
22
     * @var callable|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 getProperty()
45
    {
46
        return $this->property;
47
    }
48
49
    /**
50
     * @param string $property
51
     * @return $this
52
     */
53
    public function setProperty($property)
54
    {
55
        $this->property = $property;
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
        if (!is_callable($resolvedType) && !$resolvedType instanceof GQLType) {
94
            throw new \RuntimeException(sprintf('Invalid $resolvedType, got "%s"', gettype($resolvedType)));
95
        }
96
97
        $this->resolvedType = $resolvedType;
98
99
        return $this;
100
    }
101
102
    /**
103
     * @param array $resolveConfig
104
     * @return $this
105
     */
106
    public function mergeResolveConfig(array $resolveConfig)
107
    {
108
        $this->resolveConfig = array_merge($resolveConfig, $this->resolveConfig);
109
110
        return $this;
111
    }
112
113
    /**
114
     * @return callable
115
     */
116
    public function getResolve()
117
    {
118
        return $this->resolve;
119
    }
120
121
    /**
122
     * @param callable $resolve
123
     * @return $this
124
     */
125
    public function setResolve($resolve)
126
    {
127
        $this->resolve = $resolve;
128
129
        return $this;
130
    }
131
132
    /**
133
     * @return array
134
     */
135
    public function getResolveConfig()
136
    {
137
        return $this->resolveConfig;
138
    }
139
140
    /**
141
     * @param array $resolveConfig
142
     * @return $this
143
     */
144
    public function setResolveConfig(array $resolveConfig)
145
    {
146
        $this->resolveConfig = $resolveConfig;
147
148
        return $this;
149
    }
150
151
    /**
152
     * @param array $config
153
     * @return $this
154
     */
155
    public function mergeRevolveConfig(array $config)
156
    {
157
        $this->resolveConfig = array_merge($config, $this->resolveConfig);
158
159
        return $this;
160
    }
161
162
    /**
163
     * @return Field[]
164
     */
165
    public function getArguments()
166
    {
167
        return $this->arguments;
168
    }
169
170
    /**
171
     * @param Field[] $arguments
172
     * @return $this
173
     */
174
    public function setArguments(array $arguments)
175
    {
176
        $this->arguments = $arguments;
177
178
        return $this;
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     */
184
    public function toMapping()
185
    {
186
        $mapping = [
187
                'type'           => $this->resolvedType,
188
                'resolve'        => $this->resolve,
189
                'resolve_config' => $this->resolveConfig,
190
            ] + parent::toMapping();
191
192
        if (!empty($this->arguments)) {
193
            $mapping['args'] = [];
194
            foreach ($this->arguments as $argument) {
195
                $mapping['args'][$argument->getName()] = $argument->toMapping();
196
            }
197
        }
198
199
        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...
200
    }
201
}
202