|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Symplify |
|
7
|
|
|
* Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz). |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Symplify\MultiCodingStandard\PhpCsFixer\Application; |
|
11
|
|
|
|
|
12
|
|
|
use ArrayIterator; |
|
13
|
|
|
use Symfony\CS\Config; |
|
14
|
|
|
use Symfony\CS\Fixer; |
|
15
|
|
|
use Symplify\MultiCodingStandard\PhpCsFixer\Application\Command\RunApplicationCommand; |
|
16
|
|
|
use Symplify\MultiCodingStandard\PhpCsFixer\Factory\FixerFactory; |
|
17
|
|
|
use Symplify\MultiCodingStandard\PhpCsFixer\Report\DiffDataCollector; |
|
18
|
|
|
use Symplify\PHP7_CodeSniffer\File\Finder\SourceFinder; |
|
19
|
|
|
|
|
20
|
|
|
final class Application |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var Fixer |
|
24
|
|
|
*/ |
|
25
|
|
|
private $fixer; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var FixerFactory |
|
29
|
|
|
*/ |
|
30
|
|
|
private $fixerSetFactory; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var SourceFinder |
|
33
|
|
|
*/ |
|
34
|
|
|
private $sourceFinder; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var DiffDataCollector |
|
38
|
|
|
*/ |
|
39
|
|
|
private $diffDataCollector; |
|
40
|
|
|
|
|
41
|
|
|
public function __construct( |
|
42
|
|
|
Fixer $fixer, |
|
43
|
|
|
FixerFactory $fixerSetFactory, |
|
44
|
|
|
SourceFinder $sourceFinder, |
|
45
|
|
|
DiffDataCollector $diffDataCollector |
|
46
|
|
|
) { |
|
47
|
|
|
$this->fixer = $fixer; |
|
48
|
|
|
$this->fixerSetFactory = $fixerSetFactory; |
|
49
|
|
|
$this->sourceFinder = $sourceFinder; |
|
50
|
|
|
$this->diffDataCollector = $diffDataCollector; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function runCommand(RunApplicationCommand $command) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->registerFixersToFixer($command->getFixerLevels(), $command->getFixers(), $command->getExcludeFixers()); |
|
56
|
|
|
|
|
57
|
|
|
$this->runForSource($command->getSource(), $command->isFixer()); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
private function registerFixersToFixer(array $fixerLevels, array $fixers, array $excludedFixers) |
|
61
|
|
|
{ |
|
62
|
|
|
$fixers = $this->fixerSetFactory->createFromLevelsFixersAndExcludedFixers( |
|
63
|
|
|
$fixerLevels, $fixers, $excludedFixers |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
$this->fixer->registerCustomFixers($fixers); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
private function runForSource(array $source, bool $isFixer) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->registerSourceToFixer($source); |
|
72
|
|
|
|
|
73
|
|
|
/** @var Config $config */ |
|
74
|
|
|
$config = $this->fixer->getConfigs()[0]; |
|
75
|
|
|
$config->fixers($this->fixer->getFixers()); |
|
76
|
|
|
|
|
77
|
|
|
$changedDiffs = $this->fixer->fix($config, !$isFixer, !$isFixer); |
|
78
|
|
|
$this->diffDataCollector->setDiffs($changedDiffs); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function registerSourceToFixer(array $source) |
|
82
|
|
|
{ |
|
83
|
|
|
$files = $this->sourceFinder->find($source); |
|
84
|
|
|
|
|
85
|
|
|
$config = new Config(); |
|
86
|
|
|
$config->finder(new ArrayIterator($files)); |
|
87
|
|
|
$this->fixer->addConfig($config); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|