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
|
2 |
|
public function __construct(SniffDispatcher $sniffDispatcher, Fixer $fixer) |
27
|
|
|
{ |
28
|
2 |
|
$this->sniffDispatcher = $sniffDispatcher; |
29
|
2 |
|
$this->fixer = $fixer; |
30
|
2 |
|
} |
31
|
|
|
|
32
|
1 |
|
public function processFiles(array $files, bool $isFixer) |
33
|
|
|
{ |
34
|
1 |
|
foreach ($files as $file) { |
35
|
1 |
|
$this->processFile($file, $isFixer); |
36
|
|
|
} |
37
|
1 |
|
} |
38
|
|
|
|
39
|
1 |
|
private function processFile(File $file, bool $isFixer) |
40
|
|
|
{ |
41
|
1 |
|
if ($isFixer) { |
42
|
1 |
|
$this->processFileWithFixer($file); |
43
|
|
|
} else { |
44
|
1 |
|
$this->processFileWithoutFixer($file); |
45
|
|
|
} |
46
|
1 |
|
} |
47
|
|
|
|
48
|
1 |
|
private function processFileWithFixer(File $file) |
49
|
|
|
{ |
50
|
|
|
// 1. puts tokens into fixer |
51
|
1 |
|
$file->fixer->startFile($file); |
52
|
|
|
|
53
|
|
|
// 2. run all Sniff fixers |
54
|
1 |
|
$this->processFileWithoutFixer($file); |
55
|
|
|
|
56
|
|
|
// 3. content has changed, save it! |
57
|
1 |
|
$newContent = $file->fixer->getContents(); |
58
|
|
|
|
59
|
1 |
|
file_put_contents($file->getFilename(), $newContent); |
60
|
1 |
|
} |
61
|
|
|
|
62
|
1 |
|
private function processFileWithoutFixer(File $file) |
63
|
|
|
{ |
64
|
1 |
|
foreach ($file->getTokens() as $stackPointer => $token) { |
65
|
1 |
|
$this->sniffDispatcher->dispatch( |
66
|
1 |
|
$token['code'], |
67
|
1 |
|
new CheckFileTokenEvent($file, $stackPointer) |
68
|
|
|
); |
69
|
|
|
} |
70
|
1 |
|
} |
71
|
|
|
} |
72
|
|
|
|