Completed
Push — master ( 9971b9...3d6cb8 )
by Richard
06:14
created

ContainText::compile()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 79
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 64
CRAP Score 4.0394

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 79
ccs 64
cts 74
cp 0.8649
rs 8.5646
nc 3
cc 4
eloc 62
nop 2
crap 4.0394

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 some entity.
20
 *
21
 * TODO: Test if ContainText finds text in files.
22
 */
23
class ContainText extends Schema {
24
    /**
25
     * @inheritdoc
26
     */
27 27
    public function name() {
28 27
        return "contain text";
29
    } 
30
31
    /**
32
     * @inheritdoc
33
     */
34 5
    public function fetch_arguments(ArgumentParser $parser) {
35 5
        $regexp = $parser->fetch_string();
36 5
        return array($regexp);
37
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42 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...
43 19
        if (count($arguments) != 1) {
44
            return false;
45
        }
46 19
        $regexp = $arguments[0];
47 19
        if (!is_string($regexp) || @preg_match("%$regexp%", "") === false) {
48
            return false;
49
        }
50 19
        return true;
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56 13
    public function compile(Query $query, Rule $rule) {
57 13
        $builder = $query->builder();
58 13
        $b = $builder->expr();
59 13
        $mode = $rule->mode();
60 13
        $checked_on = $rule->checked_on();
61 13
        $regexp = $rule->argument(0);
62 13
        if ($mode == Rule::MODE_CANNOT || $mode == Rule::MODE_ONLY_CAN) {
63
            return $builder
0 ignored issues
show
Bug Compatibility introduced by
The expression $builder->select('d.name...0, $regexp)->execute(); of type Doctrine\DBAL\Driver\Statement|integer adds the type integer to the return on line 63 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...
64 9
                ->select
65 9
                    ( "d.name"
66 9
                    , "f.path as file"
67 9
                    , "src.line"
68 9
                    , "src.source"
69 9
                    )
70 9
                ->from($query->definition_table(), "d")
71
                ->join
72 9
                    ( "d", $query->file_table(), "f"
73 9
                    , $b->eq("d.file", "f.id")
74 9
                    )
75
                ->join
76 9
                    ( "d", $query->name_table(), "n"
77 9
                    , $b->eq("d.name", "n.id")
78 9
                    )
79
                ->join
80 9
                    ( "d", $query->source_table(), "src"
81 9
                    , $b->andX
82 9
                        ( $b->gte("src.line", "d.start_line")
83 9
                        , $b->lte("src.line", "d.end_line")
84 9
                        , $b->eq("src.file", "d.file")
85 9
                        , "src.source REGEXP ?"
86 9
                        )
87 9
                    )
88
                ->where
89 9
                    ( $checked_on->compile($b, "n")
90 9
                    )
91 9
                ->setParameter(0, $regexp)
92 9
                ->execute();
93
        }
94 4
        if ($mode == Rule::MODE_MUST) {
95
            return $builder
0 ignored issues
show
Bug Compatibility introduced by
The expression $builder->select('d.name...0, $regexp)->execute(); of type Doctrine\DBAL\Driver\Statement|integer adds the type integer to the return on line 95 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...
96 4
                ->select
97 4
                    ( "d.name"
98 4
                    , "f.path as file"
99 4
                    , "d.start_line as line"
100 4
                    , "src.source"
101 4
                    )
102 4
                ->from($query->definition_table(), "d")
103
                ->join
104 4
                    ( "d", $query->file_table(), "f"
105 4
                    , $b->eq("d.file", "f.id")
106 4
                    )
107
                ->join
108 4
                    ( "d", $query->name_table(), "n"
109 4
                    , $b->eq("d.name", "n.id")
110 4
                    )
111
                ->join
112 4
                    ( "d", $query->source_table(), "src"
113 4
                    , $b->andX
114 4
                        ( $b->eq("src.file", "d.file")
115 4
                        , $b->eq("src.line", "d.start_line")
116 4
                        )
117 4
                    )
118
                ->leftJoin
119 4
                    ( "d", $query->source_table(), "match"
120 4
                    , $b->andX
121 4
                        ( $b->eq("match.file", "d.file")
122 4
                        , $b->eq("match.line", "d.start_line")
123 4
                        , "match.source REGEXP ?"
124 4
                        )
125 4
                    )
126
                ->where
127 4
                    ( $checked_on->compile($b, "n")
128 4
                    , "match.line IS NULL"
129 4
                    )
130 4
                ->setParameter(0, $regexp)
131 4
                ->execute();
132
        }
133
        throw new \LogicException("Unknown rule mode: '$mode'");
134
    }
135
136
    /**
137
     * @inheritdoc
138
     */
139 2
    public function pprint(Rule $rule) {
140 2
        return $this->name().' "'.$rule->argument(0).'"';
141
    }
142
143
    /**
144
     * No listeners required for contains text. 
145
     *
146
     * @inheritdoc
147
     */
148 10
    public function register_listeners(ListenerRegistry $registry) {
149 10
    }
150
151
}
152