Passed
Push — master ( 7ed4ac...281a8d )
by Kyle
58s queued 10s
created

BaselineSetFactory::fromFile()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 8
nop 1
dl 0
loc 39
rs 7.7404
c 0
b 0
f 0
1
<?php
2
3
namespace PHPMD\Baseline;
4
5
use PHPMD\Utility\Paths;
6
use RuntimeException;
7
8
class BaselineSetFactory
9
{
10
    /**
11
     * Read the baseline violations from the given filename path. Append the baseDir to all the filepaths within
12
     * the baseline file.
13
     *
14
     * @param string $fileName
15
     * @return BaselineSet
16
     * @throws RuntimeException
17
     */
18
    public static function fromFile($fileName)
19
    {
20
        if (file_exists($fileName) === false) {
21
            throw new RuntimeException('Unable to locate the baseline file at: ' . $fileName);
22
        }
23
24
        $xml = @simplexml_load_string(file_get_contents($fileName));
25
        if ($xml === false) {
26
            throw new RuntimeException('Unable to read xml from: ' . $fileName);
27
        }
28
29
        $basePath    = dirname($fileName);
30
        $baselineSet = new BaselineSet();
31
32
        foreach ($xml->children() as $node) {
33
            if ($node->getName() !== 'violation') {
34
                continue;
35
            }
36
37
            if (isset($node['rule']) === false) {
38
                throw new RuntimeException('Missing `rule` attribute in `violation` in ' . $fileName);
39
            }
40
41
            if (isset($node['file']) === false) {
42
                throw new RuntimeException('Missing `file` attribute in `violation` in ' . $fileName);
43
            }
44
45
            $ruleName   = (string)$node['rule'];
46
            $filePath   = Paths::concat($basePath, (string)$node['file']);
47
            $methodName = null;
48
            if (isset($node['method']) === true && ((string)$node['method']) !== '') {
49
                $methodName = (string)($node['method']);
50
            }
51
52
            $baselineSet->addEntry(new ViolationBaseline($ruleName, $filePath, $methodName));
53
        }
54
55
        return $baselineSet;
56
    }
57
}
58