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 Symfony\Component\EventDispatcher\EventDispatcherInterface; |
11
|
|
|
use Symplify\PHP7_CodeSniffer\Configuration\Configuration; |
12
|
|
|
use Symplify\PHP7_CodeSniffer\Console\Progress\ShowProgress; |
13
|
|
|
use Symplify\PHP7_CodeSniffer\Event\CheckFileTokenEvent; |
14
|
|
|
use Symplify\PHP7_CodeSniffer\File\File; |
15
|
|
|
|
16
|
|
|
final class Php7CodeSniffer |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
const VERSION = '4.0.0'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Configuration |
25
|
|
|
*/ |
26
|
|
|
private $configuration; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var EventDispatcherInterface |
30
|
|
|
*/ |
31
|
|
|
private $eventDispatcher; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ShowProgress |
35
|
|
|
*/ |
36
|
|
|
private $showProgress; |
37
|
|
|
|
38
|
|
|
public function __construct( |
39
|
|
|
Configuration $configuration, |
40
|
|
|
EventDispatcherInterface $eventDispatcher, |
41
|
|
|
ShowProgress $showProgress |
42
|
|
|
) { |
43
|
|
|
$this->configuration = $configuration; |
44
|
|
|
$this->eventDispatcher = $eventDispatcher; |
45
|
|
|
$this->showProgress = $showProgress; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function runForFiles(array $files) |
49
|
|
|
{ |
50
|
|
|
foreach ($files as $file) { |
51
|
|
|
if ($this->configuration->isFixer()) { |
52
|
|
|
$this->processFileWithFixer($file); |
53
|
|
|
} else { |
54
|
|
|
$this->processFile($file); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$file->cleanUp(); |
58
|
|
|
$this->showProgress->advance(); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function processFile(File $file) |
63
|
|
|
{ |
64
|
|
|
foreach ($file->getTokens() as $stackPointer => $token) { |
65
|
|
|
$this->eventDispatcher->dispatch($token['code'], new CheckFileTokenEvent($file, $stackPointer)); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function processFileWithFixer(File $file) |
70
|
|
|
{ |
71
|
|
|
// 1. puts tokens into fixer |
72
|
|
|
$file->fixer->startFile($file); |
73
|
|
|
|
74
|
|
|
// 2. run all Sniff fixers |
75
|
|
|
$this->processFile($file); |
76
|
|
|
|
77
|
|
|
// 3. load changes to tokens |
78
|
|
|
$file->fixer->endChangeset(); |
79
|
|
|
|
80
|
|
|
// 4. content has changed, save it! |
81
|
|
|
$newContent = $file->fixer->getContents(); |
82
|
|
|
|
83
|
|
|
file_put_contents($file->getFilename(), $newContent); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|