Compiler   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 82.35%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 50
ccs 14
cts 17
cp 0.8235
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 3
A parse() 0 7 2
A compile() 0 14 3
1
<?php
2
/**
3
 * @author Gerard van Helden <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
7
namespace Zicht\Tool\Script;
8
9
use Zicht\Tool\Util;
10
use Zicht\Tool\Script\Node\Node;
11
12
/**
13
 * Wraps the tokenization, parsing and compiling into one convenience class.
14
 */
15
class Compiler
16
{
17
    /**
18
     * Constructor.
19
     *
20
     * @param ParserInterface $parser
21
     * @param TokenizerInterface $tokenizer
22
     */
23 5
    public function __construct($parser = null, $tokenizer = null)
24
    {
25 5
        $this->tokenizer = (null === $tokenizer ? new Tokenizer() : $tokenizer);
0 ignored issues
show
Bug introduced by
The property tokenizer 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...
26 5
        $this->parser = (null === $parser ? new Parser() : $parser);
0 ignored issues
show
Bug introduced by
The property parser 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...
27 5
    }
28
29
30
    /**
31
     * Parses the raw string input and returns the resulting root node.
32
     *
33
     * @param string $input
34
     * @return Node
35
     */
36 4
    public function parse($input)
37
    {
38 4
        if (strlen($input) == 0) {
39
            return null;
40
        }
41 4
        return $this->parser->parse(new TokenStream($this->tokenizer->getTokens($input)));
42
    }
43
44
    /**
45
     * Compile an input string to PHP code
46
     *
47
     * @param string $input
48
     * @return string
49
     */
50 5
    public function compile($input)
51
    {
52 5
        if (strlen($input) == 0) {
53 1
            return null;
54
        }
55
        try {
56 4
            $buffer = new Buffer();
57 4
            $this->parse($input)->compile($buffer);
58 4
            $code = $buffer->getResult();
59 4
            return $code;
60
        } catch (\UnexpectedValueException $e) {
61
            throw new \UnexpectedValueException('Error while compiling input: ' . Util::toPhp($input), 0, $e);
62
        }
63
    }
64
}
65