Completed
Push — master ( 74ff7b...2fd8e1 )
by Tomáš
03:25
created

Application   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 9
dl 0
loc 94
ccs 27
cts 36
cp 0.75
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A runCommand() 0 10 1
A createAndRegisterSniffsToSniffDispatcher() 0 6 1
A runForSource() 0 5 1
A ensureAtLeastOneSniffIsRegistered() 0 8 2
A resolveCommandConfiguration() 0 10 1
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\Application;
9
10
use Symplify\PHP7_CodeSniffer\Application\Command\RunApplicationCommand;
11
use Symplify\PHP7_CodeSniffer\Configuration\ConfigurationResolver;
12
use Symplify\PHP7_CodeSniffer\EventDispatcher\SniffDispatcher;
13
use Symplify\PHP7_CodeSniffer\Exception\AnySniffMissingException;
14
use Symplify\PHP7_CodeSniffer\File\Provider\FilesProvider;
15
use Symplify\PHP7_CodeSniffer\Legacy\LegacyConfiguration;
16
use Symplify\PHP7_CodeSniffer\Sniff\SniffSetFactory;
17
use Symplify\PHP7_CodeSniffer\Sniff\Xml\DataCollector\ExcludedSniffDataCollector;
18
19
final class Application
20
{
21
    /**
22
     * @var SniffDispatcher
23
     */
24
    private $sniffDispatcher;
25
26
    /**
27
     * @var FilesProvider
28
     */
29
    private $filesProvider;
30
31
    /**
32
     * @var SniffSetFactory
33
     */
34
    private $sniffSetFactory;
35
36
    /**
37
     * @var ExcludedSniffDataCollector
38
     */
39
    private $excludedSniffDataCollector;
40
41
    /**
42
     * @var ConfigurationResolver
43
     */
44
    private $configurationResolver;
45
46
    /**
47
     * @var FileProcessor
48
     */
49
    private $fileProcessor;
50
51 1
    public function __construct(
52
        SniffDispatcher $sniffDispatcher,
53
        FilesProvider $sourceFilesProvider,
54
        SniffSetFactory $sniffFactory,
55
        ExcludedSniffDataCollector $excludedSniffDataCollector,
56
        ConfigurationResolver $configurationResolver,
57
        FileProcessor $fileProcessor
58
    ) {
59 1
        $this->sniffDispatcher = $sniffDispatcher;
60 1
        $this->filesProvider = $sourceFilesProvider;
61 1
        $this->sniffSetFactory = $sniffFactory;
62 1
        $this->excludedSniffDataCollector = $excludedSniffDataCollector;
63 1
        $this->configurationResolver = $configurationResolver;
64 1
        $this->fileProcessor = $fileProcessor;
65
66 1
        LegacyConfiguration::setup();
67 1
    }
68
69 1
    public function runCommand(RunApplicationCommand $command)
70
    {
71 1
        $command = $this->resolveCommandConfiguration($command);
72
73 1
        $this->excludedSniffDataCollector->addExcludedSniffs($command->getExcludedSniffs());
74
75 1
        $this->createAndRegisterSniffsToSniffDispatcher($command->getStandards(), $command->getSniffs());
76
77
        $this->runForSource($command->getSource(), $command->isFixer());
78
    }
79
80 1
    private function createAndRegisterSniffsToSniffDispatcher(array $standards, array $extraSniffs)
81
    {
82 1
        $sniffs = $this->sniffSetFactory->createFromStandardsAndSniffs($standards, $extraSniffs);
83 1
        $this->ensureAtLeastOneSniffIsRegistered($sniffs);
84
        $this->sniffDispatcher->addSniffListeners($sniffs);
85
    }
86
87
    private function runForSource(array $source, bool $isFixer)
88
    {
89
        $files = $this->filesProvider->getFilesForSource($source, $isFixer);
90
        $this->fileProcessor->processFiles($files, $isFixer);
91
    }
92
93 1
    private function ensureAtLeastOneSniffIsRegistered(array $sniffs)
94
    {
95 1
        if (count($sniffs) < 1) {
96 1
            throw new AnySniffMissingException(
97 1
                'You need to specify some sniffs with "--standards=..." or "--sniffs=...".'
98
            );
99
        }
100
    }
101
102 1
    private function resolveCommandConfiguration(RunApplicationCommand $command) : RunApplicationCommand
103
    {
104 1
        return new RunApplicationCommand(
105 1
            $command->getSource(),
106 1
            $this->configurationResolver->resolve('standards', $command->getStandards()),
107 1
            $this->configurationResolver->resolve('sniffs', $command->getSniffs()),
108 1
            $this->configurationResolver->resolve('sniffs', $command->getExcludedSniffs()),
109 1
            $command->isFixer()
110
        );
111
    }
112
}
113