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);
foreach ($vars as $key => $value) {
if (is_object($value)) {
if ($key == 'parent') {
// Detach from autofilter parent
$this->$key = null;
} else {
$this->$key = clone $value;
}
} elseif ((is_array($value)) && ($key == 'ruleset')) {
// The columns array of \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter objects
$this->$key = [];
if ($key === 'parent') {
// Detach from autofilter parent
$this->parent = null;
} elseif ($key === 'ruleset') {
// The columns array of \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter objects
$this->ruleset = [];
foreach ($value as $k => $v) {
$this->$key[$k] = clone $v;
// attach the new cloned Rule to this new cloned Autofilter Cloned object
$this->$key[$k]->setParent($this);
$cloned = clone $v;
$cloned->setParent($this); // attach the new cloned Rule to this new cloned Autofilter Cloned object
$this->ruleset[$k] = $cloned;
}
} elseif (is_object($value)) {
$this->$key = clone $value;
} else {
$this->$key = $value;
}

View File

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