Completed
Branch master (3ef992)
by Michael
03:13
created

Command::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace SugaredRim\PHPUnit\TextUI;
4
5
use Psr\Log\LoggerInterface;
6
use fool\echolog\Echolog;
7
use Schnittstabil\ComposerExtra\ComposerExtra;
8
use Schnittstabil\Get\Get;
9
10
class Command extends \PHPUnit_TextUI_Command
11
{
12
    protected $argvRendererClass = Argv::class;
13
    protected $namespace = 'sugared-rim/phpunit';
14
    protected $defaultConfig;
15
    protected $logger;
16
    protected $config;
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function __construct(LoggerInterface $logger = null)
22
    {
23
        // @codeCoverageIgnoreStart
24
        if ($logger === null) {
25
            $logger = new Echolog();
26
        }
27
        // @codeCoverageIgnoreEnd
28
29
        $this->defaultConfig = new \stdClass();
30
        $this->defaultConfig->presets = ['SugaredRim\\PHPUnit\\DefaultPreset::get'];
31
32
        $this->logger = $logger;
33
34
        $this->longOptions['sugared-coverage-text-show-uncovered-files']
35
            = 'sugaredCoverageTextShowUncoveredFilesHandler';
36
        $this->longOptions['sugared-debug'] = 'sugaredDebugHandler';
37
        $this->longOptions['sugared-namespace='] = 'sugaredNamespaceHandler';
38
    }
39
40
    protected function logDebug($title, $value)
41
    {
42
        $this->logger->debug($title.': '.json_encode($value, JSON_PRETTY_PRINT));
43
    }
44
45
    protected function sugaredCoverageTextShowUncoveredFilesHandler()
46
    {
47
        $this->arguments['coverageTextShowUncoveredFiles'] = true;
48
    }
49
50
    protected function sugaredDebugHandler()
51
    {
52
        $this->arguments['sugaredDebug'] = true;
53
    }
54
55
    protected function sugaredNamespaceHandler($namespace)
56
    {
57
        $this->namespace = $namespace;
58
    }
59
60
    /**
61
     * @SuppressWarnings(PHPMD.StaticAccess)
62
     */
63
    protected function addListeners()
64
    {
65
        foreach ((array) $this->getConfig('sugared.listeners', []) as $listener) {
66
            $class = Get::value('class', $listener);
67
            $listenerClass = new \ReflectionClass($class);
68
            $arguments = json_decode(json_encode(Get::value('arguments', $listener, [])), true);
69
70
            $this->arguments['listeners'][] = $listenerClass->newInstanceArgs($arguments);
71
        }
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     *
77
     * @SuppressWarnings(PHPMD.StaticAccess)
78
     */
79
    public function handleArguments(array $argv)
80
    {
81
        parent::handleArguments($argv);
82
83
        if (Get::value('sugaredDebug', $this->arguments, false)) {
84
            $this->logDebug('Parsed arguments', $this->arguments);
85
        }
86
87
        $this->addListeners();
88
    }
89
90
    protected function getConfig($path = null, $default = null)
91
    {
92
        if ($this->config === null) {
93
            $this->config = new ComposerExtra(
94
                $this->namespace,
95
                $this->defaultConfig,
96
                'presets'
97
            );
98
        }
99
100
        if ($path === null) {
101
            $path = array();
102
        }
103
104
        return $this->config->get($path, $default);
105
    }
106
107
    protected function preprocessArgv(array $argv)
108
    {
109
        $config = $this->getConfig();
110
111
        if (in_array('--sugared-debug', $argv)) {
112
            $this->logDebug('Config', $config);
113
        }
114
115
        $argvRenderer = new $this->argvRendererClass($config);
116
117
        return $argvRenderer($argv);
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     *
123
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
124
     */
125
    public function run(array $argv, $exit = true)
126
    {
127
        $argv = $this->preprocessArgv($argv);
128
129
        if (in_array('--sugared-debug', $argv)) {
130
            $this->logDebug('Arguments', $argv);
131
        }
132
133
        parent::run($argv, $exit);
134
    }
135
}
136