1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\WashingMachine\Clover; |
5
|
|
|
|
6
|
|
|
use TheCodingMachine\WashingMachine\Clover\Analysis\Method; |
7
|
|
|
|
8
|
|
|
final class Crap4JFile implements CrapMethodFetcherInterface |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
private $fileName; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var \SimpleXMLElement |
18
|
|
|
*/ |
19
|
|
|
private $root; |
20
|
|
|
|
21
|
|
|
private function __construct() |
22
|
|
|
{ |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public static function fromFile(string $fileName) : Crap4JFile |
26
|
|
|
{ |
27
|
|
|
if (!file_exists($fileName)) { |
28
|
|
|
throw new \RuntimeException('Could not find file "'.$fileName.'". The unit tests did not run or broke before the end, or the file path is incorrect.'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$crap4JFile = new self(); |
32
|
|
|
$crap4JFile->fileName = $fileName; |
33
|
|
|
$errorReporting = error_reporting(); |
34
|
|
|
$oldErrorReporting = error_reporting($errorReporting & ~E_WARNING); |
35
|
|
|
$crap4JFile->root = simplexml_load_file($fileName); |
36
|
|
|
error_reporting($oldErrorReporting); |
37
|
|
|
if ($crap4JFile->root === false) { |
38
|
|
|
throw new \RuntimeException('Invalid XML file passed or unable to load file: "'.$fileName.'": '.error_get_last()['message']); |
39
|
|
|
} |
40
|
|
|
return $crap4JFile; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public static function fromString(string $string) : Crap4JFile |
44
|
|
|
{ |
45
|
|
|
$cloverFile = new self(); |
46
|
|
|
$errorReporting = error_reporting(); |
47
|
|
|
$oldErrorReporting = error_reporting($errorReporting & ~E_WARNING); |
48
|
|
|
$cloverFile->root = simplexml_load_string($string); |
49
|
|
|
error_reporting($oldErrorReporting); |
50
|
|
|
if ($cloverFile->root === false) { |
51
|
|
|
throw new \RuntimeException('Invalid XML file passed or unable to load string: '.error_get_last()['message']); |
52
|
|
|
} |
53
|
|
|
return $cloverFile; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns an array of method objects, indexed by method full name. |
58
|
|
|
* |
59
|
|
|
* @return Method[] |
60
|
|
|
*/ |
61
|
|
|
public function getMethods() : array |
62
|
|
|
{ |
63
|
|
|
$methods = []; |
64
|
|
|
$methodsElement = $this->root->xpath('/crap_result/methods/method'); |
65
|
|
|
|
66
|
|
|
foreach ($methodsElement as $methodElement) { |
67
|
|
|
$package = (string) $methodElement->package; |
68
|
|
|
$className = (string) $methodElement->className; |
69
|
|
|
$methodName = (string) $methodElement->methodName; |
70
|
|
|
$crap = (int) $methodElement->crap; |
71
|
|
|
$complexity = (int) $methodElement->complexity; |
72
|
|
|
|
73
|
|
|
$method = new Method($methodName, $className, $package, $complexity, $crap); |
74
|
|
|
$methods[$method->getFullName()] = $method; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $methods; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|