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

TotalPerRuleReport   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 79
Duplicated Lines 17.72 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 71.43%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 14
loc 79
ccs 30
cts 42
cp 0.7143
rs 10
c 0
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() 14 42 2
A make_url() 0 7 1

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