Skip to content

Commit

Permalink
using immutable objects for a stream result is a bad idea because of …
Browse files Browse the repository at this point in the history
…gc cycles (#86)
  • Loading branch information
frederikbosch authored Sep 29, 2020
1 parent fdf4380 commit 1e8cf3c
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 26 deletions.
16 changes: 7 additions & 9 deletions src/Protocol/Imap/Response/AggregateResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,41 +106,39 @@ public function hasCompleted(): bool
*/
public function withLine(string $line): AggregateResponse
{
$clone = clone $this;

switch (\substr($line, 0, 2)) {
case '+ ':
$clone->lines[] = new CommandContinuationRequestResponse(
$this->lines[] = new CommandContinuationRequestResponse(
\substr($line, 2)
);
break;
case '* ':
$clone->lines[] = new UntaggedResponse(
$this->lines[] = new UntaggedResponse(
\substr($line, 2)
);
break;
default:
try {
$clone->lines[] = new TaggedResponse(
$this->lines[] = new TaggedResponse(
$this->tag,
$this->tag->extractBodyFromLine($line)
);
} catch (\InvalidArgumentException $e) {
if (empty($clone->lines)) {
if (empty($this->lines)) {
throw new \UnexpectedValueException(
'Expected line to begin with +, * or tag. Got: ' . $line
);
}

$keys = \array_keys($clone->lines);
$keys = \array_keys($this->lines);
$lastKey = \end($keys);
$clone->lines[$lastKey] = $clone->lines[$lastKey]->withAddedBody($line);
$this->lines[$lastKey]->withAddedBody($line);
}
break;

}

return $clone;
return $this;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ public function __toString(): string
*/
public function withAddedBody(string $data): ResponseInterface
{
$clone = clone $this;
$clone->line .= $data;
return $clone;
$this->line .= $data;
return $this;
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/Protocol/Imap/Response/TaggedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ public function __toString(): string
*/
public function withAddedBody(string $data): ResponseInterface
{
$clone = clone $this;
$clone->line .= $data;
return $clone;
$this->line .= $data;
return $this;
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/Protocol/Imap/Response/UntaggedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ public function __toString(): string
*/
public function withAddedBody(string $data): ResponseInterface
{
$clone = clone $this;
$clone->line .= $data;
return $clone;
$this->line .= $data;
return $this;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/Unit/Protocol/Imap/Response/AggregateResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public function it_can_be_iterated(): void
/**
* @test
*/
public function it_is_immutable(): void
public function it_is_mutable(): void
{
$response = new AggregateResponse(Tag::fromNonce(1));
$this->assertNotSame($response, $response->withLine('* Untagged'));
$this->assertSame($response, $response->withLine('* Untagged'));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public function it_can_be_casted_to_string(): void
/**
* @test
*/
public function it_is_immutable(): void
public function it_is_mutable(): void
{
$response = new CommandContinuationRequestResponse('OK successful');
$this->assertNotSame($response, $response->withAddedBody('body'));
$this->assertSame($response, $response->withAddedBody('body'));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/Unit/Protocol/Imap/Response/TaggedResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public function it_can_be_casted_to_string(): void
/**
* @test
*/
public function it_is_immutable(): void
public function it_is_mutable(): void
{
$response = new TaggedResponse(Tag::fromNonce(1), 'OK successful');
$this->assertNotSame($response, $response->withAddedBody('body'));
$this->assertSame($response, $response->withAddedBody('body'));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/Unit/Protocol/Imap/Response/UntaggedResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public function it_can_be_casted_to_string(): void
/**
* @test
*/
public function it_is_immutable(): void
public function it_is_mutable(): void
{
$response = new UntaggedResponse('OK successful');
$this->assertNotSame($response, $response->withAddedBody('body'));
$this->assertSame($response, $response->withAddedBody('body'));
}

/**
Expand Down

0 comments on commit 1e8cf3c

Please sign in to comment.