|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright MediaCT. All rights reserved. |
|
4
|
|
|
* https://www.mediact.nl |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Mediact\DependencyGuard\Composer\Command\Exporter; |
|
8
|
|
|
|
|
9
|
|
|
use Mediact\DependencyGuard\Exporter\ViolationExporterInterface; |
|
10
|
|
|
use Mediact\DependencyGuard\Php\SymbolInterface; |
|
11
|
|
|
use Mediact\DependencyGuard\Violation\ViolationIteratorInterface; |
|
12
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
13
|
|
|
|
|
14
|
|
|
class TextViolationExporter implements ViolationExporterInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var SymfonyStyle */ |
|
17
|
|
|
private $prompt; |
|
18
|
|
|
|
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
private $workingDirectory; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Constructor. |
|
24
|
|
|
* |
|
25
|
|
|
* @param SymfonyStyle $prompt |
|
26
|
|
|
* @param string|null $workingDirectory |
|
27
|
|
|
*/ |
|
28
|
1 |
|
public function __construct( |
|
29
|
|
|
SymfonyStyle $prompt, |
|
30
|
|
|
string $workingDirectory = null |
|
31
|
|
|
) { |
|
32
|
1 |
|
$this->prompt = $prompt; |
|
33
|
1 |
|
$this->workingDirectory = $workingDirectory ?? getcwd(); |
|
34
|
1 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Export the given violations. |
|
38
|
|
|
* |
|
39
|
|
|
* @param ViolationIteratorInterface $violations |
|
40
|
|
|
* |
|
41
|
|
|
* @return void |
|
42
|
|
|
*/ |
|
43
|
3 |
|
public function export(ViolationIteratorInterface $violations): void |
|
44
|
|
|
{ |
|
45
|
3 |
|
$root = preg_quote($this->workingDirectory . '/', '#'); |
|
46
|
|
|
|
|
47
|
3 |
|
foreach ($violations as $violation) { |
|
48
|
2 |
|
$this->prompt->error($violation->getMessage()); |
|
49
|
|
|
|
|
50
|
2 |
|
$this->prompt->listing( |
|
51
|
2 |
|
array_map( |
|
52
|
|
|
function ( |
|
53
|
|
|
SymbolInterface $symbol |
|
54
|
|
|
) use ( |
|
55
|
1 |
|
$root |
|
56
|
|
|
) : string { |
|
57
|
1 |
|
return sprintf( |
|
58
|
|
|
'Detected <comment>%s</comment> ' |
|
59
|
1 |
|
. 'in <comment>%s:%d</comment>', |
|
60
|
1 |
|
$symbol->getName(), |
|
61
|
1 |
|
preg_replace( |
|
62
|
1 |
|
sprintf('#^%s#', $root), |
|
63
|
1 |
|
'', |
|
64
|
1 |
|
$symbol->getFile() |
|
65
|
|
|
), |
|
66
|
1 |
|
$symbol->getLine() |
|
67
|
|
|
); |
|
68
|
2 |
|
}, |
|
69
|
2 |
|
iterator_to_array($violation->getSymbols()) |
|
70
|
|
|
) |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
3 |
|
$numViolations = count($violations); |
|
75
|
|
|
|
|
76
|
3 |
|
if ($numViolations === 0) { |
|
77
|
1 |
|
$this->prompt->success('No dependency violations encountered!'); |
|
78
|
|
|
|
|
79
|
1 |
|
return; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
$this->prompt->error( |
|
83
|
2 |
|
sprintf('Number of dependency violations: %d', $numViolations) |
|
84
|
|
|
); |
|
85
|
2 |
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|