Skip to content

Commit

Permalink
Clear memoized properties when cloning object
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdifabio committed Dec 12, 2018
1 parent f2188ab commit 2c320f5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/ValueToString/ToStringMethodProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace AutoValue\ValueToString;


class ToStringMethodProcessor
{

}
1 change: 1 addition & 0 deletions src/ValueWither/WitherMethodProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function generateMethods(ReflectionMethodCollection $matchedMethods, Prop
$parameterName = $method->getParameters()[0]->getName();
$methodBody = <<<THEPHP
\$result = clone \$this;
unset(\$result->__memoized);
\$result->$propertyName = \${$parameterName};
return \$result;
THEPHP;
Expand Down
12 changes: 12 additions & 0 deletions tests/Console/Build/BuildTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ public function testCommandAutoClass(): void
self::assertTrue($command1->equals($command2->withPayload($command1->payload())));
}

public function testMemoizedValuesResetOnClone(): void
{
$address = Address::builder()
->setLines('foo')
->setCountry('UK')
->setPostCode(new PostCode())
->build();
self::assertSame('foo', $address->firstLine());
$address2 = $address->withLines('bar');
self::assertSame('bar', $address2->firstLine());
}

private function createTempDir(): string
{
$path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'auto-value-php-tests-' . \random_int(1, 1000000);
Expand Down
6 changes: 6 additions & 0 deletions tests/Console/Build/templates/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ public abstract function lines(): array;

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

/** @Memoize */
public function firstLine(): ?string
{
return $this->lines()[0] ?? null;
}

public abstract function city(): ?string;

public abstract function country(): string;
Expand Down

0 comments on commit 2c320f5

Please sign in to comment.