Completed
Push — master ( 389cae...29b40e )
by Richard
06:27
created

ContainText::compile()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 39
Code Lines 32

Duplication

Lines 32
Ratio 82.05 %

Code Coverage

Tests 31
CRAP Score 4.0109

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 32
loc 39
ccs 31
cts 34
cp 0.9118
rs 8.5806
nc 3
cc 4
eloc 32
nop 2
crap 4.0109
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
16
/**
17
 * This checks wheather there is some text in the definition of an entity.
18
 */
19
class ContainText extends Property {
20
    /**
21
     * @inheritdoc
22
     */
23 287
    public function name() {
24 287
        return "contain_text";
25
    } 
26
27
    /**
28
     * @inheritdoc
29
     */
30 49
    public function check_arguments(array $arguments) {
31 49
        if (count($arguments) != 1) {
32
            throw new \InvalidArgumentException(
33
                "One argument is required when using a contain text.");
34
        }
35 49
        $regexp = $arguments[0];
36 49 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...
37 1
            throw new \InvalidArgumentException(
38 1
                "Invalid regexp '$regexp' when using contain text.");
39
        }
40 48
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45 11
    public function compile(Query $query, Rule $rule) {
46 11
        $builder = $query->builder();
47 11
        $mode = $rule->mode();
48 11
        $checked_on = $rule->checked_on();
49 11
        $regexp = $rule->argument(0);
50 11 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...
51
            return $builder
52 7
                ->select
53 7
                    ( "id as entity_id"
54 7
                    , "file"
55 7
                    , "start_line as line"
56 7
                    , "source"
57 7
                    )
58 7
                ->from($query->entity_table())
59
                ->where
60 7
                    ( $query->compile_var($query->entity_table(), $checked_on)
61 7
                    , "source REGEXP ?"
62 7
                    )
63 7
                ->setParameter(0, $regexp)
64 7
                ->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
                    , "start_line as line"
72 4
                    , "source"
73 4
                    )
74 4
                ->from($query->entity_table())
75
                ->where
76 4
                    ( $query->compile_var($query->entity_table(), $checked_on)
77 4
                    , "source NOT REGEXP ?"
78 4
                    )
79 4
                ->setParameter(0, $regexp)
80 4
                ->execute();
81
        }
82
        throw new \LogicException("Unknown rule mode: '$mode'");
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88
    public function pprint(Rule $rule) {
89
        return $this->printable_name().' "'.$rule->argument(0).'"';
90
    }
91
}
92