Completed
Push — master ( 31785d...5192f1 )
by Richard
06:45
created

WithProperty   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 73
ccs 32
cts 34
cp 0.9412
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A variable() 0 3 1
A meaning() 0 3 1
B argument_list() 0 21 5
A compile() 0 9 2
1
<?php
2
/******************************************************************************
3
 * An implementation of dicto (scg.unibe.ch/dicto) in and for PHP.
4
 * 
5
 * Copyright (c) 2016, 2015 Richard Klees <[email protected]>
6
 *
7
 * This software is licensed under The MIT License. You should have received 
8
 * a copy of the license along with the code.
9
 */
10
11
namespace Lechimp\Dicto\Variables;
12
13
use Lechimp\Dicto\Graph\Node;
14
15
/**
16
 * A variable with a certain property.
17
 */
18
class WithProperty extends Variable {
19
    /**
20
     * @var Variable
21
     */
22
    private $other;
23
24
    /**
25
     * @var Property
26
     */
27
    private $property;
28
29
    /**
30
     * @var array
31
     */
32
    private $arguments;
33
34 29
    public function __construct(Variable $other, Property $property, array $arguments) {
35 29
        parent::__construct();
36 29
        assert('$property->arguments_are_valid($arguments)');
37 29
        $this->other = $other;
38 29
        $this->property = $property;
39 29
        $this->arguments = $arguments;
40 29
    }
41
42
    /**
43
     * @return  Variable
44
     */
45 14
    public function variable() {
46 14
        return $this->other;
47
    }
48
49
    /**
50
     * @inheritdocs
51
     */
52 14
    public function meaning() {
53 14
        return $this->variable()->meaning()." ".$this->property->parse_as().": ".$this->argument_list();
54
    }
55
56 14
    protected function argument_list() {
57 14
        $args = array();
58 14
        foreach ($this->arguments as $argument) {
59 14
            if (is_string($argument)) {
60 12
                $args[] = "\"$argument\"";
61 12
            }
62 2
            else if ($argument instanceof Variable) {
63 2
                $name = $argument->name();
64 2
                if ($name !== null) {
65 2
                    $args[] = $name;
66 2
                }
67
                else {
68
                    $args[] = "(".$argument->meaning().")";
69
                }
70 2
            }
71
            else {
72
                throw new \LogicException("Unknown arg type: ".gettype($argument));
73
            }
74 14
        }
75 14
        return implode(", ", $args);
76
    }
77
78
    /**
79
     * @inheritdocs
80
     */
81 23
    public function compile() {
82 23
        $left_condition = $this->other->compile();
83 23
        $property_condition = $this->property->compile($this->arguments);
84
85 23
        return function(Node $n) use ($left_condition, $property_condition) {
86 23
            return $left_condition($n)
87 23
                && $property_condition($n);
88 23
        };
89
    }
90
}
91