Completed
Push — master ( f8702a...461e07 )
by Alexandr
04:08
created

FieldsAwareTrait::getFields()   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 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/*
3
* This file is a part of graphql-youshido project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 12/1/15 11:05 PM
7
*/
8
9
namespace Youshido\GraphQL\Type\Config\Traits;
10
11
12
use Youshido\GraphQL\Type\AbstractType;
13
use Youshido\GraphQL\Type\Field\Field;
14
use Youshido\GraphQL\Type\Field\InputField;
15
use Youshido\GraphQL\Type\TypeMap;
16
use Youshido\GraphQL\Validator\Exception\ConfigurationException;
17
18
/**
19
 * Class FieldsAwareTrait
20
 * @package Youshido\GraphQL\Type\Config\Traits
21
 */
22
trait FieldsAwareTrait
23
{
24
    protected $fields = [];
25
26 26
    public function buildFields()
27
    {
28 26
        $sourceFields = empty($this->data['fields']) ? [] : $this->data['fields'];
0 ignored issues
show
Bug introduced by
The property data does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
29 26
        $this->addFields($sourceFields);
30 26
    }
31
32
    /**
33
     * @param array $fieldsArray
34
     * @return $this
35
     */
36 26
    public function addFields($fieldsArray)
37
    {
38 26
        foreach ($fieldsArray as $fieldName => $fieldInfo) {
39 11
            if ($fieldInfo instanceof Field) {
40
                $this->fields[$fieldName] = $fieldInfo;
41
                continue;
42
            } elseif ($fieldInfo instanceof AbstractType) {
43 1
                $config    = [];
44 1
                $namedType = $fieldInfo->getNamedType();
45 1
                if ($fieldInfo->getConfig() && $fieldInfo->getConfig()->get('resolve')) {
46
                    $config['resolve'] = $fieldInfo->getConfig()->get('resolve');
47 1 View Code Duplication
                } elseif (empty($config['resolve']) && (method_exists($fieldInfo, 'resolve'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
48 1
                    $config['resolve'] = [$fieldInfo, 'resolve'];
49
                }
50 1
                if ($fieldInfo->getConfig() && $fieldInfo->getConfig()->hasArguments()) {
51
                    $config['args'] = $fieldInfo->getConfig()->getArguments();
52
                }
53 1
                $this->addField($fieldName, $namedType, $config);
54
            } else {
55 11
                $this->addField($fieldName, $fieldInfo['type'], $fieldInfo);
56
            }
57
        }
58 26
        return $this;
59
    }
60
61 24
    public function addField($name, $type, $config = [])
62
    {
63 24
        if (is_string($type)) {
64 22
            if (!TypeMap::isScalarType($type)) {
65
                throw new ConfigurationException('You can\'t pass ' . $type . ' as a string type.');
66
            }
67
68 22
            $type = TypeMap::getScalarTypeObject($type);
69 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
70 22
            if (empty($config['resolve']) && (method_exists($type, 'resolve'))) {
71 21
                $config['resolve'] = [$type, 'resolve'];
72
            }
73
        }
74
75 24
        $config['name'] = $name;
76 24
        $config['type'] = $type;
77
78
        if (
79 24
            isset($this->contextObject)
80 24
            && method_exists($this->contextObject, 'getKind')
0 ignored issues
show
Bug introduced by
The property contextObject does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
81 24
            && $this->contextObject->getKind() == TypeMap::KIND_INPUT_OBJECT
82
        ) {
83 1
            $field = new InputField($config);
84
        } else {
85 23
            $field = new Field($config);
86
        }
87
88
89 24
        $this->fields[$name] = $field;
90
91 24
        return $this;
92
    }
93
94
    /**
95
     * @param $name
96
     *
97
     * @return Field
98
     */
99 20
    public function getField($name)
100
    {
101 20
        return $this->hasField($name) ? $this->fields[$name] : null;
102
    }
103
104
    /**
105
     * @param $name
106
     *
107
     * @return bool
108
     */
109 20
    public function hasField($name)
110
    {
111 20
        return array_key_exists($name, $this->fields);
112
    }
113
114 2
    public function hasFields()
115
    {
116 2
        return !empty($this->fields);
117
    }
118
119
    /**
120
     * @return Field[]
121
     */
122 23
    public function getFields()
123
    {
124 23
        return $this->fields;
125
    }
126
127
    public function removeField($name)
128
    {
129
        if ($this->hasField($name)) {
130
            unset($this->fields[$name]);
131
        }
132
    }
133
}
134