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\PHP7_CodeSniffer\SniffFinder; |
9
|
|
|
|
10
|
|
|
use Symplify\PHP7_CodeSniffer\Configuration\Configuration; |
11
|
|
|
use Symplify\PHP7_CodeSniffer\Ruleset\Routing\Router; |
12
|
|
|
use Symplify\PHP7_CodeSniffer\Ruleset\RulesetBuilder; |
13
|
|
|
use Symplify\PHP7_CodeSniffer\SniffFinder\Composer\VendorDirProvider; |
14
|
|
|
use Symplify\PHP7_CodeSniffer\SniffFinder\Contract\SniffFinderInterface; |
15
|
|
|
|
16
|
|
|
final class SniffProvider |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var Configuration |
20
|
|
|
*/ |
21
|
|
|
private $configuration; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var RulesetBuilder |
25
|
|
|
*/ |
26
|
|
|
private $rulesetBuilder; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Router |
30
|
|
|
*/ |
31
|
|
|
private $router; |
32
|
|
|
|
33
|
|
|
public function __construct(Configuration $configuration, RulesetBuilder $rulesetBuilder, Router $router) |
34
|
|
|
{ |
35
|
|
|
$this->configuration = $configuration; |
36
|
|
|
$this->rulesetBuilder = $rulesetBuilder; |
37
|
|
|
$this->router = $router; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getActiveSniffs() : array |
41
|
|
|
{ |
42
|
|
|
$sniffs = []; |
43
|
|
|
foreach ($this->configuration->getStandards() as $name => $rulesetXmlPath) { |
44
|
|
|
$newSniffs = $this->rulesetBuilder->buildFromRulesetXml($rulesetXmlPath); |
45
|
|
|
$sniffs = array_merge($sniffs, $newSniffs); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if ($sniffRestrictions = $this->getSniffRestrictions()) { |
49
|
|
|
$this->excludeRestrictedSniffs($sniffs, $sniffRestrictions); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $sniffs; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function getSniffRestrictions() : array |
56
|
|
|
{ |
57
|
|
|
return $this->configuration->getSniff(); |
58
|
|
|
// $sniffRestrictions = []; |
|
|
|
|
59
|
|
|
// foreach ($this->configuration->getSniff() as $sniffName) { |
60
|
|
|
// $sniffClass = $this->router->getClassFromSniffName($sniffName); |
61
|
|
|
// $sniffRestrictions[$sniffName] = $sniffClass; |
62
|
|
|
// } |
63
|
|
|
// |
64
|
|
|
// return $sniffRestrictions; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function excludeRestrictedSniffs(array $sniffs, array $sniffsToBeKept) |
68
|
|
|
{ |
69
|
|
|
$finalSniffs = []; |
70
|
|
|
foreach ($sniffs as $sniffCode => $sniffClass) { |
71
|
|
|
if (isset($sniffsToBeKept[$sniffCode]) === false) { |
72
|
|
|
continue; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$finalSniffs[$sniffCode] = $sniffClass; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $finalSniffs; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: