Completed
Push — master ( a7480b...987705 )
by Portey
03:46
created

FieldsAwareTrait   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 68.18%

Importance

Changes 8
Bugs 1 Features 2
Metric Value
wmc 20
c 8
b 1
f 2
lcom 1
cbo 4
dl 0
loc 94
ccs 30
cts 44
cp 0.6818
rs 10

7 Methods

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