Skip to content

Commit

Permalink
fix(counter): readonly
Browse files Browse the repository at this point in the history
  • Loading branch information
thepercival committed May 26, 2024
1 parent ae6c6fc commit 42017ad
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
17 changes: 8 additions & 9 deletions domain/Counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* @template T
*/
class Counter implements \Countable
readonly class Counter implements \Countable
{
/**
* @param T $countedObject
Expand All @@ -16,28 +16,27 @@ public function __construct(protected mixed $countedObject, protected int $count
{
}

public function reset(): void
public function reset2(): self
{
$this->count = 0;
return new self($this->countedObject);
}

/**
* @return self<T>
*/
public function decrement2(): self
public function decrement(): self
{
return new self($this->countedObject, $this->count - 1 );
}

public function increment(): int
public function increment(): self
{
$this->count++;
return $this->count;
return new self($this->countedObject, $this->count + 1 );
}

public function increase(int $count): void
public function increase(int $count): self
{
$this->count += $count;
return new self($this->countedObject, $this->count + $count );
}

public function count(): int
Expand Down
6 changes: 3 additions & 3 deletions tests/cases/CounterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function testReset(): void
$stdClass = new \stdClass();

$counter = new Counter($stdClass, 4);
$counter->reset();
$counter->reset2();
self::assertSame(0, $counter->count());
}

Expand All @@ -32,10 +32,10 @@ public function testDecrement(): void
$stdClass = new \stdClass();

$counter = new Counter($stdClass);
self::assertSame(-1, ($counter->decrement2())->count());
self::assertSame(-1, $counter->decrement()->count());

$counter = new Counter($stdClass, 4);
self::assertSame(3, ($counter->decrement2())->count());
self::assertSame(3, $counter->decrement()->count());
}

public function testIncrement(): void
Expand Down

0 comments on commit 42017ad

Please sign in to comment.