-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathBaseline.php
More file actions
64 lines (52 loc) · 2.1 KB
/
Baseline.php
File metadata and controls
64 lines (52 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace staabm\PHPStanBaselineAnalysis;
use Iterator;
use Nette\Neon\Neon;
use RuntimeException;
final class Baseline {
/**
* @var array{parameters?: array{ignoreErrors?: list<array{message: ?string, count: int, path: ?string, identifier: ?string, rawMessage: ?string}>}}
*/
private $content;
/**
* @var string
*/
private $filePath;
static public function forFile(string $filePath):self {
$baselineExtension = pathinfo($filePath, PATHINFO_EXTENSION);
if ($baselineExtension === 'php') {
$decoded = require $filePath;
} else {
$content = file_get_contents($filePath);
$decoded = Neon::decode($content);
}
if (!is_array($decoded)) {
throw new RuntimeException(sprintf('expecting baseline %s to be non-empty', $filePath));
}
$baseline = new self();
$baseline->content = $decoded; // @phpstan-ignore assign.propertyType
$baseline->filePath = $filePath;
return $baseline;
}
/**
* @return Iterator<BaselineError>
*/
public function getIgnoreErrors(): Iterator {
// @phpstan-ignore function.alreadyNarrowedType
if (!array_key_exists('parameters', $this->content) || !is_array($this->content['parameters'])) {
throw new RuntimeException(sprintf('missing parameters from baseline %s', $this->filePath));
}
$parameters = $this->content['parameters'];
// @phpstan-ignore function.alreadyNarrowedType
if (!array_key_exists('ignoreErrors', $parameters) || !is_array($parameters['ignoreErrors'])) {
throw new RuntimeException(sprintf('missing ignoreErrors from baseline %s', $this->filePath));
}
$ignoreErrors = $parameters['ignoreErrors'];
foreach($ignoreErrors as $error) {
yield new BaselineError($error['count'], $error['message'] ?? null, $error['path'] ?? null, $error['identifier'] ?? null, $error['rawMessage'] ?? null);
}
}
public function getFilePath():string {
return $this->filePath ? : basename(getcwd());
}
}