1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPMD\Baseline; |
4
|
|
|
|
5
|
|
|
use PHPMD\TextUI\CommandLineOptions; |
6
|
|
|
use RuntimeException; |
7
|
|
|
|
8
|
|
|
class BaselineFileFinder |
9
|
|
|
{ |
10
|
|
|
const DEFAULT_FILENAME = 'phpmd.baseline.xml'; |
11
|
|
|
|
12
|
|
|
/** @var CommandLineOptions */ |
13
|
|
|
private $options; |
14
|
|
|
|
15
|
|
|
/** @var bool */ |
16
|
|
|
private $existingFile = false; |
17
|
|
|
|
18
|
|
|
/** @var bool */ |
19
|
|
|
private $notNull = false; |
20
|
|
|
|
21
|
|
|
public function __construct(CommandLineOptions $options) |
22
|
|
|
{ |
23
|
|
|
$this->options = $options; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The baseline filepath should point to an existing file (or null) |
28
|
|
|
* @return $this |
29
|
|
|
*/ |
30
|
|
|
public function existingFile() |
31
|
|
|
{ |
32
|
|
|
$this->existingFile = true; |
33
|
|
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* if true, the finder `must` find a file path, but doesn't necessarily exist |
38
|
|
|
* @return $this |
39
|
|
|
*/ |
40
|
|
|
public function notNull() |
41
|
|
|
{ |
42
|
|
|
$this->notNull = true; |
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Find the violation baseline file |
48
|
|
|
* |
49
|
|
|
* @return string|null |
50
|
|
|
* @throws RuntimeException |
51
|
|
|
*/ |
52
|
|
|
public function find() |
53
|
|
|
{ |
54
|
|
|
$file = $this->tryFind(); |
55
|
|
|
if ($file === null && $this->notNull === true) { |
56
|
|
|
throw new RuntimeException('Unable to find the baseline file. Use --baseline-file to specify the filepath'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $file; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Try to find the violation baseline file |
64
|
|
|
* |
65
|
|
|
* @return string|null |
66
|
|
|
* @throws RuntimeException |
67
|
|
|
*/ |
68
|
|
|
private function tryFind() |
69
|
|
|
{ |
70
|
|
|
// read baseline file from cli arguments |
71
|
|
|
$file = $this->options->baselineFile(); |
72
|
|
|
if ($file !== null) { |
73
|
|
|
return $file; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// find baseline file next to the (first) ruleset |
77
|
|
|
$ruleSets = explode(',', $this->options->getRuleSets()); |
78
|
|
|
$rulePath = realpath($ruleSets[0]); |
79
|
|
|
if ($rulePath === false) { |
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// create file path and check for existence |
84
|
|
|
$baselinePath = dirname($rulePath) . '/' . self::DEFAULT_FILENAME; |
85
|
|
|
if ($this->existingFile === true && file_exists($baselinePath) === false) { |
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $baselinePath; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|