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() |
60
|
|
|
{ |
61
|
5 |
|
$this->codeSniffer = new PHP_CodeSniffer(); |
62
|
5 |
|
$this->setupSniffs($this->configuration->getActiveSniffs()); |
63
|
5 |
|
$this->setupStandards($this->configuration->getActiveStandards()); |
64
|
5 |
|
$this->setupErrorRecoding(); |
65
|
|
|
|
66
|
5 |
|
return $this->codeSniffer; |
67
|
|
|
} |
68
|
|
|
|
69
|
5 |
|
private function setupSniffs(array $sniffs) |
70
|
|
|
{ |
71
|
5 |
|
$this->codeSniffer->registerSniffs($this->sniffFileSystem->findAllSniffs(), $sniffs); |
72
|
5 |
|
$this->codeSniffer->populateTokenListeners(); |
73
|
5 |
|
} |
74
|
|
|
|
75
|
5 |
|
private function setupStandards(array $standards) |
76
|
|
|
{ |
77
|
5 |
|
foreach ($standards as $standard) { |
78
|
2 |
|
$rulesetPath = $this->rulesetFileSystem->getRulesetPathForStandardName($standard); |
79
|
2 |
|
$sniffFilePaths = $this->codeSniffer->processRuleset($rulesetPath); |
80
|
2 |
|
$sniffFilePaths = $this->removeExcludedSniffs($sniffFilePaths); |
81
|
2 |
|
$this->codeSniffer->registerSniffs($sniffFilePaths, []); |
82
|
|
|
} |
83
|
5 |
|
} |
84
|
|
|
|
85
|
5 |
|
private function setupErrorRecoding() |
86
|
|
|
{ |
87
|
5 |
|
$this->codeSniffer->cli->setCommandLineValues([ |
88
|
5 |
|
'-s', // showSources must be on, so that errors are recorded |
89
|
|
|
]); |
90
|
5 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string[] $sniffFilePaths |
94
|
|
|
* @return string[] |
95
|
|
|
*/ |
96
|
2 |
|
private function removeExcludedSniffs(array $sniffFilePaths) |
97
|
|
|
{ |
98
|
2 |
|
$sniffFilePaths = $this->sniffNaming->detectDottedFromFilePaths($sniffFilePaths); |
99
|
2 |
|
foreach ($sniffFilePaths as $dottedName => $filePath) { |
100
|
2 |
|
if (in_array($dottedName, $this->configuration->getExcludedSniffs())) { |
101
|
2 |
|
unset($sniffFilePaths[$dottedName]); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
return $sniffFilePaths; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|