Type   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 78.18%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
c 1
b 0
f 0
lcom 2
cbo 5
dl 0
loc 198
ccs 43
cts 55
cp 0.7818
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A booleanType() 0 8 2
A floatType() 0 8 2
A idType() 0 8 2
A intType() 0 8 2
A stringType() 0 8 2
A __construct() 0 5 1
A getName() 0 4 1
A getDescription() 0 4 1
A __toString() 0 4 1
A isInputType() 0 4 1
A isOutputType() 0 4 1
A isLeafType() 0 4 1
A isCompositeType() 0 4 1
A isAbstractType() 0 4 1
A getNullableType() 0 8 2
A getUnmodifiedType() 0 9 3
1
<?php
2
3
namespace Fubhy\GraphQL\Type\Definition\Types;
4
5
use Fubhy\GraphQL\Type\Definition\Types\Scalars\BooleanType;
6
use Fubhy\GraphQL\Type\Definition\Types\Scalars\FloatType;
7
use Fubhy\GraphQL\Type\Definition\Types\Scalars\IdType;
8
use Fubhy\GraphQL\Type\Definition\Types\Scalars\IntType;
9
use Fubhy\GraphQL\Type\Definition\Types\Scalars\StringType;
10
11
/**
12
 * These are all of the possible kinds of types.
13
 */
14
abstract class Type implements TypeInterface
15
{
16
    /**
17
     * @var \Fubhy\GraphQL\Type\Definition\Types\Scalars\BooleanType
18
     */
19
    protected static $boolean;
20
21
    /**
22
     * @var \Fubhy\GraphQL\Type\Definition\Types\Scalars\FloatType
23
     */
24
    protected static $float;
25
26
    /**
27
     * @var \Fubhy\GraphQL\Type\Definition\Types\Scalars\IdType
28
     */
29
    protected static $id;
30
31
    /**
32
     * @var \Fubhy\GraphQL\Type\Definition\Types\Scalars\IntType
33
     */
34
    protected static $integer;
35
36
    /**
37
     * @var \Fubhy\GraphQL\Type\Definition\Types\Scalars\StringType
38
     */
39
    protected static $string;
40
41
    /**
42
     * @var string
43
     */
44
    protected $name;
45
46
    /**
47
     * @var string
48
     */
49
    protected $description;
50
51
    /**
52
     * @return \Fubhy\GraphQL\Type\Definition\Types\Scalars\BooleanType
53
     */
54 246
    public static function booleanType()
55
    {
56 246
        if (!isset(static::$boolean)) {
57 3
            static::$boolean = new BooleanType();
58 3
        }
59
60 246
        return static::$boolean;
61
    }
62
63
    /**
64
     * @return \Fubhy\GraphQL\Type\Definition\Types\Scalars\FloatType
65
     */
66 180
    public static function floatType()
67
    {
68 180
        if (!isset(static::$float)) {
69 3
            static::$float = new FloatType();
70 3
        }
71
72 180
        return static::$float;
73
    }
74
75
    /**
76
     * @return \Fubhy\GraphQL\Type\Definition\Types\Scalars\IdType
77
     */
78 153
    public static function idType()
79
    {
80 153
        if (!isset(static::$id)) {
81 3
            static::$id = new IdType();
82 3
        }
83
84 153
        return static::$id;
85
    }
86
87
    /**
88
     * @return \Fubhy\GraphQL\Type\Definition\Types\Scalars\IntType
89
     */
90 303
    public static function intType()
91
    {
92 303
        if (!isset(static::$integer)) {
93
            static::$integer = new IntType();
94
        }
95
96 303
        return static::$integer;
97
    }
98
99
    /**
100
     * @return \Fubhy\GraphQL\Type\Definition\Types\Scalars\StringType
101
     */
102 348
    public static function stringType()
103
    {
104 348
        if (!isset(static::$string)) {
105 3
            static::$string = new StringType();
106 3
        }
107
108 348
        return static::$string;
109
    }
110
111
    /**
112
     * Constructor.
113
     *
114
     * @param string $name
115
     * @param string|null $description
116
     */
117 369
    public function __construct($name, $description = NULL)
118
    {
119 369
        $this->name = $name;
120 369
        $this->description = $description;
121 369
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 219
    public function getName()
127
    {
128 219
        return $this->name;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 9
    public function getDescription()
135
    {
136 9
        return $this->description;
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 111
    public function __toString()
143
    {
144 111
        return $this->getName();
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150 18
    public function isInputType()
151
    {
152 18
        return $this->getUnmodifiedType() instanceof InputTypeInterface;
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158 18
    public function isOutputType()
159
    {
160 18
        return $this->getUnmodifiedType() instanceof OutputTypeInterface;
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166
    public function isLeafType()
167
    {
168
        return $this->getUnmodifiedType() instanceof LeafTypeInterface;
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174
    public function isCompositeType()
175
    {
176
        return $this->getUnmodifiedType() instanceof CompositeTypeInterface;
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182
    public function isAbstractType()
183
    {
184
        return $this->getUnmodifiedType() instanceof AbstractTypeInterface;
185
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190
    public function getNullableType()
191
    {
192
        if ($this instanceof NonNullModifier) {
193
            return $this->getWrappedType();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Fubhy\GraphQL\Type\Definition\Types\Type as the method getWrappedType() does only exist in the following sub-classes of Fubhy\GraphQL\Type\Definition\Types\Type: Fubhy\GraphQL\Type\Definition\Types\ListModifier, Fubhy\GraphQL\Type\Defin...n\Types\NonNullModifier. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
194
        }
195
196
        return $this;
197
    }
198
199
    /**
200
     * {@inheritdoc}
201
     */
202 36
    public function getUnmodifiedType()
203
    {
204 36
        $return = $this;
205 36
        while ($return instanceof ListModifier || $return instanceof NonNullModifier) {
206 36
            $return = $return->getWrappedType();
207 36
        }
208
209 36
        return $return;
210
    }
211
}
212