Completed
Push — master ( 322d21...4a7f1b )
by Richard
05:35
created

ContainText   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 97
Duplicated Lines 4.12 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 89.06%

Importance

Changes 8
Bugs 0 Features 1
Metric Value
c 8
b 0
f 1
dl 4
loc 97
wmc 10
lcom 0
cbo 5
ccs 57
cts 64
cp 0.8906
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A check_arguments() 4 11 4
B compile() 0 63 4
A pprint() 0 3 1

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 Richard Klees <[email protected]>
6
 *
7
 * This software is licensed under The MIT License. You should have received
8
 * a copy of the licence along with the code.
9
 */
10
11
namespace Lechimp\Dicto\Rules;
12
13
use Lechimp\Dicto\Definition as Def;
14
use Lechimp\Dicto\Analysis\Query;
15
use Lechimp\Dicto\Analysis\Violation;
16
17
/**
18
 * This checks wheather there is some text in the definition of an entity.
19
 */
20
class ContainText extends Property {
21
    /**
22
     * @inheritdoc
23
     */
24 285
    public function name() {
25 285
        return "contain_text";
26
    } 
27
28
    /**
29
     * @inheritdoc
30
     */
31 51
    public function check_arguments(array $arguments) {
32 51
        if (count($arguments) != 1) {
33
            throw new \InvalidArgumentException(
34
                "One argument is required when using a contain text.");
35
        }
36 51
        $regexp = $arguments[0];
37 51 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...
38 1
            throw new \InvalidArgumentException(
39 1
                "Invalid regexp '$regexp' when using contain text.");
40
        }
41 50
    }
42
43
    /**
44
     * @inheritdoc
45
     */
46 13
    public function compile(Query $query, Rule $rule) {
47 13
        $builder = $query->builder();
48 13
        $b = $builder->expr();
49 13
        $mode = $rule->mode();
50 13
        $checked_on = $rule->checked_on();
51 13
        $regexp = $rule->argument(0);
52
        if ($mode == Rule::MODE_CANNOT || $mode == Rule::MODE_ONLY_CAN) {
53 9
            return $builder
54 9
                ->select
55 9
                    ( "e.id as entity_id"
56 9
                    , "e.file"
57 9
                    , "src.source"
58 9
                    , "src.line"
59
                    )
60 9
                ->from($query->entity_table(), "e")
61 9
                ->innerJoin
62 9
                    ( "e", $query->source_file_table(), "src"
63 9
                    , $b->andX
64 9
                        ( $b->gte("src.line", "e.start_line")
65
                        , $b->lte("src.line", "e.end_line")
66 4
                        , $b->eq("src.name", "e.file")
67
                        , "src.source REGEXP ?"
68 4
                        )
69 4
                    )
70 4
                ->where
71 4
                    ( $query->compile_var("e", $checked_on)
72 4
                    )
73 4
                ->setParameter(0, $regexp)
74
                ->execute();
75 4
        }
76 4
        if ($mode == Rule::MODE_MUST) {
77 4
            return $builder
78 4
                ->select
79 4
                    ( "e.id as entity_id"
80
                    , "e.file"
81
                    , "e.start_line as line"
82
                    , "src.source"
83
                    )
84
                ->from($query->entity_table(), "e")
85
                ->innerJoin
86
                    ( "e", $query->source_file_table(), "src"
87 1
                    , $b->andX
88 1
                        ( $b->eq("src.name", "e.file")
89 1
                        , $b->eq("src.line", "e.start_line")
90 1
                        )
91 1
                    )
92 1
                ->leftJoin
93 1
                    ( "e", $query->source_file_table(), "match"
94 1
                    , $b->andX
95 1
                        ( $b->eq("match.name", "e.file")
96 1
                        , $b->eq("match.line", "e.start_line")
97
                        , "match.source REGEXP ?"
98 1
                        )
99 1
                    )
100
                ->where
101
                    ( $query->compile_var("e", $checked_on)
102
                    , "match.line IS NULL"
103
                    )
104 1
                ->setParameter(0, $regexp)
105 1
                ->execute();
106 1
        }
107 1
        throw new \LogicException("Unknown rule mode: '$mode'");
108 1
    }
109
110
    /**
111
     * @inheritdoc
112
     */
113
    public function pprint(Rule $rule) {
114 2
        return $this->printable_name().' "'.$rule->argument(0).'"';
115 2
    }
116
}
117