This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * See class comment |
||
4 | * |
||
5 | * PHP Version 5 |
||
6 | * |
||
7 | * @category Netresearch |
||
8 | * @package Netresearch\Kite |
||
9 | * @subpackage Task |
||
10 | * @author Christian Opitz <[email protected]> |
||
11 | * @license http://www.netresearch.de Netresearch Copyright |
||
12 | * @link http://www.netresearch.de |
||
13 | */ |
||
14 | |||
15 | namespace Netresearch\Kite\Task; |
||
16 | use Netresearch\Kite\Exception; |
||
17 | |||
18 | /** |
||
19 | * Executes a command locally and returns the output |
||
20 | * |
||
21 | * @category Netresearch |
||
22 | * @package Netresearch\Kite |
||
23 | * @subpackage Task |
||
24 | * @author Christian Opitz <[email protected]> |
||
25 | * @license http://www.netresearch.de Netresearch Copyright |
||
26 | * @link http://www.netresearch.de |
||
27 | */ |
||
28 | class ShellTask extends \Netresearch\Kite\Task |
||
29 | { |
||
30 | /** |
||
31 | * Configure the options |
||
32 | * |
||
33 | * @return array |
||
34 | */ |
||
35 | protected function configureVariables() |
||
36 | { |
||
37 | return array( |
||
0 ignored issues
–
show
|
|||
38 | 'command' => array( |
||
39 | 'type' => 'string|array', |
||
40 | 'label' => 'Command(s) to execute', |
||
41 | 'required' => true, |
||
42 | ), |
||
43 | 'cwd' => array( |
||
44 | 'type' => 'string', |
||
45 | 'label' => 'The directory to change to before running the command' |
||
46 | ), |
||
47 | 'argv' => array( |
||
48 | 'type' => 'array|string', |
||
49 | 'label' => 'String with all options and arguments for the command or an array in the same format as $argv. ' |
||
50 | . 'Attention: Values won\'t be escaped!' |
||
51 | ), |
||
52 | 'options' => array( |
||
53 | 'type' => 'array', |
||
54 | 'default' => array(), |
||
55 | 'label' => 'Array with options: Elements with numeric keys or bool true values will be --switches.' |
||
56 | ), |
||
57 | 'arguments' => array( |
||
58 | 'type' => 'array', |
||
59 | 'default' => array(), |
||
60 | 'label' => 'Arguments to pass to the cmd' |
||
61 | ), |
||
62 | 'optArg' => array( |
||
63 | 'type' => 'array|string', |
||
64 | 'label' => 'Arguments and options in one array. ' |
||
65 | . 'When array, elements with numeric keys will be added as {@see arguments} and elements with string keys will be added as {@see options}. ' |
||
66 | . 'When string, {@see argv} will be set to this value' |
||
67 | ), |
||
68 | 'errorMessage' => array( |
||
69 | 'type' => 'string', |
||
70 | 'label' => 'Message to display when the command failed' |
||
71 | ), |
||
72 | 'processSettings' => array( |
||
73 | 'type' => 'array', |
||
74 | 'default' => array(), |
||
75 | 'label' => 'Settings for symfony process class' |
||
76 | ), |
||
77 | '--', |
||
78 | ) + parent::configureVariables(); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Handle arguments, options and optArg |
||
83 | * |
||
84 | * @param string $option Option name |
||
85 | * @param mixed $value Option value |
||
86 | * |
||
87 | * @return void |
||
88 | */ |
||
89 | public function offsetSet($option, $value) |
||
90 | { |
||
91 | if ($option === 'processSettings') { |
||
92 | $value = array_merge($this->offsetGet('processSettings'), $value); |
||
93 | } |
||
94 | if (in_array($option, array('arguments', 'options', 'optArg'), true)) { |
||
95 | if ($value === null) { |
||
96 | if ($option === 'optArg' || $option === 'options') { |
||
97 | parent::offsetSet('options', array()); |
||
98 | } |
||
99 | if ($option === 'optArg' || $option === 'arguments') { |
||
100 | parent::offsetSet('arguments', array()); |
||
101 | } |
||
102 | return; |
||
103 | } |
||
104 | if ($option === 'optArg' && is_string($value)) { |
||
105 | parent::offsetSet('argv', $value); |
||
106 | return; |
||
107 | } |
||
108 | $arguments = $this->get('arguments'); |
||
109 | $options = $this->get('options'); |
||
110 | if ($option == 'arguments') { |
||
111 | $arguments = array_merge($arguments, $value); |
||
112 | } elseif ($option == 'options') { |
||
113 | View Code Duplication | foreach ($value as $k => $v) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
114 | if (is_numeric($k)) { |
||
115 | $options[$v] = true; |
||
116 | } else { |
||
117 | $options[$k] = $v; |
||
118 | } |
||
119 | } |
||
120 | } elseif ($option == 'optArg') { |
||
121 | View Code Duplication | foreach ($value as $k => $v) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
122 | if (is_numeric($k)) { |
||
123 | $arguments[] = $v; |
||
124 | } else { |
||
125 | $options[$k] = $v; |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | parent::offsetSet('arguments', $arguments); |
||
130 | parent::offsetSet('options', $options); |
||
131 | return; |
||
132 | } |
||
133 | parent::offsetSet($option, $value); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Execute the command |
||
138 | * |
||
139 | * @return mixed |
||
140 | */ |
||
141 | protected function executeCommand() |
||
142 | { |
||
143 | $process = $this->console->createProcess($this->getCommand(), $this->get('cwd')); |
||
144 | $process->setDryRun(!$this->shouldExecute()); |
||
145 | foreach ($this->get('processSettings') as $key => $value) { |
||
146 | $process->{'set' . ucfirst($key)}($value); |
||
147 | } |
||
148 | return $process->run(); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Get the command with options and arguments |
||
153 | * |
||
154 | * @return string |
||
155 | */ |
||
156 | protected function getCommand() |
||
157 | { |
||
158 | $cmd = $this->get('command'); |
||
159 | $argv = $this->get('argv'); |
||
160 | $options = $this->get('options'); |
||
161 | $arguments = $this->get('arguments'); |
||
162 | if (is_array($cmd)) { |
||
163 | if ($argv || $options || $arguments) { |
||
164 | throw new Exception('Can not use argv, options or arguments on multiple commands'); |
||
165 | } |
||
166 | $cmd = $this->expand(implode('; ', $cmd)); |
||
167 | } else { |
||
168 | if ($argv) { |
||
169 | if ($options || $arguments) { |
||
170 | throw new Exception('Can not combine argv with options or arguments'); |
||
171 | } |
||
172 | $cmd .= ' ' . (is_array($argv) ? implode(' ', $argv) : $argv); |
||
173 | } else { |
||
174 | $cmd .= $this->renderOptions($options); |
||
175 | $cmd .= $this->renderArguments($arguments); |
||
176 | } |
||
177 | } |
||
178 | |||
179 | return $cmd; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Render options as options for the command |
||
184 | * |
||
185 | * @param array $options The options |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | protected function renderOptions(array $options) |
||
190 | { |
||
191 | $optionString = ''; |
||
192 | foreach ($options as $option => $value) { |
||
193 | $value = $this->expand($value); |
||
194 | if ($value === false) { |
||
195 | continue; |
||
196 | } |
||
197 | $l = strlen($option); |
||
198 | $optionString .= ' '; |
||
199 | if ($option[0] !== '-') { |
||
200 | $optionString .= ($l === 1) ? '-' : '--'; |
||
201 | } |
||
202 | $optionString .= $option; |
||
203 | if ($value !== true) { |
||
204 | if ($l > 1 && ($option[0] !== '-' || $option[1] === '-')) { |
||
205 | $optionString .= '='; |
||
206 | } else { |
||
207 | $optionString .= ' '; |
||
208 | } |
||
209 | $optionString .= escapeshellarg($value); |
||
210 | } |
||
211 | } |
||
212 | return $optionString; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Render arguments for the command |
||
217 | * |
||
218 | * @param array $arguments The arguments |
||
219 | * |
||
220 | * @return string |
||
221 | */ |
||
222 | protected function renderArguments(array $arguments) |
||
223 | { |
||
224 | $argumentString = ''; |
||
225 | foreach ($arguments as $argument) { |
||
226 | $value = $this->expand($argument); |
||
227 | $argumentString .= ' ' . escapeshellarg($value); |
||
228 | } |
||
229 | return $argumentString; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Run the command |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | public function run() |
||
238 | { |
||
239 | $this->preview(); |
||
240 | $res = $this->execute(); |
||
241 | return $res; |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Execute the command |
||
246 | * |
||
247 | * @return string |
||
248 | */ |
||
249 | public function execute() |
||
250 | { |
||
251 | return $this->executeCommand(); |
||
252 | } |
||
253 | } |
||
254 | ?> |
||
255 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.