Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

array_shift causes arguments following a zero argument to be omitted #10

Open
robincafolla opened this issue Feb 9, 2021 · 3 comments

Comments

@robincafolla
Copy link

Problem

Use of the return result from array_shift as the condition of the loop here:

while ($arg = array_shift($argv)) {

Causes any arguments after a zero argument to be omitted. This is because the zero value is evaluated as false.

Test case

//test.php
$cliConfig = [
  'a' => [
    'help' => 'A',
  ],
  'b' => [
    'help' => 'B'
  ],
  'c' => [
    'help' => 'C'
  ],
];

$cliArgs = new CliArgs($cliConfig);
var_dump($cliArgs->getArguments());

Running the above script with php test.php --a 1 --b 0 --c 1 will result in the following output:

array(3) {
  [0] =>  string(13) "/tmp/test.php"
  'a' =>  string(1) "1"
  'b' =>  NULL
}

The value for b is incorrect, and c is missing entirely.

Recommendation

Replace

while ($arg = array_shift($argv)) {

with

while (count($argv) > 0) {
  $arg = array_shift($argv);
@robincafolla
Copy link
Author

I can open a PR to fix this if it suits @cheprasov

@robincafolla robincafolla changed the title array_shift causes arguments following as zero argument to be omitted array_shift causes arguments following a zero argument to be omitted Feb 9, 2021
@cheprasov
Copy link
Owner

Hi @robincafolla sorry for the delay with the answer. Thanks a lot for the bug, I will try to fix it soon :)

@robincafolla
Copy link
Author

No worries, I know how it is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants