Dumper::getAst()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6.0073

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
ccs 16
cts 17
cp 0.9412
rs 8.5125
cc 6
eloc 14
nc 4
nop 2
crap 6.0073
1
<?php
2
/**
3
 * For licensing information, please see the LICENSE file accompanied with this file.
4
 *
5
 * @author Gerard van Helden <[email protected]>
6
 * @copyright 2012 Gerard van Helden <http://melp.nl>
7
 */
8
namespace Zicht\Tool\Script;
9
10
use Zicht\Tool\Util;
11
12
/**
13
 * Dumps a compiled node AST as an array
14
 */
15
class Dumper
16
{
17
    /**
18
     * Returns the AST for a specified node as an array representation
19
     *
20
     * @param Node\Node $b
21
     * @param string $path
22
     * @return array
23
     */
24 13
    public function getAst(Node\Node $b, $path = '')
25
    {
26
        $ret = array(
27 13
            'type' => str_replace('Zicht\Tool\Script\Node\\', '', get_class($b))
28 13
        );
29 13
        if ($b instanceof Node\Branch) {
30 12
            if (count($b->nodes)) {
31 11
                $ret['nodes'] = array();
32
33 11
                foreach ($b->nodes as $n) {
34 11
                    if (null === $n) {
35 2
                        $ret['nodes'][] = $n;
36 2
                    } else {
37 11
                        if (!$n instanceof Node\Node) {
38
                            throw new \InvalidArgumentException("Invalid child node in " . Util::toPhp($path));
39
                        }
40 11
                        $ret['nodes'][] = $this->getAst($n);
41
                    }
42 11
                }
43 11
            }
44 12
        }
45
46 13
        return $ret;
47
    }
48
}
49