1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Funivan\Cs\FileProcessor; |
4
|
|
|
|
5
|
|
|
use Funivan\Cs\FileFinder\FileInfoCollection; |
6
|
|
|
use Funivan\Cs\Report\Report; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author Ivan Shcherbak <[email protected]> 2016 |
10
|
|
|
*/ |
11
|
|
|
class FixerProcessor extends BaseFileProcessor { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var bool |
15
|
|
|
*/ |
16
|
|
|
private $saveFiles = false; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param FileInfoCollection $files |
21
|
|
|
* @param Report $report |
22
|
|
|
*/ |
23
|
|
|
public function process(FileInfoCollection $files, Report $report) { |
24
|
|
|
|
25
|
|
|
if (empty($this->getTools())) { |
26
|
|
|
$this->getOutput()->writeln('<comment>Empty tools list</comment>'); |
27
|
|
|
return; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$message = '✘ dry run '; |
31
|
|
|
if ($this->saveFiles) { |
32
|
|
|
$message = '✔ fixed '; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
foreach ($files as $file) { |
36
|
|
|
|
37
|
|
|
$fixed = false; |
38
|
|
|
|
39
|
|
|
foreach ($this->getTools() as $tool) { |
40
|
|
|
|
41
|
|
|
if ($this->getOutput()->isDebug()) { |
42
|
|
|
$this->getOutput()->writeln('process : (' . $tool->getName() . ') ' . $file->getPath()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (!$tool->canProcess($file)) { |
46
|
|
|
continue; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$tool->process($file, $report); |
50
|
|
|
|
51
|
|
|
$fileChanged = $file->getContent()->isChanged(); |
52
|
|
|
if ($fileChanged === false) { |
53
|
|
|
continue; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$fixed = true; |
57
|
|
|
|
58
|
|
|
$filePathInfo = ''; |
59
|
|
|
if ($this->saveFiles === false) { |
60
|
|
|
$filePathInfo = ' ## ' . $file->getPath(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->getOutput()->writeln('<info>' . $message . ': ' . $tool->getDescription() . $filePathInfo . '</info>'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($fixed and $this->saveFiles) { |
67
|
|
|
$file->save(); |
68
|
|
|
$this->getOutput()->writeln('<info>' . '✔ saved : ' . $file->getPath() . '</info>'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return boolean |
78
|
|
|
*/ |
79
|
|
|
public function getSaveFiles() { |
80
|
|
|
return $this->saveFiles; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param boolean $saveFiles |
86
|
|
|
*/ |
87
|
|
|
public function setSaveFiles($saveFiles) { |
88
|
|
|
$this->saveFiles = $saveFiles; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
} |