Completed
Pull Request — master (#10)
by Tomáš
24:14
created

CodeSnifferFactory::setupStandards()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 9
ccs 0
cts 7
cp 0
rs 9.6666
cc 2
eloc 6
nc 2
nop 1
crap 6
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();
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
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);
0 ignored issues
show
Bug introduced by
The method registerSniffs() does not seem to exist on object<Symplify\PHP7_CodeSniffer\Php7CodeSniffer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
        $this->codeSniffer->populateTokenListeners();
0 ignored issues
show
Bug introduced by
The method populateTokenListeners() does not seem to exist on object<Symplify\PHP7_CodeSniffer\Php7CodeSniffer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method processRuleset() does not seem to exist on object<Symplify\PHP7_CodeSniffer\Php7CodeSniffer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
            $sniffFilePaths = $this->removeExcludedSniffs($sniffFilePaths);
83
            $this->codeSniffer->registerSniffs($sniffFilePaths, []);
0 ignored issues
show
Bug introduced by
The method registerSniffs() does not seem to exist on object<Symplify\PHP7_CodeSniffer\Php7CodeSniffer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

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