Zend Framework

Zend_Console_Getopt => method addRules() fails

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 1.0.2
  • Fix Version/s: 1.7.0
  • Component/s: Zend_Console_Getopt
  • Labels:
    None
  • Fix Version Priority:
    Nice to Have

Description

Using addRules() result in some warnings.

reproduce code (example from the manual):

require_once 'Zend/Console/Getopt.php';

$opts = new Zend_Console_Getopt('abp:');
$opts->addRules(
  array(
    'verbose|v' => 'Print verbose output'
  )
);

Output:

PHP Warning:  preg_match_all() expects parameter 2 to be string, array given in /Users/carsten/Sites/Zend/Console/Getopt.php on line 814

Warning: preg_match_all() expects parameter 2 to be string, array given in /Users/carsten/Sites/Zend/Console/Getopt.php on line 814
PHP Notice:  Undefined offset:  1 in /Users/carsten/Sites/Zend/Console/Getopt.php on line 815

Notice: Undefined offset:  1 in /Users/carsten/Sites/Zend/Console/Getopt.php on line 815
PHP Warning:  Invalid argument supplied for foreach() in /Users/carsten/Sites/Zend/Console/Getopt.php on line 815

Warning: Invalid argument supplied for foreach() in /Users/carsten/Sites/Zend/Console/Getopt.php on line 815

Activity

Hide
Thomas Weidner added a comment -

Assigned to Bill

Show
Thomas Weidner added a comment - Assigned to Bill
Hide
Benjamin Eberlei added a comment -

This issue steems from a badly engineered switching between ZEND and GNU Modes.

Zend_Console_GetOpt works with two modes on options MODE_ZEND and MODE_GNU. What happens in the above example is the following:

In the constructor arguments are created in MODE_GNU. Console_GetOpt recognizes that and reverts its setting from MODE_ZEND (default) to MODE_GNU. Now when the addRules() is called with v|verbose Console_GetOpt is still in MODE_GNU and tries to add the array accordingly which fails.

The fix for this issue is very simple and just consists of not switching to MODE GNU when first adding a string then an array.

I suggest a refactoring to strategy pattern to fix the strange switch statement. I will add an issue for this.

Show
Benjamin Eberlei added a comment - This issue steems from a badly engineered switching between ZEND and GNU Modes. Zend_Console_GetOpt works with two modes on options MODE_ZEND and MODE_GNU. What happens in the above example is the following: In the constructor arguments are created in MODE_GNU. Console_GetOpt recognizes that and reverts its setting from MODE_ZEND (default) to MODE_GNU. Now when the addRules() is called with v|verbose Console_GetOpt is still in MODE_GNU and tries to add the array accordingly which fails. The fix for this issue is very simple and just consists of not switching to MODE GNU when first adding a string then an array. I suggest a refactoring to strategy pattern to fix the strange switch statement. I will add an issue for this.
Hide
Wil Sinclair added a comment -

Changing issues in preparation for the 1.7.0 release.

Show
Wil Sinclair added a comment - Changing issues in preparation for the 1.7.0 release.

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: