Passed
Push — master ( 3dd71e...2a510c )
by Richard
03:38
created

DiffPerRuleReport::make_url()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
ccs 0
cts 5
cp 0
cc 1
eloc 6
nc 1
nop 4
crap 2
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->current_run();
28 1
        $prev_run = $this->queries->previous_run_with_different_commit();
29 1
        $source_url = $this->source_url();
30 1
        $current = $this->queries->run_info($cur_run);
31
        return
32
            [ "run_id"  => $cur_run
33 1
            , "current" => $current
34 1
            , "previous" => $this->queries->run_info($prev_run)
35 1
            , "violations" =>
36 1
                [ "total" => $this->queries->count_violations_in($cur_run)
37 1
                , "added" => $this->queries->count_added_violations($prev_run, $cur_run)
38 1
                , "resolved" => $this->queries->count_resolved_violations($prev_run, $cur_run)
39 1
                ]
40 1
            , "rules" => array_map
41
                ( function($rule) use ($cur_run, $prev_run, $current, $source_url) {
42 1
                    $rule_info = $this->queries->rule_info($rule);
43
                    return
44 1
                        [ "rule" => $rule_info["rule"]
45 1
                        , "explanation" => $rule_info["explanation"]
46 1
                        , "violations" =>
47 1
                            [ "total" => $this->queries->count_violations_in($cur_run, $rule)
48 1
                            , "added" => $this->queries->count_added_violations($prev_run, $cur_run, $rule)
49 1
                            , "resolved" => $this->queries->count_resolved_violations($prev_run, $cur_run, $rule)
50 1
                            , "list" => array_map
51 1
                                ( function($v) use ($current, $source_url) {
52 1
                                    if ($source_url !== null) {
53
                                        $v["url"] = $this->make_url
54
                                                        ( $source_url
55
                                                        , $current["commit_hash"]
56
                                                        , $v["file"]
57
                                                        , $v["line_no"]
58
                                                        );
59
                                    }
60
                                    else {
61 1
                                        $v["url"] = null;
62
                                    }
63 1
                                    return $v;
64
                                }
65 1
                                , $this->queries->violations_of($rule, $cur_run)
66 1
                                )
67 1
                            ]
68 1
                        ];
69
                }
70 1
                , $this->queries->analyzed_rules($cur_run)
71 1
                )
72 1
            ];
73
    }
74
75
    protected function make_url($source_url, $commit_hash, $file, $line) {
76
        return
77
            str_replace("{COMMIT}", $commit_hash,
78
            str_replace("{FILE}", $file,
79
            str_replace("{LINE}", $line,
80
                $source_url)));
81
    }
82
}
83