Fix autofilter cloning across PHP versions

Avoid indirect access variable ambiguity across PHP 5.6 and PHP 7.0
This commit is contained in:
Adrien Crivelli 2016-12-04 16:44:40 +09:00
parent 39b8dbd0a1
commit 3bd0f6f985
No known key found for this signature in database
GPG Key ID: B182FD79DC6DE92E
2 changed files with 33 additions and 27 deletions

View File

@ -376,21 +376,19 @@ class Column
{ {
$vars = get_object_vars($this); $vars = get_object_vars($this);
foreach ($vars as $key => $value) { foreach ($vars as $key => $value) {
if (is_object($value)) { if ($key === 'parent') {
if ($key == 'parent') { // Detach from autofilter parent
// Detach from autofilter parent $this->parent = null;
$this->$key = null; } elseif ($key === 'ruleset') {
} else { // The columns array of \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter objects
$this->$key = clone $value; $this->ruleset = [];
}
} elseif ((is_array($value)) && ($key == 'ruleset')) {
// The columns array of \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter objects
$this->$key = [];
foreach ($value as $k => $v) { foreach ($value as $k => $v) {
$this->$key[$k] = clone $v; $cloned = clone $v;
// attach the new cloned Rule to this new cloned Autofilter Cloned object $cloned->setParent($this); // attach the new cloned Rule to this new cloned Autofilter Cloned object
$this->$key[$k]->setParent($this); $this->ruleset[$k] = $cloned;
} }
} elseif (is_object($value)) {
$this->$key = clone $value;
} else { } else {
$this->$key = $value; $this->$key = $value;
} }

View File

@ -7,9 +7,7 @@ use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter;
class ColumnTest extends \PHPUnit_Framework_TestCase class ColumnTest extends \PHPUnit_Framework_TestCase
{ {
private $testInitialColumn = 'H'; private $testInitialColumn = 'H';
private $testAutoFilterColumnObject; private $testAutoFilterColumnObject;
private $mockAutoFilterObject; private $mockAutoFilterObject;
public function setUp() public function setUp()
@ -111,9 +109,10 @@ class ColumnTest extends \PHPUnit_Framework_TestCase
public function testSetAttributes() public function testSetAttributes()
{ {
$attributeSet = ['val' => 100, $attributeSet = [
'maxVal' => 200, 'val' => 100,
]; 'maxVal' => 200,
];
// Setters return the instance to implement the fluent interface // Setters return the instance to implement the fluent interface
$result = $this->testAutoFilterColumnObject->setAttributes($attributeSet); $result = $this->testAutoFilterColumnObject->setAttributes($attributeSet);
@ -122,9 +121,10 @@ class ColumnTest extends \PHPUnit_Framework_TestCase
public function testGetAttributes() public function testGetAttributes()
{ {
$attributeSet = ['val' => 100, $attributeSet = [
'maxVal' => 200, 'val' => 100,
]; 'maxVal' => 200,
];
$this->testAutoFilterColumnObject->setAttributes($attributeSet); $this->testAutoFilterColumnObject->setAttributes($attributeSet);
@ -135,9 +135,10 @@ class ColumnTest extends \PHPUnit_Framework_TestCase
public function testSetAttribute() public function testSetAttribute()
{ {
$attributeSet = ['val' => 100, $attributeSet = [
'maxVal' => 200, 'val' => 100,
]; 'maxVal' => 200,
];
foreach ($attributeSet as $attributeName => $attributeValue) { foreach ($attributeSet as $attributeName => $attributeValue) {
// Setters return the instance to implement the fluent interface // Setters return the instance to implement the fluent interface
@ -148,9 +149,10 @@ class ColumnTest extends \PHPUnit_Framework_TestCase
public function testGetAttribute() public function testGetAttribute()
{ {
$attributeSet = ['val' => 100, $attributeSet = [
'maxVal' => 200, 'val' => 100,
]; 'maxVal' => 200,
];
$this->testAutoFilterColumnObject->setAttributes($attributeSet); $this->testAutoFilterColumnObject->setAttributes($attributeSet);
@ -164,7 +166,13 @@ class ColumnTest extends \PHPUnit_Framework_TestCase
public function testClone() public function testClone()
{ {
$originalRule = $this->testAutoFilterColumnObject->createRule();
$result = clone $this->testAutoFilterColumnObject; $result = clone $this->testAutoFilterColumnObject;
$this->assertInstanceOf(AutoFilter\Column::class, $result); $this->assertInstanceOf(AutoFilter\Column::class, $result);
$this->assertCount(1, $result->getRules());
$this->assertContainsOnlyInstancesOf(AutoFilter\Column\Rule::class, $result->getRules());
$clonedRule = $result->getRules()[0];
$this->assertNotSame($originalRule, $clonedRule);
$this->assertSame($result, $clonedRule->getParent());
} }
} }