Completed
Push — master ( 5be5c0...7c6001 )
by Sven
13s
created

Math::applyToNode()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 13
cts 13
cp 1
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 12
nc 6
nop 1
crap 4
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 $parser;
22
    
23 6
    public function __construct($property)
24
    {
25 6
        $this->property = $property;
26
        
27 6
        $this->parser = Hoa\Compiler\Llk\Llk::load(
28 6
            new Hoa\File\Read('hoa://Library/Math/Arithmetic.pp')
29 6
        );
30 6
    }
31
    
32
    /**
33
     * Apply processing to a single node
34
     * 
35
     * @param Node $node
36
     */
37 6
    public function applyToNode(Node $node)
38
    {
39 6
        $expression = $node->getInstruction($this);
40 6
        $variables = $node->getResult() ?: [];
41
42 6
        $arithmetic = new Hoa\Math\Visitor\Arithmetic();
43
        
44 6
        foreach ($variables as $name => $value) {
45
            // Constants are upper case and variables lower case. We don't really care, so using a workaround.
46 2
            if (preg_match('/^[A-Z_][A-Z0-9_]*$/', $name)) {
47 1
                $arithmetic->addConstant($name, $value);
48 1
            } else {
49
                $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...
50
            }
51 6
        }
52
        
53 6
        $ast = $this->parser->parse($expression);
54 5
        $result = $arithmetic->visit($ast);
55
        
56 4
        $node->setResult($result);
57 4
    }
58
}
59