Completed
Push — master ( 52b8cf...72ebc8 )
by Michael
02:58
created

CLI::runphpcs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace SugaredRim\PHP\CodeSniffer;
4
5
use Schnittstabil\ComposerExtra\ComposerExtra;
6
use Schnittstabil\FinderByConfig\FinderByConfig;
7
8
class CLI extends \PHP_CodeSniffer_CLI
9
{
10
    protected $namespace = 'sugared-rim/php_codesniffer';
11
    protected $finderByConfig;
12
    protected $defaultConfig;
13
    protected $config;
14
15
    public function __construct(callable $finderByConfig = null)
16
    {
17
        if ($finderByConfig === null) {
18
            $finderByConfig = new FinderByConfig();
19
        }
20
        $this->finderByConfig = $finderByConfig;
21
22
        $this->defaultConfig = new \stdClass();
23
        $this->defaultConfig->presets = [
24
            'SugaredRim\\PHP\\CodeSniffer\\DefaultPreset::get',
25
        ];
26
    }
27
28
    protected function getConfig($path = null, $default = null)
29
    {
30
        if ($this->config === null) {
31
            $this->config = new ComposerExtra(
32
                $this->namespace,
33
                $this->defaultConfig,
34
                'presets'
35
            );
36
        }
37
38
        if ($path === null) {
39
            $path = array();
40
        }
41
42
        return $this->config->get($path, $default);
43
    }
44
45
    public function processLongArgument($arg, $pos)
46
    {
47
        if (substr($arg, 0, 10) === 'namespace=') {
48
            $this->namespace = substr($arg, 10);
49
50
            return;
51
        }
52
53
        parent::processLongArgument($arg, $pos);
54
    }
55
56
    public function setCommandLineValues($args)
57
    {
58
        parent::setCommandLineValues($args);
59
60
        if (!empty($this->values['files'])) {
61
            return;
62
        }
63
64
        $files = $this->getConfig('files', []);
65
66
        foreach (call_user_func($this->finderByConfig, $files) as $file) {
67
            $this->processUnknownArgument($file->getPathname(), -1);
68
        }
69
    }
70
71
    /**
72
     * @SuppressWarnings(PHPMD.StaticAccess)
73
     */
74
    protected function processConfig()
75
    {
76
        foreach ($this->getConfig() as $key => $value) {
77
            switch ($key) {
78
                case 'presets':
79
                case 'files':
80
                    continue 2;
81
                case 'default_standard':
82
                    if (is_array($value)) {
83
                        $value = implode(',', $value);
84
                    }
85
            }
86
87
            \PHP_CodeSniffer::setConfigData($key, $value, true);
88
        }
89
    }
90
91
    /**
92
     * @codeCoverageIgnore hard coded `exit()` in `parent::runphpcbf`
93
     */
94
    public function runphpcbf()
95
    {
96
        $this->processConfig();
97
        parent::runphpcbf();
98
    }
99
100
    /**
101
     * @codeCoverageIgnore hard coded `exit()` in `parent::runphpcs`
102
     */
103
    public function runphpcs()
104
    {
105
        $this->processConfig();
106
        parent::runphpcs();
107
    }
108
}
109