|
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) { |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
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
|
|
|
|
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.