forked from PHPCSStandards/PHP_CodeSniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaselineSetFactory.php
More file actions
67 lines (50 loc) · 1.94 KB
/
BaselineSetFactory.php
File metadata and controls
67 lines (50 loc) · 1.94 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
65
66
67
<?php
/**
* A factory to create a baseline collection from a given file
*
* @author Frank Dekker <fdekker@123inkt.nl>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Baseline;
use PHP_CodeSniffer\Exceptions\RuntimeException;
class BaselineSetFactory
{
/**
* Read the baseline violations from the given filename path.
*
* @param string $fileName the baseline file to import
*
* @return BaselineSet|null
* @throws RuntimeException
*/
public static function fromFile($fileName)
{
if (file_exists($fileName) === false) {
return null;
}
$xml = @simplexml_load_string(file_get_contents($fileName));
if ($xml === false) {
throw new RuntimeException('Unable to read xml from: '.$fileName);
}
$baselineSet = new BaselineSet();
foreach ($xml->children() as $node) {
if ($node->getName() !== 'violation') {
continue;
}
if (isset($node['sniff']) === false) {
throw new RuntimeException('Missing `sniff` attribute in `violation` in '.$fileName);
}
if (isset($node['file']) === false) {
throw new RuntimeException('Missing `file` attribute in `violation` in '.$fileName);
}
if (isset($node['signature']) === false) {
throw new RuntimeException('Missing `signature` attribute in `violation` in '.$fileName);
}
// Normalize filepath (if needed).
$filePath = '/'.ltrim(str_replace('\\', '/', (string) $node['file']), '/');
$baselineSet->addEntry(new ViolationBaseline((string) $node['sniff'], $filePath, (string) $node['signature']));
}//end foreach
return $baselineSet;
}//end fromFile()
}//end class