Completed
Push — master ( a94ac3...322d21 )
by Richard
05:34
created

ContainText   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 98
Duplicated Lines 34.69 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 89.06%

Importance

Changes 7
Bugs 0 Features 1
Metric Value
wmc 14
c 7
b 0
f 1
lcom 0
cbo 5
dl 34
loc 98
ccs 57
cts 64
cp 0.8906
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A check_arguments() 4 11 4
B compile() 30 37 4
B to_violation() 0 23 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
        $mode = $rule->mode();
49 13
        $checked_on = $rule->checked_on();
50 13
        $regexp = $rule->argument(0);
51 13 View Code Duplication
        if ($mode == Rule::MODE_CANNOT || $mode == Rule::MODE_ONLY_CAN) {
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...
52
            return $builder
53 9
                ->select
54 9
                    ( "id as entity_id"
55 9
                    , "file"
56 9
                    , "source"
57 9
                    )
58 9
                ->from($query->entity_table())
59
                ->where
60 9
                    ( $query->compile_var($query->entity_table(), $checked_on)
61 9
                    , "source REGEXP ?"
62 9
                    )
63 9
                ->setParameter(0, $regexp)
64 9
                ->execute();
65
        }
66 4 View Code Duplication
        if ($mode == Rule::MODE_MUST) {
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...
67
            return $builder
68 4
                ->select
69 4
                    ( "id as entity_id"
70 4
                    , "file"
71 4
                    , "source"
72 4
                    )
73 4
                ->from($query->entity_table())
74
                ->where
75 4
                    ( $query->compile_var($query->entity_table(), $checked_on)
76 4
                    , "source NOT REGEXP ?"
77 4
                    )
78 4
                ->setParameter(0, $regexp)
79 4
                ->execute();
80
        }
81
        throw new \LogicException("Unknown rule mode: '$mode'");
82
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87 1
    public function to_violation(Rule $rule, array $row) {
88 1
        $line_no = 0;
89 1
        $line = null;
90 1
        $lines = explode("\n", $row["source"]);
91 1
        $pattern = $rule->argument(0);
92 1
        foreach ($lines as $l) {
93 1
            $line_no++;
94 1
            if (preg_match("%$pattern%", $l) > 0) {
95 1
                $line = $l;
96 1
                break;
97
            }
98 1
        }
99 1
        if ($line === null) {
100
            throw new \LogicException(
101
                "Found '$pattern' with SQL query but not in postprocessing...");
102
        }
103
        return new Violation
104 1
            ( $rule
105 1
            , $row["file"]
106 1
            , $line_no
107 1
            , $line
108 1
            );
109
    }
110
111
    /**
112
     * @inheritdoc
113
     */
114 2
    public function pprint(Rule $rule) {
115 2
        return $this->printable_name().' "'.$rule->argument(0).'"';
116
    }
117
}
118