Passed
Pull Request — master (#586)
by Šimon
12:52
created

TestUtils::nodeToArray()   B

Complexity

Conditions 10
Paths 6

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 29
rs 7.6666
c 0
b 0
f 0
cc 10
nc 6
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Tests\Language;
6
7
use GraphQL\Language\AST\Location;
8
use GraphQL\Language\AST\Node;
9
use GraphQL\Language\AST\NodeList;
10
use function get_object_vars;
11
use function is_array;
12
use function is_scalar;
13
14
class TestUtils
15
{
16
    /**
17
     * @return mixed[]
18
     */
19
    public static function nodeToArray(Node $node) : array
20
    {
21
        $result = [
22
            'kind' => $node->kind,
23
            'loc'  => self::locationToArray($node->loc),
24
        ];
25
26
        foreach (get_object_vars($node) as $prop => $propValue) {
27
            if (isset($result[$prop])) {
28
                continue;
29
            }
30
31
            if (is_array($propValue) || $propValue instanceof NodeList) {
32
                $tmp = [];
33
                foreach ($propValue as $tmp1) {
34
                    $tmp[] = $tmp1 instanceof Node ? self::nodeToArray($tmp1) : (array) $tmp1;
35
                }
36
            } elseif ($propValue instanceof Node) {
37
                $tmp = self::nodeToArray($propValue);
38
            } elseif (is_scalar($propValue) || $propValue === null) {
39
                $tmp = $propValue;
40
            } else {
41
                $tmp = null;
42
            }
43
44
            $result[$prop] = $tmp;
45
        }
46
47
        return $result;
48
    }
49
50
    /**
51
     * @return int[]
52
     */
53
    public static function locationToArray(Location $loc) : array
54
    {
55
        return [
56
            'start' => $loc->start,
57
            'end'   => $loc->end,
58
        ];
59
    }
60
61
    /**
62
     * @return int[]
63
     */
64
    public static function locArray(int $start, int $end) : array
65
    {
66
        return ['start' => $start, 'end' => $end];
67
    }
68
}
69