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\FileSystem\RulesetFileSystemInterface; |
12
|
|
|
use Symplify\MultiCodingStandard\Contract\CodeSniffer\FileSystem\SniffFileSystemInterface; |
13
|
|
|
use Symplify\MultiCodingStandard\Contract\CodeSniffer\Naming\SniffNamingInterface; |
14
|
|
|
use Symplify\MultiCodingStandard\Contract\Configuration\ConfigurationInterface; |
15
|
|
|
use Symplify\PHP7_CodeSniffer\Php7CodeSniffer; |
16
|
|
|
|
17
|
|
|
final class CodeSnifferFactory |
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 Php7CodeSniffer |
41
|
|
|
*/ |
42
|
|
|
private $codeSniffer; |
43
|
|
|
|
44
|
|
|
public function __construct( |
45
|
|
|
ConfigurationInterface $configuration, |
46
|
|
|
SniffFileSystemInterface $sniffFileSystem, |
47
|
|
|
SniffNamingInterface $sniffNaming, |
48
|
|
|
RulesetFileSystemInterface $rulesetFileSystem |
49
|
|
|
) { |
50
|
|
|
$this->configuration = $configuration; |
51
|
|
|
$this->sniffFileSystem = $sniffFileSystem; |
52
|
|
|
$this->sniffNaming = $sniffNaming; |
53
|
|
|
$this->rulesetFileSystem = $rulesetFileSystem; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function create() |
60
|
|
|
{ |
61
|
|
|
// $this->codeSniffer = new Php7CodeSniffer(); |
|
|
|
|
62
|
|
|
// dump($codeSniffer); |
63
|
|
|
// die; |
64
|
|
|
|
65
|
|
|
$this->setupSniffs($this->configuration->getActiveSniffs()); |
66
|
|
|
$this->setupStandards($this->configuration->getActiveStandards()); |
67
|
|
|
|
68
|
|
|
return $this->codeSniffer; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function setupSniffs(array $sniffs) |
72
|
|
|
{ |
73
|
|
|
$this->codeSniffer->registerSniffs($this->sniffFileSystem->findAllSniffs(), $sniffs); |
|
|
|
|
74
|
|
|
$this->codeSniffer->populateTokenListeners(); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function setupStandards(array $standards) |
78
|
|
|
{ |
79
|
|
|
foreach ($standards as $standard) { |
80
|
|
|
$rulesetPath = $this->rulesetFileSystem->getRulesetPathForStandardName($standard); |
81
|
|
|
$sniffFilePaths = $this->codeSniffer->processRuleset($rulesetPath); |
|
|
|
|
82
|
|
|
$sniffFilePaths = $this->removeExcludedSniffs($sniffFilePaths); |
83
|
|
|
$this->codeSniffer->registerSniffs($sniffFilePaths, []); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string[] $sniffFilePaths |
89
|
|
|
* @return string[] |
90
|
|
|
*/ |
91
|
|
|
private function removeExcludedSniffs(array $sniffFilePaths) |
92
|
|
|
{ |
93
|
|
|
$sniffFilePaths = $this->sniffNaming->detectDottedFromFilePaths($sniffFilePaths); |
94
|
|
|
foreach ($sniffFilePaths as $dottedName => $filePath) { |
95
|
|
|
if (in_array($dottedName, $this->configuration->getExcludedSniffs())) { |
96
|
|
|
unset($sniffFilePaths[$dottedName]); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $sniffFilePaths; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.