Completed
Push — master ( e8c337...7f89eb )
by Alexandr
02:46
created

FieldsAwareTrait   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.06%

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 29
c 6
b 0
f 1
lcom 1
cbo 3
dl 0
loc 78
ccs 33
cts 34
cp 0.9706
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B buildFields() 0 12 5
A addFields() 0 10 3
A addField() 0 18 3
A getField() 0 4 2
A hasField() 0 4 1
A getFields() 0 4 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\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 14
    public function buildFields()
26
    {
27 14
        $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 14
        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 14
        }
36 14
    }
37
38
    /**
39
     * @param array $fieldsArray
40
     */
41 1
    public function addFields($fieldsArray)
42
    {
43 1
        foreach($fieldsArray as $fieldName => $fieldConfig) {
44 1
            if (is_object($fieldConfig)) {
45 1
                $this->addField($fieldName, $fieldConfig);
46 1
            } else {
47 1
                $this->addField($fieldName, $fieldConfig['type'], $fieldConfig);
48
            }
49 1
        }
50 1
    }
51
52 11
    public function addField($name, $type, $config = [])
53
    {
54 11
        if (is_string($type)) {
55 10
            if (!TypeMap::isScalarType($type)) {
56
                throw new ConfigurationException('You can\'t pass ' . $type . ' as a string type.');
57
            }
58
59 10
            $type = TypeMap::getScalarTypeObject($type);
60 10
        }
61
62 11
        $config['name'] = $name;
63 11
        $config['type'] = $type;
64 11
        $field          = new Field($config);
65
66 11
        $this->fields[$name] = $field;
67
68 11
        return $this;
69
    }
70
71
    /**
72
     * @param $name
73
     *
74
     * @return Field
75
     */
76 8
    public function getField($name)
77
    {
78 8
        return $this->hasField($name) ? $this->fields[$name] : null;
79
    }
80
81
    /**
82
     * @param $name
83
     *
84
     * @return bool
85
     */
86 8
    public function hasField($name)
87
    {
88 8
        return array_key_exists($name, $this->fields);
89
    }
90
91
    /**
92
     * @return Field[]
93
     */
94 5
    public function getFields()
95
    {
96 5
        return $this->fields;
97
    }
98
}