Skip to content

Commit bc2aaab

Browse files
committed
refactor: tighten EnumHelper and dedupe rebalancer enum conversion
- type EnumHelper::convertEnumToString param as mixed, mark class final, add strict_types - split BackedEnum / UnitEnum branches, throw InvalidArgumentException on unknown objects - convert column ids once inside findColumnsNeedingRebalancing so callers don't re-convert - update unit test to assert the throw
1 parent b0c0416 commit bc2aaab

5 files changed

Lines changed: 32 additions & 26 deletions

File tree

src/Commands/RebalancePositionsCommand.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Illuminate\Console\Command;
66
use Illuminate\Database\Eloquent\Model;
77
use Relaticle\Flowforge\Services\PositionRebalancer;
8-
use Relaticle\Flowforge\Support\EnumHelper;
98

109
use function Laravel\Prompts\confirm;
1110
use function Laravel\Prompts\error;
@@ -163,9 +162,8 @@ private function rebalanceAllNeeded(
163162
$this->newLine();
164163

165164
foreach ($columnsNeedingRebalancing as $columnId) {
166-
$columnValue = EnumHelper::convertEnumToString($columnId);
167-
$stats = $rebalancer->getGapStatistics($query, $columnField, $columnValue, $positionField);
168-
$this->line(" - {$columnValue}: {$stats['count']} records, {$stats['small_gaps']} small gaps");
165+
$stats = $rebalancer->getGapStatistics($query, $columnField, $columnId, $positionField);
166+
$this->line(" - {$columnId}: {$stats['count']} records, {$stats['small_gaps']} small gaps");
169167
}
170168

171169
$this->newLine();

src/Commands/RepairPositionsCommand.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ private function analyzePositions(string $model, string $columnField, string $po
189189
->groupBy($columnField)
190190
->pluck('record_count', $columnField)
191191
->mapWithKeys(function ($count, $key) {
192-
// Convert enum to string value if needed
193192
$stringKey = EnumHelper::convertEnumToString($key);
194193

195194
return [$stringKey => $count];
@@ -237,7 +236,6 @@ private function calculateChanges(string $model, string $columnField, string $po
237236
->pluck($columnField);
238237

239238
foreach ($groups as $group) {
240-
// Convert enum to string for array key
241239
$groupKey = EnumHelper::convertEnumToString($group);
242240

243241
$records = $this->getRecordsForStrategy($model, $columnField, $positionField, $group, $strategy, $query);

src/Services/PositionRebalancer.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,11 @@ public function findColumnsNeedingRebalancing(
102102
$columns = (clone $query)
103103
->select($columnField)
104104
->distinct()
105-
->pluck($columnField);
105+
->pluck($columnField)
106+
->map(fn ($columnId) => EnumHelper::convertEnumToString($columnId));
106107

107-
return $columns->filter(function ($columnId) use ($query, $columnField, $positionField) {
108-
return $this->needsRebalancing($query, $columnField, EnumHelper::convertEnumToString($columnId), $positionField);
108+
return $columns->filter(function (string $columnId) use ($query, $columnField, $positionField) {
109+
return $this->needsRebalancing($query, $columnField, $columnId, $positionField);
109110
})->values();
110111
}
111112

@@ -131,12 +132,10 @@ public function rebalanceAll(
131132
);
132133

133134
foreach ($columnsNeedingRebalancing as $columnId) {
134-
$columnValue = EnumHelper::convertEnumToString($columnId);
135-
136-
$results[$columnValue] = $this->rebalanceColumn(
135+
$results[$columnId] = $this->rebalanceColumn(
137136
$query,
138137
$columnField,
139-
$columnValue,
138+
$columnId,
140139
$positionField
141140
);
142141
}

src/Support/EnumHelper.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Relaticle\Flowforge\Support;
46

5-
class EnumHelper
7+
use BackedEnum;
8+
use InvalidArgumentException;
9+
use UnitEnum;
10+
11+
final class EnumHelper
612
{
7-
public static function convertEnumToString($value): string
13+
public static function convertEnumToString(mixed $value): string
814
{
15+
if ($value instanceof BackedEnum) {
16+
return (string) $value->value;
17+
}
18+
19+
if ($value instanceof UnitEnum) {
20+
return $value->name;
21+
}
22+
923
if (is_object($value)) {
10-
// Handle Laravel Enums (implements UnitEnum)
11-
if ($value instanceof \UnitEnum) {
12-
return $value->value ?? $value->name;
13-
}
14-
// Handle objects with value property
1524
if (property_exists($value, 'value')) {
1625
return (string) $value->value;
1726
}
18-
// Handle objects with __toString method
27+
1928
if (method_exists($value, '__toString')) {
2029
return (string) $value;
2130
}
2231

23-
// Fallback: try to get class name or serialize
24-
return class_basename($value);
32+
throw new InvalidArgumentException(sprintf(
33+
'Cannot convert object of type %s to a column identifier string.',
34+
$value::class,
35+
));
2536
}
2637

2738
return (string) $value;

tests/Unit/EnumHelperTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
expect(EnumHelper::convertEnumToString(42))->toBe('42');
2121
});
2222

23-
test('falls back to class basename for unknown objects', function () {
23+
test('throws for unknown objects', function () {
2424
$obj = new class {};
2525

26-
expect(EnumHelper::convertEnumToString($obj))->toBeString()->not->toBeEmpty();
27-
});
26+
EnumHelper::convertEnumToString($obj);
27+
})->throws(InvalidArgumentException::class);
2828

2929
test('returns value from object with value property', function () {
3030
$obj = new class

0 commit comments

Comments
 (0)