Completed
Push — master ( 31785d...5192f1 )
by Richard
06:45
created

Relation   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 115
Duplicated Lines 6.96 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 94.52%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 10
dl 8
loc 115
ccs 69
cts 73
cp 0.9452
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A pprint() 0 3 1
A fetch_arguments() 0 4 1
A arguments_are_valid() 0 9 3
B compile() 8 57 7
A insert_relation_into() 0 15 3

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\Definition\ArgumentParser;
15
use Lechimp\Dicto\Indexer\Insert;
16
use Lechimp\Dicto\Indexer\Location;
17
use Lechimp\Dicto\Variables\Variable;
18
use Lechimp\Dicto\Graph\Node;
19
20
/**
21
 * This is a rule that checks a relation between two entities
22
 * in the code.
23
 */
24
abstract class Relation extends Schema {
25
    /**
26
     * @inheritdoc
27
     */
28 4
    public function fetch_arguments(ArgumentParser $parser) {
29 4
        $var = $parser->fetch_variable();
30 4
        return array($var);
31
    }
32
33
    /**
34
     * @inheritdoc
35
     */
36 36
    public function arguments_are_valid(array &$arguments) {
37 36
         if (count($arguments) != 1) {
38
            return false;
39
        }
40 36
        if (!($arguments[0] instanceof Variable)) {
41
            return false;
42
        }
43 36
        return true;
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49 28
    public function pprint(Rule $rule) {
50 28
        return $this->name()." ".$rule->argument(0)->name();
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56 16
    public function compile(Index $index, Rule $rule) {
57 16
        $mode = $rule->mode();
58 16
        $var_left = $rule->checked_on();
59 16
        $var_right = $rule->argument(0);
60 16
        if ($mode == Rule::MODE_CANNOT || $mode == Rule::MODE_ONLY_CAN) {
61 10
            $filter_left = $var_left->compile();
62 10
            $filter_right = $var_right->compile();
63 10
            return $index->query()
64 10
                ->filter($filter_left)
65 10
                ->expand_relations([$this->name()])
66 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...
67 5
                    $file = $e->property("file");
68 5
                    assert('$file->type() == "file"');
69 5
                    $r["file"] = $file->property("path");
70 5
                    $line = $e->property("line");
71 5
                    $r["line"] = $line;
72 5
                    $r["source"] = $file->property("source")[$line - 1];
73 10
                })
74 10
                ->expand_target()
75 10
                ->filter($filter_right)
76 10
                ;
77
        }
78 6
        if ($mode == Rule::MODE_MUST) {
79 6
            $filter_left = $var_left->compile();
80 6
            $filter_right = $var_right->compile();
81 6
            return $index->query()
82 6
                ->filter($filter_left)
83
                ->filter(function(Node $n) use ($filter_right) {
84
                    $rels = $n->relations(function($r) {
85 6
                        return $r->type() == $this->name();
86 6
                    });
87 6
                    if (count($rels) == 0) {
88
                        return true;
89
                    }
90 6
                    foreach ($rels as $rel) {
91 6
                        if ($filter_right($rel->target())) {
92 3
                            return false;
93
                        }
94 4
                    }
95 3
                    return true;
96 6
                })
97
                ->extract(function($e,&$r) use ($index, $rule) {
98 3
                    $rels = $e->relations(function($r) {
99 3
                        return $r->type() == "defined in";
100 3
                    });
101 3
                    assert('count($rels) == 1');
102 3
                    $file = $rels[0]->target();
103 3
                    assert('$file->type() == "file"');
104 3
                    $r["file"] = $file->property("path");
105 3
                    $line = $rels[0]->property("start_line");
106 3
                    $r["line"] = $line;
107 3
                    $r["source"] = $file->property("source")[$line - 1];
108 6
                })
109 6
                ;
110
        }
111
        throw new \LogicException("Unknown rule mode: '$mode'");
112
    }
113
114
    /**
115
     * Insert this relation somewhere, where it is recorded for all
116
     * entities that the current location is in.
117
     *
118
     * @param   Insert      $insert
119
     * @param   Location    $location
120
     * @param   mixed       $other
121
     * @return  null
122
     */
123 22
    protected function insert_relation_into(Insert $insert, Location $location, $other, $line) {
124 22
        assert('is_int($line)');
125 22
        foreach ($location->in_entities() as $entity) {
126 22
            if ($entity[0] == Variable::FILE_TYPE) {
127 22
                continue;
128
            }
129 22
            $insert->_relation
130 22
                ( $entity[1]
131 22
                , $this->name()
132 22
                , $other
133 22
                , $location->file()
134 22
                , $line
135 22
                );
136 22
        }
137 22
    }
138
}
139