BaseCommand   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 4
dl 0
loc 90
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A logDebug() 0 6 2
A sugaredCoverageTextShowUncoveredFilesHandler() 0 4 1
A sugaredDebugHandler() 0 4 1
A sugaredNamespaceHandler() 0 4 1
A addListeners() 0 10 2
A getConfig() 0 16 3
A preprocessArgv() 0 12 2
1
<?php
2
3
namespace SugaredRim\PHPUnit\TextUI;
4
5
use Psr\Log\LoggerInterface;
6
use Schnittstabil\ComposerExtra\ComposerExtra;
7
use Schnittstabil\Get\Get;
8
9
class BaseCommand extends \PHPUnit\TextUI\Command
10
{
11
    protected $argvRendererClass = Argv::class;
12
    protected $namespace = 'sugared-rim/phpunit';
13
    protected $defaultConfig;
14
    protected $logger;
15
    protected $config;
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function __construct(LoggerInterface $logger = null)
21
    {
22
        $this->defaultConfig = new \stdClass();
23
        $this->defaultConfig->presets = ['SugaredRim\\PHPUnit\\DefaultPreset::get'];
24
25
        $this->logger = $logger;
26
27
        $this->longOptions['sugared-coverage-text-show-uncovered-files']
28
            = 'sugaredCoverageTextShowUncoveredFilesHandler';
29
        $this->longOptions['sugared-debug'] = 'sugaredDebugHandler';
30
        $this->longOptions['sugared-namespace='] = 'sugaredNamespaceHandler';
31
    }
32
33
    protected function logDebug($title, $value)
34
    {
35
        if ($this->logger !== null) {
36
            $this->logger->debug($title.': '.json_encode($value, JSON_PRETTY_PRINT));
37
        }
38
    }
39
40
    protected function sugaredCoverageTextShowUncoveredFilesHandler()
41
    {
42
        $this->arguments['coverageTextShowUncoveredFiles'] = true;
43
    }
44
45
    protected function sugaredDebugHandler()
46
    {
47
        $this->arguments['sugaredDebug'] = true;
48
    }
49
50
    protected function sugaredNamespaceHandler($namespace)
51
    {
52
        $this->namespace = $namespace;
53
    }
54
55
    /**
56
     * @SuppressWarnings(PHPMD.StaticAccess)
57
     */
58
    protected function addListeners()
59
    {
60
        foreach ((array) $this->getConfig('sugared.listeners', []) as $listener) {
61
            $class = Get::value('class', $listener);
62
            $listenerClass = new \ReflectionClass($class);
63
            $arguments = json_decode(json_encode(Get::value('arguments', $listener, [])), true);
64
65
            $this->arguments['listeners'][] = $listenerClass->newInstanceArgs($arguments);
66
        }
67
    }
68
69
    protected function getConfig($path = null, $default = null)
70
    {
71
        if ($this->config === null) {
72
            $this->config = new ComposerExtra(
73
                $this->namespace,
74
                $this->defaultConfig,
75
                'presets'
76
            );
77
        }
78
79
        if ($path === null) {
80
            $path = array();
81
        }
82
83
        return $this->config->get($path, $default);
84
    }
85
86
    protected function preprocessArgv(array $argv)
87
    {
88
        $config = $this->getConfig();
89
90
        if (in_array('--sugared-debug', $argv)) {
91
            $this->logDebug('Config', $config);
92
        }
93
94
        $argvRenderer = new $this->argvRendererClass($config);
95
96
        return $argvRenderer($argv);
97
    }
98
}
99