Skip to content

Commit 2c320f5

Browse files
committed
Clear memoized properties when cloning object
1 parent f2188ab commit 2c320f5

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace AutoValue\ValueToString;
4+
5+
6+
class ToStringMethodProcessor
7+
{
8+
9+
}

src/ValueWither/WitherMethodProcessor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function generateMethods(ReflectionMethodCollection $matchedMethods, Prop
3737
$parameterName = $method->getParameters()[0]->getName();
3838
$methodBody = <<<THEPHP
3939
\$result = clone \$this;
40+
unset(\$result->__memoized);
4041
\$result->$propertyName = \${$parameterName};
4142
return \$result;
4243
THEPHP;

tests/Console/Build/BuildTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ public function testCommandAutoClass(): void
9494
self::assertTrue($command1->equals($command2->withPayload($command1->payload())));
9595
}
9696

97+
public function testMemoizedValuesResetOnClone(): void
98+
{
99+
$address = Address::builder()
100+
->setLines('foo')
101+
->setCountry('UK')
102+
->setPostCode(new PostCode())
103+
->build();
104+
self::assertSame('foo', $address->firstLine());
105+
$address2 = $address->withLines('bar');
106+
self::assertSame('bar', $address2->firstLine());
107+
}
108+
97109
private function createTempDir(): string
98110
{
99111
$path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'auto-value-php-tests-' . \random_int(1, 1000000);

tests/Console/Build/templates/Address.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ public abstract function lines(): array;
1717

1818
public abstract function withLines(string ...$lines): self;
1919

20+
/** @Memoize */
21+
public function firstLine(): ?string
22+
{
23+
return $this->lines()[0] ?? null;
24+
}
25+
2026
public abstract function city(): ?string;
2127

2228
public abstract function country(): string;

0 commit comments

Comments
 (0)