|
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 PHP_CodeSniffer\Files\File; |
|
11
|
|
|
use Symplify\PHP7_CodeSniffer\EventDispatcher\Event\CheckFileTokenEvent; |
|
12
|
|
|
use Symplify\PHP7_CodeSniffer\EventDispatcher\SniffDispatcher; |
|
13
|
|
|
|
|
14
|
|
|
final class FileProcessor |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var SniffDispatcher |
|
18
|
|
|
*/ |
|
19
|
|
|
private $sniffDispatcher; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Fixer |
|
23
|
|
|
*/ |
|
24
|
|
|
private $fixer; |
|
25
|
|
|
|
|
26
|
1 |
|
public function __construct(SniffDispatcher $sniffDispatcher, Fixer $fixer) |
|
27
|
|
|
{ |
|
28
|
1 |
|
$this->sniffDispatcher = $sniffDispatcher; |
|
29
|
1 |
|
$this->fixer = $fixer; |
|
30
|
1 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function processFiles(array $files, bool $isFixer) |
|
33
|
|
|
{ |
|
34
|
|
|
foreach ($files as $file) { |
|
35
|
|
|
$this->processFile($file, $isFixer); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
private function processFile(File $file, bool $isFixer) |
|
40
|
|
|
{ |
|
41
|
|
|
if ($isFixer) { |
|
42
|
|
|
$this->processFileWithFixer($file); |
|
43
|
|
|
} else { |
|
44
|
|
|
$this->processFileWithoutFixer($file); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private function processFileWithFixer(File $file) |
|
49
|
|
|
{ |
|
50
|
|
|
// 1. puts tokens into fixer |
|
51
|
|
|
$file->fixer->startFile($file); |
|
52
|
|
|
|
|
53
|
|
|
// 2. run all Sniff fixers |
|
54
|
|
|
$this->processFileWithoutFixer($file); |
|
55
|
|
|
|
|
56
|
|
|
// 3. content has changed, save it! |
|
57
|
|
|
$newContent = $file->fixer->getContents(); |
|
58
|
|
|
|
|
59
|
|
|
file_put_contents($file->getFilename(), $newContent); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
private function processFileWithoutFixer(File $file) |
|
63
|
|
|
{ |
|
64
|
|
|
foreach ($file->getTokens() as $stackPointer => $token) { |
|
65
|
|
|
$this->sniffDispatcher->dispatch( |
|
66
|
|
|
$token['code'], |
|
67
|
|
|
new CheckFileTokenEvent($file, $stackPointer) |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|