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; |
9
|
|
|
|
10
|
|
|
use Symplify\PHP7_CodeSniffer\EventDispatcher\Event\CheckFileTokenEvent; |
11
|
|
|
use Symplify\PHP7_CodeSniffer\EventDispatcher\SniffDispatcher; |
12
|
|
|
use Symplify\PHP7_CodeSniffer\Exception\AnySniffMissingException; |
13
|
|
|
use Symplify\PHP7_CodeSniffer\File\File; |
14
|
|
|
use Symplify\PHP7_CodeSniffer\File\Provider\FilesProvider; |
15
|
|
|
use Symplify\PHP7_CodeSniffer\Sniff\SniffFactory; |
16
|
|
|
use Symplify\PHP7_CodeSniffer\Sniff\SniffClassesResolver; |
17
|
|
|
|
18
|
|
|
final class Php7CodeSniffer |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var SniffDispatcher |
22
|
|
|
*/ |
23
|
|
|
private $sniffDispatcher; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var FilesProvider |
27
|
|
|
*/ |
28
|
|
|
private $filesProvider; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var SniffClassesResolver |
32
|
|
|
*/ |
33
|
|
|
private $sniffClassesResolver; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var SniffFactory |
37
|
|
|
*/ |
38
|
|
|
private $sniffFactory; |
39
|
|
|
|
40
|
1 |
|
public function __construct( |
41
|
|
|
SniffDispatcher $sniffDispatcher, |
42
|
|
|
FilesProvider $sourceFilesProvider, |
43
|
|
|
SniffClassesResolver $sniffProvider, |
44
|
|
|
SniffFactory $sniffFactory |
45
|
|
|
) { |
46
|
1 |
|
$this->sniffDispatcher = $sniffDispatcher; |
47
|
1 |
|
$this->filesProvider = $sourceFilesProvider; |
48
|
1 |
|
$this->sniffClassesResolver = $sniffProvider; |
49
|
1 |
|
$this->sniffFactory = $sniffFactory; |
50
|
|
|
|
51
|
1 |
|
$this->setupRequirements(); |
52
|
1 |
|
} |
53
|
|
|
|
54
|
1 |
|
public function runCommand(Php7CodeSnifferCommand $command) |
55
|
|
|
{ |
56
|
1 |
|
$this->registerSniffs( |
57
|
1 |
|
$command->getStandards(), |
58
|
1 |
|
$command->getSniffs(), |
59
|
1 |
|
$command->getExcludedSniffs() |
60
|
|
|
); |
61
|
|
|
|
62
|
1 |
|
$this->ensureSniffsAreRegistered(); |
63
|
|
|
|
64
|
|
|
$this->runForSource($command->getSource(), $command->isFixer()); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
private function registerSniffs(array $standards, array $extraSniffs, array $excludedSniffs) |
68
|
|
|
{ |
69
|
1 |
|
$sniffClasses = $this->sniffClassesResolver->resolveFromStandardsAndSniffs( |
70
|
|
|
$standards, |
71
|
|
|
$extraSniffs, |
72
|
|
|
$excludedSniffs |
73
|
|
|
); |
74
|
|
|
|
75
|
1 |
|
$sniffs = $this->sniffFactory->createFromSniffClassNames($sniffClasses); |
76
|
1 |
|
$this->sniffDispatcher->addSniffListeners($sniffs); |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
private function runForSource(array $source, bool $isFixer) |
80
|
|
|
{ |
81
|
|
|
$files = $this->filesProvider->getFilesForSource($source, $isFixer); |
82
|
|
|
|
83
|
|
|
foreach ($files as $file) { |
84
|
|
|
if ($isFixer) { |
85
|
|
|
$this->processFileWithFixer($file); |
86
|
|
|
} else { |
87
|
|
|
$this->processFile($file); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$file->cleanUp(); // todo: check performance influence |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function processFile(File $file) |
95
|
|
|
{ |
96
|
|
|
foreach ($file->getTokens() as $stackPointer => $token) { |
97
|
|
|
$this->sniffDispatcher->dispatch( |
98
|
|
|
$token['code'], |
99
|
|
|
new CheckFileTokenEvent($file, $stackPointer) |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function processFileWithFixer(File $file) |
105
|
|
|
{ |
106
|
|
|
// 1. puts tokens into fixer |
107
|
|
|
$file->fixer->startFile($file); |
108
|
|
|
|
109
|
|
|
// 2. run all Sniff fixers |
110
|
|
|
$this->processFile($file); |
111
|
|
|
|
112
|
|
|
// 3. load changes to tokens |
113
|
|
|
$file->fixer->endChangeset(); |
114
|
|
|
|
115
|
|
|
// 4. content has changed, save it! |
116
|
|
|
$newContent = $file->fixer->getContents(); |
117
|
|
|
|
118
|
|
|
file_put_contents($file->getFilename(), $newContent); |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
private function setupRequirements() |
122
|
|
|
{ |
123
|
1 |
|
$this->ensureLineEndingsAreDetected(); |
124
|
1 |
|
$this->setupVerbosityToMakeLegacyCodeRun(); |
125
|
1 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Ensure this option is enabled or else line endings will not always |
129
|
|
|
* be detected properly for files created on a Mac with the /r line ending. |
130
|
|
|
*/ |
131
|
1 |
|
private function ensureLineEndingsAreDetected() |
132
|
|
|
{ |
133
|
1 |
|
ini_set('auto_detect_line_endings', true); |
134
|
1 |
|
} |
135
|
|
|
|
136
|
1 |
|
private function setupVerbosityToMakeLegacyCodeRun() |
137
|
|
|
{ |
138
|
1 |
|
if (!defined('PHP_CODESNIFFER_VERBOSITY')) { |
139
|
|
|
define('PHP_CODESNIFFER_VERBOSITY', 0); |
140
|
|
|
} |
141
|
1 |
|
} |
142
|
|
|
|
143
|
1 |
|
private function ensureSniffsAreRegistered() |
144
|
|
|
{ |
145
|
1 |
|
$listeners = $this->sniffDispatcher->getListeners(); |
146
|
1 |
|
if ($listeners === []) { |
147
|
1 |
|
throw new AnySniffMissingException( |
148
|
1 |
|
'You need to specify some sniffs with "--standards=..." or "--sniffs=...".' |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|