1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of project-quality-inspector. |
4
|
|
|
* |
5
|
|
|
* (c) Alexandre GESLIN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace ProjectQualityInspector\Rule; |
12
|
|
|
|
13
|
|
|
use ProjectQualityInspector\Exception\RuleViolationException; |
14
|
|
|
use ProjectQualityInspector\Exception\ExpectationFailedException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class AbstractRule |
18
|
|
|
* |
19
|
|
|
* @package ProjectQualityInspector\Rule |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractRule implements RuleInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $config; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $baseDir; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $assertions = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritdoc |
40
|
|
|
*/ |
41
|
|
|
public function __construct(array $config, $baseDir) |
42
|
|
|
{ |
43
|
|
|
$this->config = $config; |
44
|
|
|
$this->baseDir = $baseDir; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritdoc |
49
|
|
|
*/ |
50
|
|
|
public function getConfig() |
51
|
|
|
{ |
52
|
|
|
return $this->config; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public static function getRuleName() |
59
|
|
|
{ |
60
|
|
|
$path = explode('\\', static::class); |
61
|
|
|
$name = array_pop($path); |
62
|
|
|
$name = preg_replace('#([A-Z])#', '-$1', lcfirst($name)); |
63
|
|
|
$name = strtolower($name); |
64
|
|
|
|
65
|
|
|
return $name; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
public function getAssertions() |
72
|
|
|
{ |
73
|
|
|
return $this->assertions; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param array|string $raw |
78
|
|
|
* @return array|string |
79
|
|
|
*/ |
80
|
|
|
protected function getValue($raw) |
81
|
|
|
{ |
82
|
|
|
if (is_array($raw) && isset($raw['value'])) { |
83
|
|
|
return $raw['value']; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $raw; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param array|string $raw |
91
|
|
|
* @return array|string|null |
92
|
|
|
*/ |
93
|
|
|
protected function getReason($raw) |
94
|
|
|
{ |
95
|
|
|
if (is_array($raw) && isset($raw['reason'])) { |
96
|
|
|
return $raw['reason']; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return ''; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param array $expectationFailedExceptions |
104
|
|
|
* |
105
|
|
|
* @throws RuleViolationException |
106
|
|
|
*/ |
107
|
|
|
protected function throwRuleViolationException(array $expectationFailedExceptions) |
108
|
|
|
{ |
109
|
|
|
throw new RuleViolationException($this, $expectationFailedExceptions); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $name |
114
|
|
|
* @param array $failures |
115
|
|
|
* @param integer $assertions |
116
|
|
|
* @param integer $time |
117
|
|
|
* @param array $errors |
118
|
|
|
*/ |
119
|
|
|
protected function addAssertion($name, array $failures = [], $assertions = 1, $time = 0, array $errors = []) |
120
|
|
|
{ |
121
|
|
|
$this->assertions[] = [ |
122
|
|
|
'name' => $name, |
123
|
|
|
'assertions' => $assertions, |
124
|
|
|
'classname' => '', |
125
|
|
|
'status' => '', |
126
|
|
|
'time' => $time, |
127
|
|
|
'failures' => [ |
128
|
|
|
'sum' => count($failures), |
129
|
|
|
'list' => ((count($failures)) ? [['message' => $failures[0]['message'], 'type' => $failures[0]['type']]] : []) //TODO all failures |
130
|
|
|
], |
131
|
|
|
'errors' => [ |
132
|
|
|
'sum' => count($errors), |
133
|
|
|
'list' => ((count($errors)) ? [['message' => $errors[0]['message'], 'type' => $errors[0]['type']]] : []) //TODO all errors |
134
|
|
|
], |
135
|
|
|
]; |
136
|
|
|
} |
137
|
|
|
} |