Completed
Push — master ( 7c6001...071064 )
by Arnold
05:03
created

Math::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
namespace LegalThings\DataEnricher\Processor;
4
5
use LegalThings\DataEnricher\Node;
6
use LegalThings\DataEnricher\Processor;
7
use Hoa;
8
9
/**
10
 * Description of Math
11
 *
12
 * @author arnold
13
 */
14
class Math implements Processor
15
{
16
    use Processor\Implementation;
17
18
    /**
19
     * @var Hoa\Compiler\Llk\Parser
20
     */
21
    protected static $parser;
22
    
23
    /**
24
     * Create a parser for arithmitic expressions
25
     * 
26
     * @return Hoa\Compiler\Llk\Parser
27
     */
28 6
    protected function getParser()
29
    {
30 6
        if (!isset(static::$parser)) {
31 1
            static::$parser = Hoa\Compiler\Llk\Llk::load(
32 1
                new Hoa\File\Read('hoa://Library/Math/Arithmetic.pp')
33
            );
34
        }
35
        
36 6
        return static::$parser;
37
    }
38
    
39
    /**
40
     * Apply processing to a single node
41
     * 
42
     * @param Node $node
43
     */
44 6
    public function applyToNode(Node $node)
45
    {
46 6
        $expression = $node->getInstruction($this);
47 6
        $ast = $this->getParser()->parse($expression);
48
        
49 5
        $variables = $node->getResult() ?: [];
50 5
        $arithmetic = new Hoa\Math\Visitor\Arithmetic();
51
        
52 5
        foreach ($variables as $name => $value) {
53
            // Constants are upper case and variables lower case. We don't really care, so using a workaround.
54 2
            if (preg_match('/^[A-Z_][A-Z0-9_]*$/', $name)) {
55 1
                $arithmetic->addConstant($name, $value);
56
            } else {
57
                $arithmetic->addVariable($name, function() use ($value) { return $value; });
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
Coding Style introduced by
Opening brace must be the last content on the line
Loading history...
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
58
            }
59
        }
60
        
61 5
        $result = $arithmetic->visit($ast);
62
        
63 4
        $node->setResult($result);
64 4
    }
65
}
66