Completed
Push — master ( bd87f9...9971b9 )
by Richard
06:03
created

WithProperty   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 96.67%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 8
c 2
b 0
f 1
lcom 1
cbo 3
dl 0
loc 73
ccs 29
cts 30
cp 0.9667
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A variable() 0 3 1
A meaning() 0 4 1
A argument_list() 0 12 3
A compile() 0 17 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 Doctrine\DBAL\Query\Expression\ExpressionBuilder;
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 36
    public function __construct(Variable $other, Property $property, array $arguments) {
35 36
        parent::__construct();
36 36
        assert('$property->arguments_are_valid($arguments)');
37 36
        $this->other = $other;
38 36
        $this->property = $property;
39 36
        $this->arguments = $arguments;
40 36
    }
41
42
    /**
43
     * @return  Variable
44
     */
45 38
    public function variable() {
46 38
        return $this->other;
47
    }
48
49
    /**
50
     * @inheritdocs
51
     */
52 9
    public function meaning() {
53
        // TODO: maybe i should use the name here?
54 9
        return $this->variable()->meaning()." with ".$this->property->name().": ".$this->argument_list();
55
    }
56
57 9
    protected function argument_list() {
58 9
        $args = array();
59 9
        foreach ($this->arguments as $argument) {
60 9
            if (is_string($argument)) {
61 9
                $args[] = "\"$argument\"";
62 9
            }
63
            else {
64
                throw new \LogicException("Unknown arg type: ".gettype($argument));
65
            }
66 9
        }
67 9
        return implode(", ", $args);
68
    }
69
70
    /**
71
     * @inheritdocs
72
     */
73 29
    public function compile(ExpressionBuilder $builder, $table_name, $negate = false) {
74
        // normal case : left_condition AND property 
75 29
        if (!$negate) {
76 11
            return $builder->andX
77 11
                ( $this->variable()->compile($builder, $table_name)
78 11
                , $this->property->compile($this->arguments, $builder, $table_name, false)
79 11
                );
80
        }
81
        // negated case: not (left_condition_left and property)
82
        //             = not left_condition or not property
83
        else {
84 18
            return $builder->orX
85 18
                ( $this->variable()->compile($builder, $table_name, true)
86 18
                , $this->property->compile($this->arguments, $builder, $table_name, true)
87 18
                );
88
        }
89
    }
90
}
91