Completed
Push — master ( 48a1e6...3c3b08 )
by Portey
03:48
created

NonNullType::parseValue()   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 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
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\Validator\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
     * @param AbstractType|string $fieldType
24
     * @throws ConfigurationException
25
     */
26 41
    public function __construct($fieldType)
27
    {
28 41
        if (!TypeService::isGraphQLType($fieldType)) {
29 1
            throw new ConfigurationException('NonNullType accepts only GraphpQL Types as argument');
30
        }
31 40
        if (TypeService::isScalarType($fieldType)) {
32 39
            $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 32 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...
33
        }
34
35 40
        $this->_typeOf = $fieldType;
36 40
    }
37
38 6
    public function getName()
39
    {
40 6
        return null;
41
    }
42
43 20
    public function getKind()
44
    {
45 20
        return TypeMap::KIND_NON_NULL;
46
    }
47
48 1
    public function resolve($value)
49
    {
50 1
        return $value;
51
    }
52
53 4
    public function isValidValue($value)
54
    {
55 4
        return $value !== null;
56
    }
57
58 10
    public function isCompositeType()
59
    {
60 10
        return true;
61
    }
62
63 14
    public function getNamedType()
64
    {
65 14
        return $this->getTypeOf();
66
    }
67
68 33
    public function getNullableType()
69
    {
70 33
        return $this->getTypeOf();
71
    }
72
73 35
    public function getTypeOf()
74
    {
75 35
        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 75 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...
76
    }
77
78 4
    public function parseValue($value)
79
    {
80 4
        return $this->getNullableType()->parseValue($value);
81
    }
82
83
84
}
85