|
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\MultiCodingStandard\CodeSniffer; |
|
9
|
|
|
|
|
10
|
|
|
use PHP_CodeSniffer; |
|
11
|
|
|
use Symplify\MultiCodingStandard\Contract\CodeSniffer\CodeSnifferFactoryInterface; |
|
12
|
|
|
use Symplify\MultiCodingStandard\Contract\CodeSniffer\FileSystem\RulesetFileSystemInterface; |
|
13
|
|
|
use Symplify\MultiCodingStandard\Contract\CodeSniffer\FileSystem\SniffFileSystemInterface; |
|
14
|
|
|
use Symplify\MultiCodingStandard\Contract\CodeSniffer\Naming\SniffNamingInterface; |
|
15
|
|
|
use Symplify\MultiCodingStandard\Contract\Configuration\ConfigurationInterface; |
|
16
|
|
|
|
|
17
|
|
|
final class CodeSnifferFactory implements CodeSnifferFactoryInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var ConfigurationInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $configuration; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var SniffFileSystemInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $sniffFileSystem; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var RulesetFileSystemInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $rulesetFileSystem; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var SniffNamingInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $sniffNaming; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var PHP_CodeSniffer |
|
41
|
|
|
*/ |
|
42
|
|
|
private $codeSniffer; |
|
43
|
|
|
|
|
44
|
5 |
|
public function __construct( |
|
45
|
|
|
ConfigurationInterface $configuration, |
|
46
|
|
|
SniffFileSystemInterface $sniffFileSystem, |
|
47
|
|
|
SniffNamingInterface $sniffNaming, |
|
48
|
|
|
RulesetFileSystemInterface $rulesetFileSystem |
|
49
|
|
|
) { |
|
50
|
5 |
|
$this->configuration = $configuration; |
|
51
|
5 |
|
$this->sniffFileSystem = $sniffFileSystem; |
|
52
|
5 |
|
$this->sniffNaming = $sniffNaming; |
|
53
|
5 |
|
$this->rulesetFileSystem = $rulesetFileSystem; |
|
54
|
5 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
5 |
|
public function create($useBeautifier = false) |
|
60
|
|
|
{ |
|
61
|
5 |
|
if ($useBeautifier) { |
|
62
|
|
|
$this->codeSniffer = new CodeBeautifier; |
|
63
|
|
|
} else { |
|
64
|
5 |
|
$this->codeSniffer = new PHP_CodeSniffer(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$this->setupSniffs($this->configuration->getActiveSniffs()); |
|
68
|
|
|
$this->setupStandards($this->configuration->getActiveStandards()); |
|
69
|
|
|
$this->setupErrorRecoding(); |
|
70
|
|
|
|
|
71
|
|
|
return $this->codeSniffer; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
private function setupSniffs(array $sniffs) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->codeSniffer->registerSniffs($this->sniffFileSystem->findAllSniffs(), $sniffs); |
|
77
|
|
|
$this->codeSniffer->populateTokenListeners(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
private function setupStandards(array $standards) |
|
81
|
|
|
{ |
|
82
|
|
|
foreach ($standards as $standard) { |
|
83
|
|
|
$rulesetPath = $this->rulesetFileSystem->getRulesetPathForStandardName($standard); |
|
84
|
|
|
$sniffFilePaths = $this->codeSniffer->processRuleset($rulesetPath); |
|
85
|
|
|
$sniffFilePaths = $this->removeExcludedSniffs($sniffFilePaths); |
|
86
|
|
|
$this->codeSniffer->registerSniffs($sniffFilePaths, []); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
private function setupErrorRecoding() |
|
91
|
|
|
{ |
|
92
|
|
|
$this->codeSniffer->cli->setCommandLineValues([ |
|
93
|
|
|
'-s', // showSources must be on, so that errors are recorded |
|
94
|
|
|
]); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param string[] $sniffFilePaths |
|
99
|
|
|
* @return string[] |
|
100
|
|
|
*/ |
|
101
|
|
|
private function removeExcludedSniffs(array $sniffFilePaths) |
|
102
|
|
|
{ |
|
103
|
|
|
$sniffFilePaths = $this->sniffNaming->detectDottedFromFilePaths($sniffFilePaths); |
|
104
|
|
|
foreach ($sniffFilePaths as $dottedName => $filePath) { |
|
105
|
|
|
if (in_array($dottedName, $this->configuration->getExcludedSniffs())) { |
|
106
|
|
|
unset($sniffFilePaths[$dottedName]); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $sniffFilePaths; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|