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\Configuration\ConfigurationInterface; |
15
|
|
|
|
16
|
|
|
final class CodeSnifferFactory implements CodeSnifferFactoryInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var ConfigurationInterface |
20
|
|
|
*/ |
21
|
|
|
private $configuration; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var SniffFileSystemInterface |
25
|
|
|
*/ |
26
|
|
|
private $sniffFileSystem; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var RulesetFileSystemInterface |
30
|
|
|
*/ |
31
|
|
|
private $rulesetFileSystem; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var PHP_CodeSniffer |
35
|
|
|
*/ |
36
|
|
|
private $codeSniffer; |
37
|
|
|
|
38
|
4 |
|
public function __construct( |
39
|
|
|
ConfigurationInterface $configuration, |
40
|
|
|
SniffFileSystemInterface $sniffFileSystem, |
41
|
|
|
RulesetFileSystemInterface $rulesetFileSystem |
42
|
|
|
) { |
43
|
4 |
|
$this->configuration = $configuration; |
44
|
4 |
|
$this->sniffFileSystem = $sniffFileSystem; |
45
|
4 |
|
$this->rulesetFileSystem = $rulesetFileSystem; |
46
|
4 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
4 |
|
public function create() |
52
|
|
|
{ |
53
|
4 |
|
$this->codeSniffer = new PHP_CodeSniffer(); |
54
|
4 |
|
$this->setupSniffs($this->configuration->getActiveSniffs()); |
55
|
4 |
|
$this->setupStandards($this->configuration->getActiveStandards()); |
56
|
4 |
|
$this->setupErrorRecoding(); |
57
|
|
|
|
58
|
4 |
|
return $this->codeSniffer; |
59
|
|
|
} |
60
|
|
|
|
61
|
4 |
|
private function setupSniffs(array $sniffs) |
62
|
|
|
{ |
63
|
4 |
|
$this->codeSniffer->registerSniffs($this->sniffFileSystem->findAllSniffs(), $sniffs); |
64
|
4 |
|
$this->codeSniffer->populateTokenListeners(); |
65
|
4 |
|
} |
66
|
|
|
|
67
|
4 |
|
private function setupStandards(array $standards) |
68
|
|
|
{ |
69
|
4 |
|
foreach ($standards as $standard) { |
70
|
4 |
|
$rulesetPath = $this->rulesetFileSystem->getRulesetPathForStandardName($standard); |
71
|
4 |
|
$sniffs = $this->codeSniffer->processRuleset($rulesetPath); |
72
|
4 |
|
$this->codeSniffer->registerSniffs($sniffs, []); |
73
|
|
|
} |
74
|
4 |
|
} |
75
|
|
|
|
76
|
4 |
|
private function setupErrorRecoding() |
77
|
|
|
{ |
78
|
4 |
|
$this->codeSniffer->cli->setCommandLineValues([ |
79
|
4 |
|
'-s', // showSources must be on, so that errors are recorded |
80
|
|
|
]); |
81
|
4 |
|
} |
82
|
|
|
} |
83
|
|
|
|