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

WithName   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 4.76 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95.83%

Importance

Changes 7
Bugs 0 Features 4
Metric Value
wmc 8
lcom 1
cbo 2
dl 3
loc 63
ccs 23
cts 24
cp 0.9583
rs 10
c 7
b 0
f 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A regexp() 0 3 1
A variable() 0 3 1
A meaning() 0 4 1
A compile() 0 17 2
A __construct() 3 8 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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