Completed
Pull Request — master (#19)
by Christoffer
02:15
created

AbstractBuilder::createLocation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\GraphQL\Language\AST\Node\Builder;
4
5
use Digia\GraphQL\Language\AST\Node\Builder\Contract\BuilderInterface;
6
use Digia\GraphQL\Language\AST\Node\Builder\Contract\DirectorInterface;
7
use Digia\GraphQL\Language\Location;
8
9
abstract class AbstractBuilder implements BuilderInterface
10
{
11
12
    /**
13
     * @var DirectorInterface
14
     */
15
    protected $director;
16
17
    /**
18
     * @param DirectorInterface $director
19
     * @return $this
20
     */
21
    public function setDirector(DirectorInterface $director)
22
    {
23
        $this->director = $director;
24
        return $this;
25
    }
26
27
    /**
28
     * Creates a location object.
29
     *
30
     * @param array $ast
31
     * @return Location
32
     */
33
    protected function createLocation(array $ast): Location
34
    {
35
        return isset($ast['loc']['start'], $ast['loc']['end'])
0 ignored issues
show
Bug Best Practice introduced by
The expression return IssetNode ? new D...t['loc']['end']) : null could return the type null which is incompatible with the type-hinted return Digia\GraphQL\Language\Location. Consider adding an additional type-check to rule them out.
Loading history...
36
            ? new Location($ast['loc']['start'], $ast['loc']['end'])
37
            : null;
38
    }
39
40
    /**
41
     * Returns the value of a single property in the given AST.
42
     *
43
     * @param array  $ast
44
     * @param string $propertyName
45
     * @param null   $defaultValue
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $defaultValue is correct as it would always require null to be passed?
Loading history...
46
     * @return mixed|null
47
     */
48
    protected function get(array $ast, string $propertyName, $defaultValue = null)
49
    {
50
        return $ast[$propertyName] ?? $defaultValue;
51
    }
52
53
    /**
54
     * Builds a single item from the given AST.
55
     *
56
     * @param array  $ast
57
     * @param string $propertyName
58
     * @return mixed|null
59
     */
60
    protected function buildOne(array $ast, string $propertyName)
61
    {
62
        return isset($ast[$propertyName]) ? $this->director->build($ast[$propertyName]) : null;
63
    }
64
65
    /**
66
     * Builds many items from the given AST.
67
     *
68
     * @param array  $ast
69
     * @param string $propertyName
70
     * @return array
71
     */
72
    protected function buildMany(array $ast, string $propertyName): array
73
    {
74
        $array = [];
75
76
        if (isset($ast[$propertyName])) {
77
            foreach ($ast[$propertyName] as $subAst) {
78
                $array[] = $this->director->build($subAst);
79
            }
80
        }
81
82
        return $array;
83
    }
84
}
85