NonNullModifier   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 80%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 1
cbo 1
dl 57
loc 57
ccs 12
cts 15
cp 0.8
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 3
A getWrappedType() 8 8 2
A getName() 4 4 1
A getDescription() 4 4 1
A __toString() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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