CommandLineOptions::parseDefaults()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.2
c 0
b 0
f 0
cc 4
eloc 12
nc 4
nop 2
1
<?php
2
3
namespace SugaredRim\PHPMD\TextUI;
4
5
class CommandLineOptions extends \PHPMD\TextUI\CommandLineOptions
6
{
7
    public function __construct(array $args, array $availableRuleSets = array(), $defaults = null)
8
    {
9
        $this->parseDefaults($args, $defaults);
10
11
        try {
12
            parent::__construct($args, $availableRuleSets);
13
        } catch (\InvalidArgumentException $err) {
14
            if ($err->getCode() !== self::INPUT_ERROR) {
15
                throw $err;
16
            }
17
        }
18
19
        $this->assertValidState();
20
    }
21
22
    protected function parseDefaults(&$args, $defaults = null)
23
    {
24
        if ($defaults === null) {
25
            return;
26
        }
27
28
        foreach (get_object_vars($defaults) as $k => $v) {
29
            switch ($k) {
30
                case 'inputfile':
31
                    // prepend inputPath arg
32
                    $script = array_shift($args);
33
                    array_unshift($args, $this->inputPath = $this->readInputFile($v));
34
                    array_unshift($args, $script);
35
                    break;
36
                default:
37
                    $this->$k = $v;
38
            }
39
        }
40
    }
41
42
    protected function assertValidState()
43
    {
44
        if (empty($this->inputPath) || empty($this->reportFormat) || empty($this->ruleSets)) {
45
            throw new \InvalidArgumentException($this->usage(), self::INPUT_ERROR);
46
        }
47
    }
48
}
49