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

ContainText   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 113
Duplicated Lines 8.85 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 89.19%

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 12
c 7
b 0
f 0
lcom 0
cbo 7
dl 10
loc 113
ccs 66
cts 74
cp 0.8919
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A fetch_arguments() 0 4 1
A arguments_are_valid() 10 10 4
B compile() 0 63 4
A pprint() 0 3 1
A register_listeners() 0 2 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 license along with the code.
9
 */
10
11
namespace Lechimp\Dicto\Rules;
12
13
use Lechimp\Dicto\Analysis\Query;
14
use Lechimp\Dicto\Analysis\Violation;
15
use Lechimp\Dicto\Definition\ArgumentParser;
16
use Lechimp\Dicto\Indexer\ListenerRegistry;
17
18
/**
19
 * This checks wheather there is some text in the definition of an entity.
20
 */
21
class ContainText extends Schema {
22
    /**
23
     * @inheritdoc
24
     */
25 27
    public function name() {
26 27
        return "contain text";
27
    } 
28
29
    /**
30
     * @inheritdoc
31
     */
32 5
    public function fetch_arguments(ArgumentParser $parser) {
33 5
        $regexp = $parser->fetch_string();
34 5
        return array($regexp);
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40 19 View Code Duplication
    public function arguments_are_valid(array &$arguments) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
41 19
        if (count($arguments) != 1) {
42
            return false;
43
        }
44 19
        $regexp = $arguments[0];
45 19
        if (!is_string($regexp) || @preg_match("%$regexp%", "") === false) {
46
            return false;
47
        }
48 19
        return true;
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54 13
    public function compile(Query $query, Rule $rule) {
55 13
        $builder = $query->builder();
56 13
        $b = $builder->expr();
57 13
        $mode = $rule->mode();
58 13
        $checked_on = $rule->checked_on();
59 13
        $regexp = $rule->argument(0);
60 13
        if ($mode == Rule::MODE_CANNOT || $mode == Rule::MODE_ONLY_CAN) {
61
            return $builder
0 ignored issues
show
Bug Compatibility introduced by
The expression $builder->select('e.id a...0, $regexp)->execute(); of type Doctrine\DBAL\Driver\Statement|integer adds the type integer to the return on line 61 which is incompatible with the return type declared by the abstract method Lechimp\Dicto\Rules\Schema::compile of type Doctrine\DBAL\Driver\Statement.
Loading history...
62 9
                ->select
63 9
                    ( "e.id as entity_id"
64 9
                    , "e.file"
65 9
                    , "src.source"
66 9
                    , "src.line"
67 9
                    )
68 9
                ->from($query->entity_table(), "e")
69
                ->innerJoin
70 9
                    ( "e", $query->source_file_table(), "src"
71 9
                    , $b->andX
72 9
                        ( $b->gte("src.line", "e.start_line")
73 9
                        , $b->lte("src.line", "e.end_line")
74 9
                        , $b->eq("src.name", "e.file")
75 9
                        , "src.source REGEXP ?"
76 9
                        )
77 9
                    )
78
                ->where
79 9
                    ( $checked_on->compile($b, "e")
80 9
                    )
81 9
                ->setParameter(0, $regexp)
82 9
                ->execute();
83
        }
84 4
        if ($mode == Rule::MODE_MUST) {
85
            return $builder
0 ignored issues
show
Bug Compatibility introduced by
The expression $builder->select('e.id a...0, $regexp)->execute(); of type Doctrine\DBAL\Driver\Statement|integer adds the type integer to the return on line 85 which is incompatible with the return type declared by the abstract method Lechimp\Dicto\Rules\Schema::compile of type Doctrine\DBAL\Driver\Statement.
Loading history...
86 4
                ->select
87 4
                    ( "e.id as entity_id"
88 4
                    , "e.file"
89 4
                    , "e.start_line as line"
90 4
                    , "src.source"
91 4
                    )
92 4
                ->from($query->entity_table(), "e")
93
                ->innerJoin
94 4
                    ( "e", $query->source_file_table(), "src"
95 4
                    , $b->andX
96 4
                        ( $b->eq("src.name", "e.file")
97 4
                        , $b->eq("src.line", "e.start_line")
98 4
                        )
99 4
                    )
100
                ->leftJoin
101 4
                    ( "e", $query->source_file_table(), "match"
102 4
                    , $b->andX
103 4
                        ( $b->eq("match.name", "e.file")
104 4
                        , $b->eq("match.line", "e.start_line")
105 4
                        , "match.source REGEXP ?"
106 4
                        )
107 4
                    )
108
                ->where
109 4
                    ( $checked_on->compile($b, "e")
110 4
                    , "match.line IS NULL"
111 4
                    )
112 4
                ->setParameter(0, $regexp)
113 4
                ->execute();
114
        }
115
        throw new \LogicException("Unknown rule mode: '$mode'");
116
    }
117
118
    /**
119
     * @inheritdoc
120
     */
121 2
    public function pprint(Rule $rule) {
122 2
        return $this->name().' "'.$rule->argument(0).'"';
123
    }
124
125
    /**
126
     * No listeners required for contains text. 
127
     *
128
     * @inheritdoc
129
     */
130 25
    public function register_listeners(ListenerRegistry $registry) {
131 25
    }
132
133
}
134