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

ContainText::compile()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 63
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 52
CRAP Score 4.0176

Importance

Changes 4
Bugs 0 Features 0
Metric Value
dl 0
loc 63
ccs 52
cts 58
cp 0.8966
rs 8.8945
c 4
b 0
f 0
cc 4
eloc 50
nc 3
nop 2
crap 4.0176

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\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 27
    public function name() {
25 27
        return "contain text";
26
    } 
27
28
    /**
29
     * @inheritdoc
30
     */
31 5
    public function fetch_arguments(ArgumentParser $parser) {
32 5
        $regexp = $parser->fetch_string();
33 5
        return array($regexp);
34
    }
35
36
    /**
37
     * @inheritdoc
38
     */
39 19
    public function arguments_are_valid(array &$arguments) {
40 19
        if (count($arguments) != 1) {
41
            return false;
42
        }
43 19
        $regexp = $arguments[0];
44 19 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...
45
            return false;
46
        }
47 19
        return true;
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53 13
    public function compile(Query $query, Rule $rule) {
54 13
        $builder = $query->builder();
55 13
        $b = $builder->expr();
56 13
        $mode = $rule->mode();
57 13
        $checked_on = $rule->checked_on();
58 13
        $regexp = $rule->argument(0);
59 13
        if ($mode == Rule::MODE_CANNOT || $mode == Rule::MODE_ONLY_CAN) {
60
            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 60 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...
61 9
                ->select
62 9
                    ( "e.id as entity_id"
63 9
                    , "e.file"
64 9
                    , "src.source"
65 9
                    , "src.line"
66 9
                    )
67 9
                ->from($query->entity_table(), "e")
68
                ->innerJoin
69 9
                    ( "e", $query->source_file_table(), "src"
70 9
                    , $b->andX
71 9
                        ( $b->gte("src.line", "e.start_line")
72 9
                        , $b->lte("src.line", "e.end_line")
73 9
                        , $b->eq("src.name", "e.file")
74 9
                        , "src.source REGEXP ?"
75 9
                        )
76 9
                    )
77
                ->where
78 9
                    ( $checked_on->compile($b, "e")
79 9
                    )
80 9
                ->setParameter(0, $regexp)
81 9
                ->execute();
82
        }
83 4
        if ($mode == Rule::MODE_MUST) {
84
            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 84 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...
85 4
                ->select
86 4
                    ( "e.id as entity_id"
87 4
                    , "e.file"
88 4
                    , "e.start_line as line"
89 4
                    , "src.source"
90 4
                    )
91 4
                ->from($query->entity_table(), "e")
92
                ->innerJoin
93 4
                    ( "e", $query->source_file_table(), "src"
94 4
                    , $b->andX
95 4
                        ( $b->eq("src.name", "e.file")
96 4
                        , $b->eq("src.line", "e.start_line")
97 4
                        )
98 4
                    )
99
                ->leftJoin
100 4
                    ( "e", $query->source_file_table(), "match"
101 4
                    , $b->andX
102 4
                        ( $b->eq("match.name", "e.file")
103 4
                        , $b->eq("match.line", "e.start_line")
104 4
                        , "match.source REGEXP ?"
105 4
                        )
106 4
                    )
107
                ->where
108 4
                    ( $checked_on->compile($b, "e")
109 4
                    , "match.line IS NULL"
110 4
                    )
111 4
                ->setParameter(0, $regexp)
112 4
                ->execute();
113
        }
114
        throw new \LogicException("Unknown rule mode: '$mode'");
115
    }
116
117
    /**
118
     * @inheritdoc
119
     */
120 2
    public function pprint(Rule $rule) {
121 2
        return $this->name().' "'.$rule->argument(0).'"';
122
    }
123
}
124