NonNullModifier::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 8
ccs 4
cts 5
cp 0.8
rs 9.4285
cc 3
eloc 4
nc 2
nop 1
crap 3.072
1
<?php
2
3
namespace Fubhy\GraphQL\Type\Definition\Types;
4
5
/**
6
 * Non-Null Modifier.
7
 *
8
 * A non-null is a kind of type marker, a wrapping type which points to another
9
 * type. Non-null types enforce that their values are never null and can ensure
10
 * an error is raised if this ever occurs during a request. It is useful for
11
 * fields which you can make a strong guarantee on non-nullability, for example
12
 * usually the id field of a database row will never be null.
13
 *
14
 * Note: The enforcement of non-nullability occurs within the executor.
15
 */
16 View Code Duplication
class NonNullModifier extends Type implements TypeInterface, InputTypeInterface, OutputTypeInterface, ModifierInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
{
18
    /**
19
     * @var callable|\Fubhy\GraphQL\Type\Definition\Types\TypeInterface
20
     */
21
    protected $ofType;
22
23
    /**
24
     * Constructor.
25
     *
26
     * @param callable|\Fubhy\GraphQL\Type\Definition\Types\TypeInterface $ofType
27
     */
28 249
    public function __construct($ofType)
29
    {
30 249
        if (!($ofType instanceof TypeInterface) && !is_callable($ofType)) {
31
            throw new \LogicException('Expected callable or instance of \Fubhy\GraphQL\Type\Definition\Types\TypeInterface.');
32
        }
33
34 249
        $this->ofType = $ofType;
0 ignored issues
show
Documentation Bug introduced by
It seems like $ofType can also be of type object<Fubhy\GraphQL\Typ...on\Types\TypeInterface>. However, the property $ofType is declared as type callable. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
35 249
    }
36
37
    /**
38
     * @return \Fubhy\GraphQL\Type\Definition\Types\TypeInterface
39
     */
40 267
    public function getWrappedType()
41
    {
42 267
        if (is_callable($this->ofType)) {
43 159
            $this->ofType = call_user_func($this->ofType);
44
        }
45
46 264
        return $this->ofType;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 18
    public function getName()
53
    {
54 18
        return (string) $this->getWrappedType() . '!';
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function getDescription()
61
    {
62
        return NULL;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 9
    public function __toString()
69
    {
70 9
        return $this->getName();
71
    }
72
}
73