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

Relation::compile()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 57
Code Lines 44

Duplication

Lines 8
Ratio 14.04 %

Code Coverage

Tests 46
CRAP Score 7.0035

Importance

Changes 0
Metric Value
dl 8
loc 57
ccs 46
cts 48
cp 0.9583
rs 7.6759
c 0
b 0
f 0
cc 7
eloc 44
nc 3
nop 2
crap 7.0035

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\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