Completed
Push — master ( 2a510c...5bfa5e )
by Richard
03:56
created

TotalPerRuleReport::generate()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 42
Code Lines 30

Duplication

Lines 14
Ratio 33.33 %

Code Coverage

Tests 26
CRAP Score 2.0381

Importance

Changes 0
Metric Value
dl 14
loc 42
ccs 26
cts 33
cp 0.7879
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 30
nc 1
nop 0
crap 2.0381
1
<?php
2
3
namespace Lechimp\Dicto\Report;
4
5
/**
6
 * Shows the diff in violations between two commits per rule.
7
 */
8
class TotalPerRuleReport extends Report {
9
    /**
10
     * @inheritdoc
11
     */
12 1
    protected function default_template() {
13 1
        return "total_per_rule";
14
    }
15
16
    /**
17
     * TODO: Move this to Report.
18
     * @return  string|null
19
     */
20 1
    protected function source_url() {
21 1
        return $this->custom_config_value("source_url", null);
22
    }
23
24
    /**
25
     * @inheritdoc
26
     */
27 1
    public function generate() {
28 1
        $cur_run = $this->queries->current_run();
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
            , "violations" =>
35 1
                [ "total" => $this->queries->count_violations_in($cur_run)
36 1
                ]
37 1
            , "rules" => array_map
38
                ( function($rule) use ($cur_run, $current, $source_url) {
39 1
                    $rule_info = $this->queries->rule_info($rule);
40
                    return
41 1
                        [ "rule" => $rule_info["rule"]
42 1
                        , "explanation" => $rule_info["explanation"]
43 1
                        , "violations" =>
44 1
                            [ "total" => $this->queries->count_violations_in($cur_run, $rule)
45 1
                            , "list" => array_map
46 1 View Code Duplication
                                ( function($v) use ($current, $source_url) {
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...
47 1
                                    if ($source_url !== null) {
48
                                        $v["url"] = $this->make_url
49
                                                        ( $source_url
50
                                                        , $current["commit_hash"]
51
                                                        , $v["file"]
52
                                                        , $v["line_no"]
53
                                                        );
54
                                    }
55
                                    else {
56 1
                                        $v["url"] = null;
57
                                    }
58 1
                                    return $v;
59
                                }
60 1
                                , $this->queries->violations_of($rule, $cur_run)
61 1
                                )
62 1
                            ]
63 1
                        ];
64
                }
65 1
                , $this->queries->analyzed_rules($cur_run)
66 1
                )
67 1
            ];
68
    }
69
70
    /**
71
     * TODO: Move this to Report.
72
     *
73
     * @param   string  $source_url
74
     * @param   string  $commit_hash
75
     * @param   string  $file
76
     * @param   int     $line
77
     * @return  string
78
     */
79
    protected function make_url($source_url, $commit_hash, $file, $line) {
80
        return
81
            str_replace("{COMMIT}", $commit_hash,
82
            str_replace("{FILE}", $file,
83
            str_replace("{LINE}", $line,
84
                $source_url)));
85
    }
86
}
87