forked from spaceemotion/php-coding-standard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeptrac.php
More file actions
88 lines (66 loc) · 2.3 KB
/
Deptrac.php
File metadata and controls
88 lines (66 loc) · 2.3 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
declare(strict_types=1);
namespace Spaceemotion\PhpCodingStandard\Tools;
use Spaceemotion\PhpCodingStandard\Context;
use Spaceemotion\PhpCodingStandard\Formatter\File;
use Spaceemotion\PhpCodingStandard\Formatter\Result;
use Spaceemotion\PhpCodingStandard\Formatter\Violation;
use function preg_match;
class Deptrac extends Tool
{
/** @var string */
protected $name = 'deptrac';
protected function supportsMemoryLimit(): bool
{
return true;
}
public function run(Context $context): bool
{
$outputFile = $this->createTempReportFile();
if (
$this->execute(self::vendorBinary('deptrac'), [
'--formatter=xml',
'--no-progress',
'--no-interaction',
$this->useNewOutputFormat()
? "--output={$outputFile}"
: "--xml-dump={$outputFile}",
]) === 0
) {
return true;
}
$entries = simplexml_load_string(file_get_contents($outputFile));
if ($entries === false) {
return false;
}
foreach ($entries->entry as $entry) {
$layerA = (string) $entry->LayerA;
$layerB = (string) $entry->LayerB;
$classA = (string) $entry->ClassA;
$classB = (string) $entry->ClassB;
$occurrence = $entry->occurrence;
$violation = new Violation();
$violation->message = "{$classA} must not depend on {$classB}";
$violation->source = "{$layerA} on {$layerB}";
$violation->tool = $this->getName();
$violation->line = (int) $occurrence['line'];
$file = new File();
$file->violations[] = $violation;
$result = new Result();
$result->files[(string) $occurrence['file']] = $file;
$context->addResult($result);
}
return false;
}
protected function useNewOutputFormat(): bool
{
$output = [];
$matches = [];
$this->execute(self::vendorBinary('deptrac'), ['--version'], $output);
preg_match('/(?<version>[\d.]+)/', implode(' ', $output), $matches);
if (isset($matches['version'])) {
return ((float) $matches['version']) >= 0.19;
}
return false;
}
}