Completed
Push — master ( 3b5f38...c5ceed )
by Tomáš
04:59
created

SniffProvider::getActiveSniffs()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 12
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 0
crap 12
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);
0 ignored issues
show
Unused Code introduced by
The call to the method Symplify\PHP7_CodeSniffe...cludeRestrictedSniffs() seems un-needed as the method has no side-effects.

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:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

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:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

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:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
50
        }
51
52
        return $sniffs;
53
    }
54
55
    private function getSniffRestrictions() : array
56
    {
57
        return $this->configuration->getSniff();
58
//        $sniffRestrictions = [];
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
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