Completed
Pull Request — master (#75)
by Greg
02:15
created

CommandData::getArgsAndOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
namespace Consolidation\AnnotatedCommand;
3
4
use Symfony\Component\Console\Input\InputInterface;
5
use Symfony\Component\Console\Output\OutputInterface;
6
7
class CommandData
8
{
9
    /** var AnnotationData */
10
    protected $annotationData;
11
    /** var InputInterface */
12
    protected $input;
13
    /** var OutputInterface */
14
    protected $output;
15
    /** var boolean */
16
    protected $usesInputInterface;
17
    /** var boolean */
18
    protected $usesOutputInterface;
19
    /** var boolean */
20
    protected $includeOptionsInArgs;
21
    /** var string */
22
    protected $filePath;
23
24
    public function __construct(
25
        AnnotationData $annotationData,
26
        InputInterface $input,
27
        OutputInterface $output,
28
        $usesInputInterface = false,
0 ignored issues
show
Unused Code introduced by
The parameter $usesInputInterface is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
29
        $usesOutputInterface = false
0 ignored issues
show
Unused Code introduced by
The parameter $usesOutputInterface is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
30
    ) {
31
        $this->annotationData = $annotationData;
32
        $this->input = $input;
33
        $this->output = $output;
34
        $this->usesInputInterface = false;
35
        $this->usesOutputInterface = false;
36
        $this->includeOptionsInArgs = true;
37
    }
38
39
    /**
40
     * For internal use only; indicates that the function to be called
41
     * should be passed an InputInterface &/or an OutputInterface.
42
     * @param booean $usesInputInterface
43
     * @param boolean $usesOutputInterface
44
     * @return self
45
     */
46
    public function setUseIOInterfaces($usesInputInterface, $usesOutputInterface)
47
    {
48
        $this->usesInputInterface = $usesInputInterface;
49
        $this->usesOutputInterface = $usesOutputInterface;
50
        return $this;
51
    }
52
53
    /**
54
     * For backwards-compatibility mode only: disable addition of
55
     * options on the end of the arguments list.
56
     */
57
    public function setIncludeOptionsInArgs($includeOptionsInArgs)
58
    {
59
        $this->includeOptionsInArgs = $includeOptionsInArgs;
60
        return $this;
61
    }
62
63
    public function annotationData()
64
    {
65
        return $this->annotationData;
66
    }
67
68
    public function input()
69
    {
70
        return $this->input;
71
    }
72
73
    public function output()
74
    {
75
        return $this->output;
76
    }
77
78
    public function arguments()
79
    {
80
        return $this->input->getArguments();
81
    }
82
83
    public function options()
84
    {
85
        return $this->input->getOptions();
86
    }
87
88
    // TODO: This should be a required constructor argument, but
89
    // implementing it this way to maintain backwards compatibility.
90
    public function setFilePath($filePath)
91
    {
92
        $this->filePath = $filePath;
93
    }
94
95
    public function filePath()
96
    {
97
        return $this->filePath;
98
    }
99
100
    public function getArgsWithoutAppName()
101
    {
102
        $args = $this->arguments();
103
104
        // When called via the Application, the first argument
105
        // will be the command name. The Application alters the
106
        // input definition to match, adding a 'command' argument
107
        // to the beginning.
108
        array_shift($args);
109
110
        if ($this->usesInputInterface) {
111
            array_unshift($args, $this->input());
112
        }
113
114
        if ($this->usesOutputInterface) {
115
            array_unshift($args, $this->output());
116
        }
117
118
        return $args;
119
    }
120
121
    public function getArgsAndOptions()
122
    {
123
        // Get passthrough args, and add the options on the end.
124
        $args = $this->getArgsWithoutAppName();
125
        if ($this->includeOptionsInArgs) {
126
            $args['options'] = $this->options();
127
        }
128
        return $args;
129
    }
130
}
131