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

ContainText   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 73
Duplicated Lines 49.32 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 85.11%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 10
c 5
b 0
f 0
lcom 0
cbo 4
dl 36
loc 73
ccs 40
cts 47
cp 0.8511
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A check_arguments() 4 11 4
B compile() 32 39 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
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