Completed
Push — master ( 1e9ef5...3740bc )
by Richard
05:53 queued 29s
created

ContainText   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 133
Duplicated Lines 12.78 %

Coupling/Cohesion

Components 0
Dependencies 9

Test Coverage

Coverage 83.75%

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 9
dl 17
loc 133
ccs 67
cts 80
cp 0.8375
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A fetch_arguments() 0 4 1
A arguments_are_valid() 10 10 4
A compile() 7 54 4
A get_source_for() 0 13 1
A regexp_source_filter() 0 13 2
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\Index;
14
use Lechimp\Dicto\Analysis\Violation;
15
use Lechimp\Dicto\Definition\ArgumentParser;
16
use Lechimp\Dicto\Indexer\ListenerRegistry;
17
use Lechimp\Dicto\Graph\Node;
18
use Lechimp\Dicto\Graph\PredicateFactory;
19
use Lechimp\Dicto\Graph;
20
21
/**
22
 * This checks wheather there is some text in some entity.
23
 *
24
 * TODO: Test if ContainText finds text in files.
25
 */
26
class ContainText extends Schema {
27
    /**
28
     * @inheritdoc
29
     */
30 42
    public function name() {
31 42
        return "contain text";
32
    } 
33
34
    /**
35
     * @inheritdoc
36
     */
37 7
    public function fetch_arguments(ArgumentParser $parser) {
38 7
        $regexp = $parser->fetch_string();
39 7
        return array($regexp);
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45 16 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...
46 16
        if (count($arguments) != 1) {
47
            return false;
48
        }
49 16
        $regexp = $arguments[0];
50 16
        if (!is_string($regexp) || @preg_match("%$regexp%", "") === false) {
51
            return false;
52
        }
53 16
        return true;
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59 8
    public function compile(Index $index, Rule $rule) {
60 8
        $mode = $rule->mode();
61 8
        $query = $index->query();
62 8
        $predicate_factory = $query->predicate_factory();
63 8
        $filter = $rule->checked_on()->compile($predicate_factory);
64 8
        $regexp = $rule->argument(0);
65
        $regexp_filter = function(Graph\Relation $r) use ($regexp) {
0 ignored issues
show
Unused Code introduced by
$regexp_filter is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
66
            $start_line = $r->property("start_line");
67
            $end_line = $r->property("end_line");
68
            $source = implode
69
                ( "\n"
70
                , array_slice
71
                    ( $r->target()->property("source")
72
                    , $start_line - 1
73
                    , $end_line - $start_line
74
                    )
75
                );
76
            return preg_match("%$regexp%", $source) == 1;
77 8
        };
78
79 8
        if ($mode == Rule::MODE_CANNOT || $mode == Rule::MODE_ONLY_CAN) {
80
            return $query
81 5
                ->filter($filter)
0 ignored issues
show
Documentation introduced by
$filter is of type object<Lechimp\Dicto\Graph\PredicateFactory>, but the function expects a object<Lechimp\Dicto\Graph\Predicate>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82 5
                ->expand_relations(["defined in"])
83 5
                ->filter($this->regexp_source_filter($predicate_factory, $regexp, false))
84
                ->extract(function($e,&$r) use ($rule, $regexp) {
85 1
                    $matches = [];
86 1
                    $source = $this->get_source_for($e);
87 1
                    preg_match("%(.*)$regexp%", $source, $matches);
88
89 1
                    $file = $e->target();
90 1
                    $r["file"] = $file->property("path");
91 1
                    $start_line = $e->property("start_line");
92 2
                    $found_at_line = substr_count($matches[0], "\n") + 1;
93 1
                    $line = $start_line + $found_at_line;
94 1
                    $r["line"] = $line + 1;
95 1
                    $r["source"] = $file->property("source")[$line];
96 5
                });
97
        }
98 3
        if ($mode == Rule::MODE_MUST) {
99 3
            return $index->query()
100 3
                ->filter($filter)
0 ignored issues
show
Documentation introduced by
$filter is of type object<Lechimp\Dicto\Graph\PredicateFactory>, but the function expects a object<Lechimp\Dicto\Graph\Predicate>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
101 3
                ->expand_relations(["defined in"])
102 3
                ->filter($this->regexp_source_filter($predicate_factory, $regexp, true))
103 View Code Duplication
                ->extract(function($e,&$r) use ($rule) {
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...
104 1
                    $file = $e->target();
105 1
                    $r["file"] = $file->property("path");
106 1
                    $line = $e->property("start_line");
107 1
                    $r["line"] = $line;
108 1
                    $r["source"] = $file->property("source")[$line - 1];
109 3
                });
110
        }
111
        throw new \LogicException("Unknown rule mode: '$mode'");
112
    }
113
114
    // Helpers for compile
115
116 6
    protected function get_source_for(Graph\Relation $r) {
117 6
        assert('$r->type() == "defined in"');
118 5
        $start_line = $r->property("start_line");
119 5
        $end_line = $r->property("end_line");
120
        return implode
121 5
            ( "\n"
122 5
            , array_slice
123 5
                ( $r->target()->property("source")
124 5
                , $start_line - 1
125 5
                , $end_line - $start_line
126 5
                )
127 5
            );
128
    }
129
130 8
    protected function regexp_source_filter(PredicateFactory $f, $regexp, $negate) {
131 8
        assert('is_string($regexp)');
132 8
        assert('is_bool($negate)');
133 8
        return $f->_custom(function(Graph\Relation $r) use ($regexp, $negate) {
134 5
            $source = $this->get_source_for($r);
135 5
            if(!$negate) {
136 3
                return preg_match("%$regexp%", $source) == 1;
137
            }
138
            else {
139 2
                return preg_match("%$regexp%", $source) == 0;
140
            }
141 8
        });
142
    }
143
144
    /**
145
     * @inheritdoc
146
     */
147 8
    public function pprint(Rule $rule) {
148 8
        return $this->name().' "'.$rule->argument(0).'"';
149
    }
150
151
    /**
152
     * No listeners required for contains text. 
153
     *
154
     * @inheritdoc
155
     */
156 7
    public function register_listeners(ListenerRegistry $registry) {
157 7
    }
158
}
159