From 42c3f03dba70e8217dbce7719b3a28f84dcc5a2c Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Thu, 24 Aug 2023 12:39:02 +0200 Subject: [PATCH 1/4] feat: document clone method --- src/Database/Document.php | 12 +++++++++ tests/Database/DocumentTest.php | 43 +++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/Database/Document.php b/src/Database/Document.php index d158628ac..5a2227359 100644 --- a/src/Database/Document.php +++ b/src/Database/Document.php @@ -432,4 +432,16 @@ public function getArrayCopy(array $allow = [], array $disallow = []): array return $output; } + + public function __clone() + { + foreach ($this as $key => $value) { + if ($value instanceof self) { + $this[$key] = clone $value; + } + if (\is_array($value)) { + $this[$key] = \array_map(fn ($item) => $item instanceof self ? clone $item : $item, $value); + } + } + } } diff --git a/tests/Database/DocumentTest.php b/tests/Database/DocumentTest.php index 8acb95096..acb9e7be0 100644 --- a/tests/Database/DocumentTest.php +++ b/tests/Database/DocumentTest.php @@ -321,6 +321,49 @@ public function testIsSet(): void $this->assertEquals(true, $this->document->isSet('title')); } + public function testClone(): void + { + $before = new Document([ + 'level' => 0, + 'name' => '_', + 'document' => new Document(['name' => 'zero']), + 'children' => [ + new Document([ + 'level' => 1, + 'name' => 'a', + 'document' => new Document(['name' => 'one']), + 'children' => [ + new Document([ + 'level' => 2, + 'name' => 'x', + 'document' => new Document(['name' => 'two']), + 'children' => [ + new Document([ + 'level' => 3, + 'name' => 'i' + ]), + ] + ]) + ] + ]) + ] + ]); + + $after = clone $before; + + $before->setAttribute('name', 'before'); + $before->getAttribute('document')->setAttribute('name', 'before_one'); + $before->getAttribute('children')[0]->setAttribute('name', 'before_a'); + $before->getAttribute('children')[0]->getAttribute('document')->setAttribute('name', 'before_two'); + $before->getAttribute('children')[0]->getAttribute('children')[0]->setAttribute('name', 'before_x'); + + $this->assertEquals('_', $after->getAttribute('name')); + $this->assertEquals('zero', $after->getAttribute('document')->getAttribute('name')); + $this->assertEquals('a', $after->getAttribute('children')[0]->getAttribute('name')); + $this->assertEquals('one', $after->getAttribute('children')[0]->getAttribute('document')->getAttribute('name')); + $this->assertEquals('x', $after->getAttribute('children')[0]->getAttribute('children')[0]->getAttribute('name')); + } + public function testGetArrayCopy(): void { $this->assertEquals([ From 0d6c9de4e2ca43feb26a60debe3254f3194db019 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 24 Aug 2023 14:21:25 +0000 Subject: [PATCH 2/4] Attempt to fix warnings --- src/Database/Document.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Database/Document.php b/src/Database/Document.php index 5a2227359..87a53d6ae 100644 --- a/src/Database/Document.php +++ b/src/Database/Document.php @@ -438,9 +438,10 @@ public function __clone() foreach ($this as $key => $value) { if ($value instanceof self) { $this[$key] = clone $value; - } - if (\is_array($value)) { + } else if (\is_array($value)) { $this[$key] = \array_map(fn ($item) => $item instanceof self ? clone $item : $item, $value); + } else { + $this[$key] = $value; } } } From ab6d8b0b75b4f1755c6175587c94a5044adfd9d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 24 Aug 2023 16:41:44 +0200 Subject: [PATCH 3/4] Update src/Database/Document.php --- src/Database/Document.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Database/Document.php b/src/Database/Document.php index 87a53d6ae..eb8de01f1 100644 --- a/src/Database/Document.php +++ b/src/Database/Document.php @@ -440,8 +440,6 @@ public function __clone() $this[$key] = clone $value; } else if (\is_array($value)) { $this[$key] = \array_map(fn ($item) => $item instanceof self ? clone $item : $item, $value); - } else { - $this[$key] = $value; } } } From 2d52ce8ac92436cd51c36cff2ec7bfdf00c44189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Fri, 25 Aug 2023 06:18:31 +0000 Subject: [PATCH 4/4] Linter fix --- composer.lock | 37 +++++++++++++++++++------------------ src/Database/Document.php | 2 +- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/composer.lock b/composer.lock index d42fcea85..17be45409 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "edeac562417fec69a2f2c46f44fc6ab4", + "content-hash": "5955d41842661561dd1356f1f7b89188", "packages": [ { "name": "composer/package-versions-deprecated", @@ -706,16 +706,16 @@ }, { "name": "nikic/php-parser", - "version": "v4.16.0", + "version": "v4.17.1", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "19526a33fb561ef417e822e85f08a00db4059c17" + "reference": "a6303e50c90c355c7eeee2c4a8b27fe8dc8fef1d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/19526a33fb561ef417e822e85f08a00db4059c17", - "reference": "19526a33fb561ef417e822e85f08a00db4059c17", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/a6303e50c90c355c7eeee2c4a8b27fe8dc8fef1d", + "reference": "a6303e50c90c355c7eeee2c4a8b27fe8dc8fef1d", "shasum": "" }, "require": { @@ -756,9 +756,9 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v4.16.0" + "source": "https://github.com/nikic/PHP-Parser/tree/v4.17.1" }, - "time": "2023-06-25T14:52:30+00:00" + "time": "2023-08-13T19:53:39+00:00" }, { "name": "pcov/clobber", @@ -907,16 +907,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.10.27", + "version": "1.10.32", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "a9f44dcea06f59d1363b100bb29f297b311fa640" + "reference": "c47e47d3ab03137c0e121e77c4d2cb58672f6d44" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/a9f44dcea06f59d1363b100bb29f297b311fa640", - "reference": "a9f44dcea06f59d1363b100bb29f297b311fa640", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/c47e47d3ab03137c0e121e77c4d2cb58672f6d44", + "reference": "c47e47d3ab03137c0e121e77c4d2cb58672f6d44", "shasum": "" }, "require": { @@ -965,7 +965,7 @@ "type": "tidelift" } ], - "time": "2023-08-05T09:57:55+00:00" + "time": "2023-08-24T21:54:50+00:00" }, { "name": "phpunit/php-code-coverage", @@ -1288,16 +1288,16 @@ }, { "name": "phpunit/phpunit", - "version": "9.6.10", + "version": "9.6.11", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "a6d351645c3fe5a30f5e86be6577d946af65a328" + "reference": "810500e92855eba8a7a5319ae913be2da6f957b0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/a6d351645c3fe5a30f5e86be6577d946af65a328", - "reference": "a6d351645c3fe5a30f5e86be6577d946af65a328", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/810500e92855eba8a7a5319ae913be2da6f957b0", + "reference": "810500e92855eba8a7a5319ae913be2da6f957b0", "shasum": "" }, "require": { @@ -1371,7 +1371,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "security": "https://github.com/sebastianbergmann/phpunit/security/policy", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.10" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.11" }, "funding": [ { @@ -1387,7 +1387,7 @@ "type": "tidelift" } ], - "time": "2023-07-10T04:04:23+00:00" + "time": "2023-08-19T07:10:56+00:00" }, { "name": "psr/container", @@ -2672,6 +2672,7 @@ "prefer-lowest": false, "platform": { "ext-pdo": "*", + "ext-mbstring": "*", "php": ">=8.0" }, "platform-dev": [], diff --git a/src/Database/Document.php b/src/Database/Document.php index eb8de01f1..6311841b4 100644 --- a/src/Database/Document.php +++ b/src/Database/Document.php @@ -438,7 +438,7 @@ public function __clone() foreach ($this as $key => $value) { if ($value instanceof self) { $this[$key] = clone $value; - } else if (\is_array($value)) { + } elseif (\is_array($value)) { $this[$key] = \array_map(fn ($item) => $item instanceof self ? clone $item : $item, $value); } }