Completed
Push — master ( cc2765...340ff8 )
by Richard
08:19
created

WithName::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 3
Ratio 37.5 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 3
loc 8
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 2
b 0
f 1
cc 3
eloc 6
nc 2
nop 2
crap 3.0261
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
 * Another variable that has a certain name.
17
 */
18
class WithName extends Variable {
19
    /**
20
     * @var string
21
     */
22
    private $regexp;
23
24
    /**
25
     * @var Variable
26
     */
27
    private $other;
28
29 36
    public function __construct($regexp, Variable $other) {
30 36
        parent::__construct();
31 36 View Code Duplication
        if (!is_string($regexp) || @preg_match("%$regexp%", "") === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
            throw new \InvalidArgumentException("Invalid regexp: '%$regexp%'");
33
        }
34 36
        $this->regexp = $regexp;
35 36
        $this->other = $other;
36 36
    }
37
38
    /**
39
     * @return  string
40
     */
41 38
    public function regexp() {
42 38
        return $this->regexp;
43
    }
44
45
    /**
46
     * @return  Variable
47
     */
48 38
    public function variable() {
49 38
        return $this->other;
50
    }
51
52
    /**
53
     * @inheritdocs
54
     */
55 9
    public function meaning() {
56 9
        $re = $this->regexp();
57 9
        return $this->variable()->meaning()." with name \"$re\""; 
58
    }
59
60
    /**
61
     * @inheritdocs
62
     */
63 29
    public function compile(ExpressionBuilder $builder, $table_name, $negate = false) {
64
        // normal case : left_condition AND regexp matches
65 29
        if (!$negate) {
66 11
            return $builder->andX
67 11
                ( $this->variable()->compile($builder, $table_name)
68 11
                , "$table_name.name REGEXP ".$builder->literal('^'.$this->regexp().'$')
69 11
                );
70
        }
71
        // negated case: not (left_condition_left and regexp matches)
72
        //             = not left_condition and not regexp matches
73
        else {
74 18
            return $builder->orX
75 18
                ( $this->variable()->compile($builder, $table_name, true)
76 18
                , "$table_name.name NOT REGEXP ".$builder->literal('^'.$this->regexp().'$')
77 18
                );
78
        }
79
    }
80
}
81