From d950d129ecfad51acc94bb6caff153b022c9af78 Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Fri, 23 Feb 2024 14:17:36 +0100 Subject: [PATCH] Switch to resturn object instead of array --- .../practice/two-bucket/.meta/example.php | 23 ++++----- exercises/practice/two-bucket/TwoBucket.php | 10 +--- .../practice/two-bucket/TwoBucketTest.php | 48 +++++++++---------- 3 files changed, 37 insertions(+), 44 deletions(-) diff --git a/exercises/practice/two-bucket/.meta/example.php b/exercises/practice/two-bucket/.meta/example.php index 76950c25..b43b72d5 100644 --- a/exercises/practice/two-bucket/.meta/example.php +++ b/exercises/practice/two-bucket/.meta/example.php @@ -6,6 +6,9 @@ class TwoBucket { private int $goal; private array $buckets; + public int $moves; + public string $goalBucket; + public int $otherBucket; public function __construct(int $sizeBucketOne, int $sizeBucketTwo, int $goal, string $startBucket) { @@ -19,7 +22,7 @@ public function __construct(int $sizeBucketOne, int $sizeBucketTwo, int $goal, s $this->validate(); } - public function solve(): array + public function solve(): self { $this->first()->empty(); $this->second()->empty(); @@ -35,19 +38,17 @@ public function solve(): array while (true) { if ($this->first()->getAmount() === $this->goal) { - return [ - 'moves' => $moves, - 'goalBucket' => $this->first()->getName(), - 'otherBucket' => $this->second()->getAmount(), - ]; + $this->moves = $moves; + $this->goalBucket = $this->first()->getName(); + $this->otherBucket = $this->second()->getAmount(); + return $this; } if ($this->second()->getAmount() === $this->goal) { - return [ - 'moves' => $moves, - 'goalBucket' => $this->second()->getName(), - 'otherBucket' => $this->first()->getAmount(), - ]; + $this->moves = $moves; + $this->goalBucket = $this->second()->getName(); + $this->otherBucket = $this->first()->getAmount(); + return $this; } if ($this->first()->isEmpty()) { diff --git a/exercises/practice/two-bucket/TwoBucket.php b/exercises/practice/two-bucket/TwoBucket.php index a9728d6c..7ed53c5c 100644 --- a/exercises/practice/two-bucket/TwoBucket.php +++ b/exercises/practice/two-bucket/TwoBucket.php @@ -31,15 +31,7 @@ public function __construct(int $sizeBucketOne, int $sizeBucketTwo, int $goal, s throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); } - public function solve(): array - { - throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); - } -} - -class Bucket -{ - public function __construct(string $name, int $size) + public function solve(): self { throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); } diff --git a/exercises/practice/two-bucket/TwoBucketTest.php b/exercises/practice/two-bucket/TwoBucketTest.php index 6bfc45c6..dd9fc91f 100644 --- a/exercises/practice/two-bucket/TwoBucketTest.php +++ b/exercises/practice/two-bucket/TwoBucketTest.php @@ -21,9 +21,9 @@ public function testMeasureUsingBucketOneOfSize3AndBucketTwoOfSize5StartWithBuck $twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck); $result = $twoBucket->solve(); - $this->assertEquals(4, $result['moves']); - $this->assertEquals('one', $result['goalBucket']); - $this->assertEquals(5, $result['otherBucket']); + $this->assertEquals(4, $result->moves); + $this->assertEquals('one', $result->goalBucket); + $this->assertEquals(5, $result->otherBucket); } /** @@ -38,9 +38,9 @@ public function testMeasureUsingBucketOneOfSize3AndBucketTwoOfSize5StartWithBuck $twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck); $result = $twoBucket->solve(); - $this->assertEquals(8, $result['moves']); - $this->assertEquals('two', $result['goalBucket']); - $this->assertEquals(3, $result['otherBucket']); + $this->assertEquals(8, $result->moves); + $this->assertEquals('two', $result->goalBucket); + $this->assertEquals(3, $result->otherBucket); } /** @@ -55,9 +55,9 @@ public function testMeasureUsingBucketOneOfSize7AndBucketTwoOfSize11StartWithBuc $twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck); $result = $twoBucket->solve(); - $this->assertEquals(14, $result['moves']); - $this->assertEquals('one', $result['goalBucket']); - $this->assertEquals(11, $result['otherBucket']); + $this->assertEquals(14, $result->moves); + $this->assertEquals('one', $result->goalBucket); + $this->assertEquals(11, $result->otherBucket); } /** @@ -72,9 +72,9 @@ public function testMeasureUsingBucketOneOfSize7AndBucketTwoOfSize11StartWithBuc $twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck); $result = $twoBucket->solve(); - $this->assertEquals(18, $result['moves']); - $this->assertEquals('two', $result['goalBucket']); - $this->assertEquals(7, $result['otherBucket']); + $this->assertEquals(18, $result->moves); + $this->assertEquals('two', $result->goalBucket); + $this->assertEquals(7, $result->otherBucket); } /** @@ -85,9 +85,9 @@ public function testMeasureOneStepUsingBucketOneOfSize1AndBucketTwoOfSize3StartW $twoBucket = new TwoBucket(1, 3, 3, 'two'); $result = $twoBucket->solve(); - $this->assertEquals(1, $result['moves']); - $this->assertEquals('two', $result['goalBucket']); - $this->assertEquals(0, $result['otherBucket']); + $this->assertEquals(1, $result->moves); + $this->assertEquals('two', $result->goalBucket); + $this->assertEquals(0, $result->otherBucket); } /** @@ -98,9 +98,9 @@ public function testMeasureUsingBucketOneOfSize2AndBucketTwoOfSize3StartWithBuck $twoBucket = new TwoBucket(2, 3, 3, 'one'); $result = $twoBucket->solve(); - $this->assertEquals(2, $result['moves']); - $this->assertEquals('two', $result['goalBucket']); - $this->assertEquals(2, $result['otherBucket']); + $this->assertEquals(2, $result->moves); + $this->assertEquals('two', $result->goalBucket); + $this->assertEquals(2, $result->otherBucket); } /** @@ -123,9 +123,9 @@ public function testReachabilityNotPossibleToReachGoalStartWithBucketOneAndEndWi $twoBucket = new TwoBucket(6, 15, 9, 'one'); $result = $twoBucket->solve(); - $this->assertEquals(10, $result['moves']); - $this->assertEquals('two', $result['goalBucket']); - $this->assertEquals(0, $result['otherBucket']); + $this->assertEquals(10, $result->moves); + $this->assertEquals('two', $result->goalBucket); + $this->assertEquals(0, $result->otherBucket); } /** @@ -140,9 +140,9 @@ public function testWithSameBucketsButDifferentGoalItIsPossible(): void $twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck); $result = $twoBucket->solve(); - $this->assertEquals(10, $result['moves']); - $this->assertEquals('two', $result['goalBucket']); - $this->assertEquals(0, $result['otherBucket']); + $this->assertEquals(10, $result->moves); + $this->assertEquals('two', $result->goalBucket); + $this->assertEquals(0, $result->otherBucket); } /**