Completed
Push — master ( 389cae...29b40e )
by Richard
06:27
created

Result   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 38.55 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 94.12%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 10
c 3
b 0
f 1
lcom 1
cbo 2
dl 32
loc 83
ccs 32
cts 34
cp 0.9412
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A ruleset() 0 3 1
A violations_of() 16 16 4
A violations_in() 16 16 4

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
 * An implementation of dicto (scg.unibe.ch/dicto) in and for PHP.
4
 * 
5
 * Copyright (c) 2016, 2015 Richard Klees <[email protected]>
6
 *
7
 * This software is licensed under The MIT License. You should have received 
8
 * a copy of the licence along with the code.
9
 */
10
11
namespace Lechimp\Dicto\Analysis;
12
use Lechimp\Dicto\Definition as Def;
13
use Lechimp\Dicto\Rules\Rule;
14
15
class Result {
16
    /**
17
     * @var Def\Ruleset
18
     */
19
    protected $ruleset;
20
21
    /**
22
     * @var Violation[] 
23
     */
24
    protected $violations;
25
26
    /**
27
     * @var array
28
     */
29
    protected $by_rule_cache;
30
31
    /**
32
     * @var array
33
     */
34
    protected $by_filename_cache;
35
36
    /**
37
     * @param   Def\Ruleset     $ruleset
38
     * @param   Violations[]    $violations
39
     */
40 4
    public function __construct(Def\Ruleset $ruleset, array $violations) {
41 4
        $this->ruleset = $ruleset;
42 4
        $this->violations = array_map(function(Violation $v) {
43 4
            return $v;
44 4
        }, $violations);
45 4
        $this->by_rule_cache = array();
46 4
        $this->by_filename_cache = array();
47 4
    }
48
49
    /**
50
     * @return  Def\Ruleset
51
     */
52
    public function ruleset() {
53
        return $this->ruleset;
54
    }
55
56
    /**
57
     * @param   Def\Rules\Rule  $rule
58
     * @return  Violation[]
59
     */
60 2 View Code Duplication
    public function violations_of(Rule $rule) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
61 2
        $r = $rule->pprint();
62 2
        if (array_key_exists($r, $this->by_rule_cache)) {
63 1
            return $this->by_rule_cache[$r];
64
        }
65
66 2
        $vs = array();
67 2
        foreach ($this->violations as $v) {
68 2
            if ($v->rule() == $rule) {
69 2
                $vs[] = $v;
70 2
            }
71 2
        }
72
73 2
        $this->by_rule_cache[$r] = $vs;
74 2
        return $vs;
75
    }
76
77
    /**
78
     * @param   string          $filename
79
     * @return  Violation[]
80
     */
81 2 View Code Duplication
    public function violations_in($filename) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
82 2
        assert('is_string($filename)');
83 2
        if (array_key_exists($filename, $this->by_filename_cache)) {
84 1
            return $this->by_filename_cache[$filename];
85
        }
86
87 2
        $vs = array();
88 2
        foreach ($this->violations as $v) {
89 2
            if ($v->filename() == $filename) {
90 2
                $vs[] = $v;
91 2
            }
92 2
        }
93
94 2
        $this->by_filename_cache[$filename] = $vs;
95 2
        return $vs;
96
    }
97
}
98