|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Leankoala\HealthFoundation\Check\Files\Content; |
|
4
|
|
|
|
|
5
|
|
|
use Leankoala\HealthFoundation\Check\BasicCheck; |
|
6
|
|
|
use Leankoala\HealthFoundation\Check\MetricAwareResult; |
|
7
|
|
|
use Leankoala\HealthFoundation\Check\Result; |
|
8
|
|
|
|
|
9
|
|
|
class NumberOfLinesCheck extends BasicCheck |
|
10
|
|
|
{ |
|
11
|
|
|
protected $identifier = 'base:files:content:numberOfLines'; |
|
12
|
|
|
|
|
13
|
|
|
private $files = []; |
|
14
|
|
|
|
|
15
|
|
|
private $relation; |
|
16
|
|
|
|
|
17
|
|
|
private $limit; |
|
18
|
|
|
|
|
19
|
|
|
const RELATION_MAX = 'max'; |
|
20
|
|
|
const RELATION_MIN = 'min'; |
|
21
|
|
|
|
|
22
|
|
|
private $pattern = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @return MetricAwareResult |
|
26
|
|
|
*/ |
|
27
|
|
|
public function run() |
|
28
|
|
|
{ |
|
29
|
|
|
foreach ($this->files as $file) { |
|
30
|
|
|
if (!file_exists($file)) { |
|
31
|
|
|
return new MetricAwareResult(Result::STATUS_FAIL, 'Unable to get document length because file "' . $file . '" does not exist.'); |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$numberLines = $this->getNumberOfLines(); |
|
36
|
|
|
|
|
37
|
|
|
return $this->processData($numberLines); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return int |
|
42
|
|
|
*/ |
|
43
|
|
|
private function getNumberOfLines() |
|
44
|
|
|
{ |
|
45
|
|
|
$numberOfLines = 0; |
|
46
|
|
|
|
|
47
|
|
|
foreach ($this->files as $file) { |
|
48
|
|
|
$grep = ''; |
|
49
|
|
|
if ($this->pattern) { |
|
|
|
|
|
|
50
|
|
|
foreach ($this->pattern as $pattern) { |
|
51
|
|
|
$grep .= ' | grep -a "' . $pattern . '"'; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$command = 'cat ' . $file . $grep . ' | wc -l'; |
|
56
|
|
|
$output = []; |
|
57
|
|
|
exec($command, $output, $return); |
|
58
|
|
|
$numberOfLines += (int)$output[0]; |
|
59
|
|
|
} |
|
60
|
|
|
return $numberOfLines; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param $numberLines |
|
65
|
|
|
* @return Result |
|
66
|
|
|
*/ |
|
67
|
|
|
private function processData($numberLines) |
|
68
|
|
|
{ |
|
69
|
|
|
if ($this->relation === self::RELATION_MAX) { |
|
70
|
|
|
if ($numberLines > $this->limit) { |
|
71
|
|
|
$result = new MetricAwareResult(Result::STATUS_FAIL, 'The document contains too many lines (' . $numberLines . '). Expected where ' . $this->limit . ' at the most.'); |
|
72
|
|
|
} else { |
|
73
|
|
|
$result = new MetricAwareResult(Result::STATUS_PASS, 'The document contains ' . $numberLines . ' lines. Expected where ' . $this->limit . ' at the most.'); |
|
74
|
|
|
} |
|
75
|
|
|
} else { |
|
76
|
|
|
if ($numberLines < $this->limit) { |
|
77
|
|
|
$result = new MetricAwareResult(Result::STATUS_FAIL, 'The document contains too few lines (' . $numberLines . '). Expected where ' . $this->limit . ' at least.'); |
|
78
|
|
|
} else { |
|
79
|
|
|
$result = new MetricAwareResult(Result::STATUS_PASS, 'The document contains ' . $numberLines . ' lines. Expected where ' . $this->limit . ' at least.'); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$result->setMetric($numberLines, 'lines'); |
|
84
|
|
|
|
|
85
|
|
|
$result->addAttribute('files', $this->files); |
|
86
|
|
|
$result->addAttribute('pattern', $this->pattern); |
|
87
|
|
|
|
|
88
|
|
|
return $result; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function init($files, $limit, $relation = self::RELATION_MAX, $pattern = []) |
|
92
|
|
|
{ |
|
93
|
|
|
if (is_string($files)) { |
|
94
|
|
|
$files = [$file]; |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$this->files = $files; |
|
98
|
|
|
$this->pattern = (array)$pattern; |
|
99
|
|
|
$this->relation = $relation; |
|
100
|
|
|
$this->limit = $limit; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected function getCheckIdentifier() |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->identifier . '.' . md5(json_encode($this->files) . serialize($this->pattern)); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.