Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix generating array with unsorted keys #4566

Closed
wants to merge 1 commit into from

2 participants

@ftdebugger

For example array

$value = array(
    1 => 'a',
    0 => 'b',
    'c'
)

generating to

array(
    1 => 'a',
    'b', // this value will be lost, because it will be rewrite by value 'c'
    2 => 'c'
)

this patch fix it, and do as expected

array(
    1 => 'a',
    0 => 'b',
    'c'
)
@weierophinney weierophinney was assigned
@weierophinney weierophinney referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/4566' into develop
Forward port #4566
87f22c1
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/4566'
Close #4566
f3280b2
@weierophinney weierophinney referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@weierophinney weierophinney referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@weierophinney weierophinney referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 30, 2013
  1. @ftdebugger
This page is out of date. Refresh to see the latest.
View
13 library/Zend/Code/Generator/ValueGenerator.php
@@ -364,9 +364,18 @@ public function generate()
/* @var $v ValueGenerator */
$v->setArrayDepth($this->arrayDepth + 1);
$partV = $v->generate();
- if ($n === $noKeyIndex) {
+ $short = false;
+ if (is_int($n)) {
+ if ($n === $noKeyIndex) {
+ $short = true;
+ $noKeyIndex++;
+ } else {
+ $noKeyIndex = max($n + 1, $noKeyIndex);
+ }
+ }
+
+ if ($short) {
$outputParts[] = $partV;
- $noKeyIndex++;
} else {
$outputParts[] = (is_int($n) ? $n : self::escape($n)) . ' => ' . $partV;
}
View
25 tests/ZendTest/Code/Generator/ValueGeneratorTest.php
@@ -112,4 +112,29 @@ public function testPropertyDefaultValueCanHandleComplexArrayOfTypes()
$generatedTargetSource = $valueGenerator->generate();
$this->assertEquals($expectedSource, $generatedTargetSource);
}
+
+ public function testPropertyDefaultValueCanHandleArrayWithUnsortedKeys()
+ {
+ $value = array(
+ 1 => 'a',
+ 0 => 'b',
+ 'c',
+ 7 => 'd',
+ 3 => 'e'
+ );
+
+ $valueGenerator = new ValueGenerator();
+ $valueGenerator->setValue($value);
+$expectedSource = <<<EOS
+array(
+ 1 => 'a',
+ 0 => 'b',
+ 'c',
+ 7 => 'd',
+ 3 => 'e'
+ )
+EOS;
+
+ $this->assertEquals($expectedSource, $valueGenerator->generate());
+ }
}
Something went wrong with that request. Please try again.