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

Result::violations_of()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 16
loc 16
ccs 12
cts 12
cp 1
rs 9.2
nc 4
cc 4
eloc 10
nop 1
crap 4
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