|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Symplify |
|
5
|
|
|
* Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz). |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Symplify\PHP7_CodeSniffer\Application; |
|
9
|
|
|
|
|
10
|
|
|
use Symplify\PHP7_CodeSniffer\Application\Command\RunApplicationCommand; |
|
11
|
|
|
use Symplify\PHP7_CodeSniffer\Configuration\ConfigurationResolver; |
|
12
|
|
|
use Symplify\PHP7_CodeSniffer\EventDispatcher\SniffDispatcher; |
|
13
|
|
|
use Symplify\PHP7_CodeSniffer\Exception\AnySniffMissingException; |
|
14
|
|
|
use Symplify\PHP7_CodeSniffer\File\Provider\FilesProvider; |
|
15
|
|
|
use Symplify\PHP7_CodeSniffer\Legacy\LegacyConfiguration; |
|
16
|
|
|
use Symplify\PHP7_CodeSniffer\Sniff\SniffSetFactory; |
|
17
|
|
|
use Symplify\PHP7_CodeSniffer\Sniff\Xml\DataCollector\ExcludedSniffDataCollector; |
|
18
|
|
|
|
|
19
|
|
|
final class Application |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var SniffDispatcher |
|
23
|
|
|
*/ |
|
24
|
|
|
private $sniffDispatcher; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var FilesProvider |
|
28
|
|
|
*/ |
|
29
|
|
|
private $filesProvider; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var SniffSetFactory |
|
33
|
|
|
*/ |
|
34
|
|
|
private $sniffSetFactory; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var ExcludedSniffDataCollector |
|
38
|
|
|
*/ |
|
39
|
|
|
private $excludedSniffDataCollector; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var ConfigurationResolver |
|
43
|
|
|
*/ |
|
44
|
|
|
private $configurationResolver; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var FileProcessor |
|
48
|
|
|
*/ |
|
49
|
|
|
private $fileProcessor; |
|
50
|
|
|
|
|
51
|
1 |
|
public function __construct( |
|
52
|
|
|
SniffDispatcher $sniffDispatcher, |
|
53
|
|
|
FilesProvider $sourceFilesProvider, |
|
54
|
|
|
SniffSetFactory $sniffFactory, |
|
55
|
|
|
ExcludedSniffDataCollector $excludedSniffDataCollector, |
|
56
|
|
|
ConfigurationResolver $configurationResolver, |
|
57
|
|
|
FileProcessor $fileProcessor |
|
58
|
|
|
) { |
|
59
|
1 |
|
$this->sniffDispatcher = $sniffDispatcher; |
|
60
|
1 |
|
$this->filesProvider = $sourceFilesProvider; |
|
61
|
1 |
|
$this->sniffSetFactory = $sniffFactory; |
|
62
|
1 |
|
$this->excludedSniffDataCollector = $excludedSniffDataCollector; |
|
63
|
1 |
|
$this->configurationResolver = $configurationResolver; |
|
64
|
1 |
|
$this->fileProcessor = $fileProcessor; |
|
65
|
|
|
|
|
66
|
1 |
|
LegacyConfiguration::setup(); |
|
67
|
1 |
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
public function runCommand(RunApplicationCommand $command) |
|
70
|
|
|
{ |
|
71
|
1 |
|
$command = $this->resolveCommandConfiguration($command); |
|
72
|
|
|
|
|
73
|
1 |
|
$this->excludedSniffDataCollector->addExcludedSniffs($command->getExcludedSniffs()); |
|
74
|
|
|
|
|
75
|
1 |
|
$this->createAndRegisterSniffsToSniffDispatcher( |
|
76
|
1 |
|
$command->getStandards(), |
|
77
|
1 |
|
$command->getSniffs() |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
$this->runForSource($command->getSource(), $command->isFixer()); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
private function createAndRegisterSniffsToSniffDispatcher(array $standards, array $extraSniffs) |
|
84
|
|
|
{ |
|
85
|
1 |
|
$sniffs = $this->sniffSetFactory->createFromStandardsAndSniffs($standards, $extraSniffs); |
|
86
|
1 |
|
$this->ensureAtLeastOneSniffIsRegistered($sniffs); |
|
87
|
|
|
$this->sniffDispatcher->addSniffListeners($sniffs); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
private function runForSource(array $source, bool $isFixer) |
|
91
|
|
|
{ |
|
92
|
|
|
$files = $this->filesProvider->getFilesForSource($source, $isFixer); |
|
93
|
|
|
$this->fileProcessor->processFiles($files, $isFixer); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
private function ensureAtLeastOneSniffIsRegistered(array $sniffs) |
|
97
|
|
|
{ |
|
98
|
1 |
|
if (count($sniffs) < 1) { |
|
99
|
1 |
|
throw new AnySniffMissingException( |
|
100
|
1 |
|
'You need to specify some sniffs with "--standards=..." or "--sniffs=...".' |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
private function resolveCommandConfiguration( |
|
106
|
|
|
RunApplicationCommand $command |
|
107
|
|
|
) : RunApplicationCommand { |
|
108
|
1 |
|
return new RunApplicationCommand( |
|
109
|
1 |
|
$command->getSource(), |
|
110
|
1 |
|
$this->configurationResolver->resolve('standards', $command->getStandards()), |
|
111
|
1 |
|
$this->configurationResolver->resolve('sniffs', $command->getSniffs()), |
|
112
|
1 |
|
$this->configurationResolver->resolve('sniffs', $command->getExcludedSniffs()), |
|
113
|
1 |
|
$command->isFixer() |
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|