Completed
Push — master ( 120e4f...bd44b4 )
by Portey
07:04
created

FieldsAwareTrait::removeField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4286
cc 2
eloc 3
nc 2
nop 1
crap 6
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\TypeMap;
15
use Youshido\GraphQL\Validator\Exception\ConfigurationException;
16
17
/**
18
 * Class FieldsAwareTrait
19
 * @package Youshido\GraphQL\Type\Config\Traits
20
 */
21
trait FieldsAwareTrait
22
{
23
    protected $fields = [];
24
25 20
    public function buildFields()
26
    {
27 20
        $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...
28 20
        foreach ($sourceFields as $fieldName => $fieldInfo) {
29 10
            if ($fieldInfo instanceof Field || $fieldInfo instanceof AbstractType) {
30 1
                $this->fields[$fieldName] = $fieldInfo;
31 1
                continue;
32
            };
33
34 9
            $this->addField($fieldName, $fieldInfo['type'], $fieldInfo);
35 20
        }
36 20
    }
37
38
    /**
39
     * @param array $fieldsArray
40
     */
41
    public function addFields($fieldsArray)
42
    {
43
        foreach($fieldsArray as $fieldName => $fieldConfig) {
44
            if (is_object($fieldConfig)) {
45
                $this->addField($fieldName, $fieldConfig);
46
            } else {
47
                $this->addField($fieldName, $fieldConfig['type'], $fieldConfig);
48
            }
49
        }
50
    }
51
52 17
    public function addField($name, $type, $config = [])
53
    {
54 17
        if (is_string($type)) {
55 16
            if (!TypeMap::isScalarType($type)) {
56
                throw new ConfigurationException('You can\'t pass ' . $type . ' as a string type.');
57
            }
58
59 16
            $type = TypeMap::getScalarTypeObject($type);
60 16
        }
61
62 17
        $config['name'] = $name;
63 17
        $config['type'] = $type;
64 17
        $field          = new Field($config);
65
66 17
        $this->fields[$name] = $field;
67
68 17
        return $this;
69
    }
70
71
    /**
72
     * @param $name
73
     *
74
     * @return Field
75
     */
76 14
    public function getField($name)
77
    {
78 14
        return $this->hasField($name) ? $this->fields[$name] : null;
79
    }
80
81
    /**
82
     * @param $name
83
     *
84
     * @return bool
85
     */
86 14
    public function hasField($name)
87
    {
88 14
        return array_key_exists($name, $this->fields);
89
    }
90
91
    /**
92
     * @return Field[]
93
     */
94 7
    public function getFields()
95
    {
96 7
        return $this->fields;
97
    }
98
99
    public function removeField($name)
100
    {
101
        if ($this->hasField($name)) {
102
            unset($this->fields[$name]);
103
        }
104
    }
105
}