Completed
Push — master ( a7480b...987705 )
by Portey
03:46
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 25
    public function buildFields()
27
    {
28 25
        $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 25
        foreach ($sourceFields as $fieldName => $fieldInfo) {
30 11
            if ($fieldInfo instanceof Field || $fieldInfo instanceof AbstractType) {
31 1
                $this->fields[$fieldName] = $fieldInfo;
32 1
                continue;
33
            };
34
35 10
            $this->addField($fieldName, $fieldInfo['type'], $fieldInfo);
36 25
        }
37 25
    }
38
39
    /**
40
     * @param array $fieldsArray
41
     */
42
    public function addFields($fieldsArray)
43
    {
44
        foreach($fieldsArray as $fieldName => $fieldConfig) {
45
            if (is_object($fieldConfig)) {
46
                $this->addField($fieldName, $fieldConfig);
47
            } else {
48
                $this->addField($fieldName, $fieldConfig['type'], $fieldConfig);
49
            }
50
        }
51
    }
52
53 22
    public function addField($name, $type, $config = [])
54
    {
55 22
        if (is_string($type)) {
56 21
            if (!TypeMap::isScalarType($type)) {
57
                throw new ConfigurationException('You can\'t pass ' . $type . ' as a string type.');
58
            }
59
60 21
            $type = TypeMap::getScalarTypeObject($type);
61 21
        }
62
63 22
        $config['name'] = $name;
64 22
        $config['type'] = $type;
65
66
        if(
67 22
            isset($this->contextObject)
68 22
            && 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...
69 22
            && $this->contextObject->getKind() == TypeMap::KIND_INPUT_OBJECT) {
70 1
            $field = new InputField($config);
71 1
        } else {
72 21
            $field = new Field($config);
73
        }
74
75
76 22
        $this->fields[$name] = $field;
77
78 22
        return $this;
79
    }
80
81
    /**
82
     * @param $name
83
     *
84
     * @return Field
85
     */
86 19
    public function getField($name)
87
    {
88 19
        return $this->hasField($name) ? $this->fields[$name] : null;
89
    }
90
91
    /**
92
     * @param $name
93
     *
94
     * @return bool
95
     */
96 19
    public function hasField($name)
97
    {
98 19
        return array_key_exists($name, $this->fields);
99
    }
100
101
    /**
102
     * @return Field[]
103
     */
104 8
    public function getFields()
105
    {
106 8
        return $this->fields;
107
    }
108
109
    public function removeField($name)
110
    {
111
        if ($this->hasField($name)) {
112
            unset($this->fields[$name]);
113
        }
114
    }
115
}