Completed
Push — master ( a7556a...5a15ba )
by Richard
04:16
created

DiffPerRuleReport   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 71.67%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 95
ccs 43
cts 60
cp 0.7167
rs 10
c 2
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A default_template() 0 3 1
A source_url() 0 3 1
B generate() 0 68 6
A make_url() 0 7 1
1
<?php
2
3
namespace Lechimp\Dicto\Report;
4
5
/**
6
 * Shows the diff in violations between two commits per rule.
7
 */
8
class DiffPerRuleReport extends Report {
9
    /**
10
     * @inheritdoc
11
     */
12 1
    protected function default_template() {
13 1
        return "diff_per_rule";
14
    }
15
16
    /**
17
     * @return  string|null
18
     */
19 1
    protected function source_url() {
20 1
        return $this->custom_config_value("source_url", null);
21
    }
22
23
    /**
24
     * @inheritdoc
25
     */
26 1
    public function generate() {
27 1
        $cur_run = $this->queries->last_run();
28 1
        $prev_run = $this->queries->run_with_different_commit_before($cur_run);
29 1
        $source_url = $this->source_url();
30 1
        $current = $this->queries->run_info($cur_run);
31 1
        $previous = $this->queries->run_info($prev_run);
32
33
        return
34
            [ "run_id"  => $cur_run
35 1
            , "current" => $current
36 1
            , "previous" => $this->queries->run_info($prev_run)
37 1
            , "violations" =>
38 1
                [ "total" => $this->queries->count_violations_in($cur_run)
39 1
                , "added" => $this->queries->count_added_violations($prev_run, $cur_run)
40 1
                , "resolved" => $this->queries->count_resolved_violations($prev_run, $cur_run)
41 1
                ]
42 1
            , "rules" => array_map
43
                ( function($rule) use ($cur_run, $prev_run, $current, $previous, $source_url) {
44 1
                    $rule_info = $this->queries->rule_info($rule);
45
                    $violations = array_merge
46 1
                        ( $this->queries->violations_of($rule, $cur_run)
47 1
                        , $this->queries->resolved_violations($rule, $prev_run, $cur_run)
48 1
                        );
49
                    usort($violations, function($l, $r) {
50 1
                        $file_cmp = strcmp($l["file"], $r["file"]);
51 1
                        if ($file_cmp == 0) {
52
                            if ($l["line_no"] == $r["line_no"]) {
53
                                return 0;
54
                            }
55
                            if ($l["line_no"] < $r["line_no"]) {
56
                                return -1;
57
                            }
58
                            else {
59
                                return 1;
60
                            }
61
                        }
62 1
                        return $file_cmp;
63 1
                    });
64
                    return
65 1
                        [ "rule" => $rule_info["rule"]
66 1
                        , "explanation" => $rule_info["explanation"]
67 1
                        , "violations" =>
68 1
                            [ "total" => $this->queries->count_violations_in($cur_run, $rule)
69 1
                            , "added" => $this->queries->count_added_violations($prev_run, $cur_run, $rule)
70 1
                            , "resolved" => $this->queries->count_resolved_violations($prev_run, $cur_run, $rule)
71 1
                            , "list" => array_map
72 1
                                ( function($v) use ($current, $previous, $source_url) {
73 1
                                    if ($source_url !== null) {
74
                                        $v["url"] = $this->make_url
75
                                                        ( $source_url
76
                                                        , (isset($v["last_seen_in"])) ? $v["last_seen_in"] : $previous["commit_hash"]
77
                                                        , $v["file"]
78
                                                        , $v["line_no"]
79
                                                        );
80
                                    }
81
                                    else {
82 1
                                        $v["url"] = null;
83
                                    }
84 1
                                    return $v;
85
                                }
86 1
                                , $violations
87 1
                            )]
88 1
                        ];
89
                }
90 1
                , $this->queries->analyzed_rules($cur_run)
91 1
                )
92 1
            ];
93
    }
94
95
    protected function make_url($source_url, $commit_hash, $file, $line) {
96
        return
97
            str_replace("{COMMIT}", $commit_hash,
98
            str_replace("{FILE}", $file,
99
            str_replace("{LINE}", $line,
100
                $source_url)));
101
    }
102
}
103