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

FieldsAwareTrait   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 64.1%

Importance

Changes 7
Bugs 0 Features 2
Metric Value
wmc 17
c 7
b 0
f 2
lcom 1
cbo 3
dl 0
loc 85
ccs 25
cts 39
cp 0.641
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
B buildFields() 0 12 5
A addField() 0 18 3
A getField() 0 4 2
A hasField() 0 4 1
A getFields() 0 4 1
A addFields() 0 10 3
A removeField() 0 6 2
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
}