Completed
Push — master ( da1622...d3aaf0 )
by Alexandr
02:54
created

NonNullType::getNullableType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/*
3
 * This file is a part of GraphQL project.
4
 *
5
 * @author Alexandr Viniychuk <[email protected]>
6
 * created: 3:40 PM 4/29/16
7
 */
8
9
namespace Youshido\GraphQL\Type;
10
11
12
use Youshido\GraphQL\Config\Traits\ConfigAwareTrait;
13
use Youshido\GraphQL\Exception\ConfigurationException;
14
15
final class NonNullType extends AbstractType implements CompositeTypeInterface
16
{
17
    use ConfigAwareTrait;
18
19
    private $_typeOf;
20
21
    /**
22
     * NonNullType constructor.
23
     *
24
     * @param AbstractType|string $fieldType
25
     *
26
     * @throws ConfigurationException
27
     */
28 60
    public function __construct($fieldType)
29
    {
30 60
        if (!TypeService::isGraphQLType($fieldType)) {
31 1
            throw new ConfigurationException('NonNullType accepts only GraphpQL Types as argument');
32
        }
33 59
        if (TypeService::isScalarType($fieldType)) {
34 59
            $fieldType = TypeFactory::getScalarType($fieldType);
0 ignored issues
show
Bug introduced by
It seems like $fieldType defined by \Youshido\GraphQL\Type\T...tScalarType($fieldType) on line 34 can also be of type object<Youshido\GraphQL\Type\AbstractType>; however, Youshido\GraphQL\Type\TypeFactory::getScalarType() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
35 59
        }
36
37 59
        $this->_typeOf = $fieldType;
38 59
    }
39
40 8
    public function getName()
41
    {
42 8
        return null;
43
    }
44
45 32
    public function getKind()
46
    {
47 32
        return TypeMap::KIND_NON_NULL;
48
    }
49
50 1
    public function resolve($value)
51
    {
52 1
        return $value;
53
    }
54
55 16
    public function isValidValue($value)
56
    {
57 16
        if ($value === null) {
58 7
            return false;
59
        }
60
61 15
        return $this->getNullableType()->isValidValue($value);
62
    }
63
64 9
    public function isCompositeType()
65
    {
66 9
        return true;
67
    }
68
69 7
    public function isInputType()
70
    {
71 7
        return true;
72
    }
73
74 15
    public function getNamedType()
75
    {
76 15
        return $this->getTypeOf();
77
    }
78
79 29
    public function getNullableType()
80
    {
81 29
        return $this->getTypeOf();
82
    }
83
84 32
    public function getTypeOf()
85
    {
86 32
        return $this->_typeOf;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->_typeOf; of type Youshido\GraphQL\Type\AbstractType|string adds the type string to the return on line 86 which is incompatible with the return type declared by the interface Youshido\GraphQL\Type\Co...ypeInterface::getTypeOf of type Youshido\GraphQL\Type\AbstractType.
Loading history...
87
    }
88
89 5
    public function parseValue($value)
90
    {
91 5
        return $this->getNullableType()->parseValue($value);
92
    }
93
94
95
}
96