Skip to content
This repository was archived by the owner on Aug 25, 2025. It is now read-only.

Commit 3d22850

Browse files
committed
Make addReturn and addReturnCase take a value renderer
Consistency; previously was always literals; replace with one of: - `addReturn($any_literal, HackBuilderValues::literal())` - `addReturnf('some literal with no placeholders')` - `addReturnf('%s', $any_literal)` Similar for addReturnCase. Fixes #32
1 parent 0baa1ec commit 3d22850

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

examples/dorm/CodegenDorm.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private function getLoad(): CodegenMethod {
9595
->addMultilineCall('$conn->query', Vector {"\"$sql\""}, true)
9696
->addLine('$result = $cursor->fetch(PDO::FETCH_ASSOC);')
9797
->startIfBlock('!$result')
98-
->addReturn('null')
98+
->addReturnf('null')
9999
->endIfBlock()
100100
->addAssignment(
101101
'$ts',
@@ -168,7 +168,7 @@ private function getGetters(): Vector<CodegenMethod> {
168168
$data,
169169
HackBuilderValues::literal(),
170170
)
171-
->addReturn($return_data)
171+
->addReturn($return_data, HackBuilderValues::literal())
172172
->getCode();
173173
}
174174
$methods[] = $cg->codegenMethod('get'.$name)

examples/dorm/CodegenMutator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ private function getSaveMethod(): CodegenMethod {
182182
$this->schema->getTableName(),
183183
)
184184
->addLine('$conn->exec($st);')
185-
->addReturn('(int) $conn->lastInsertId()')
185+
->addReturnf('(int) $conn->lastInsertId()')
186186
->addElseBlock()
187187
->addAssignment(
188188
'$pairs',
@@ -195,7 +195,7 @@ private function getSaveMethod(): CodegenMethod {
195195
$this->schema->getIdField(),
196196
)
197197
->addLine('$conn->exec($st);')
198-
->addReturn('$id')
198+
->addReturnf('$id')
199199
->endIfBlock();
200200

201201
return $cg->codegenMethod('save')
@@ -267,7 +267,7 @@ private function getSetters(): Vector<CodegenMethod> {
267267
$body->endManualSection();
268268
}
269269

270-
$body->addReturn('$this');
270+
$body->addReturnf('$this');
271271

272272
$methods[] = $cg->codegenMethod('set'.$name)
273273
->setReturnType('this')

src/HackBuilder.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,23 @@ private function addWrappedStringImpl(
158158
return $this;
159159
}
160160

161-
public function addReturn(string $value): this {
162-
return $this->addLineImpl('return %s;', [$value]);
161+
public function addReturn<T>(
162+
T $value,
163+
IHackBuilderValueRenderer<T> $renderer,
164+
): this {
165+
return $this
166+
->add('return ')
167+
->addValue($value, $renderer)
168+
->addLine(';');
163169
}
164170
public function addReturnf(
165171
SprintfFormatString $value,
166172
mixed ...$args
167173
): this {
168-
return $this->addReturn(vsprintf($value, $args));
174+
return $this->addReturn(
175+
vsprintf($value, $args),
176+
HackBuilderValues::literal(),
177+
);
169178
}
170179

171180
public function addAssignment<T>(
@@ -353,8 +362,21 @@ public function endDefault(): this {
353362
return $this->unindent();
354363
}
355364

356-
public function returnCase(string $statement): this {
357-
return $this->addReturn($statement)->unindent();
365+
public function returnCase<T>(
366+
T $value,
367+
IHackBuilderValueRenderer<T> $r,
368+
): this {
369+
return $this->addReturn($value, $r)->unindent();
370+
}
371+
372+
public function returnCasef(
373+
SprintfFormatString $value,
374+
mixed ...$args
375+
): this {
376+
return $this->returnCase(
377+
vsprintf($value, $args),
378+
HackBuilderValues::literal(),
379+
);
358380
}
359381

360382
public function breakCase(): this {

test/HackBuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public function testSwitchBodyWithReturnsInCaseAndDefault(): void {
250250
($player, $body) ==> {
251251
$body->addCase(sprintf('\'%s\'', $player['name']))
252252
->addLinef('$shot = new Shot(\'%s\');', $player['favorite_shot'])
253-
->returnCase('$shot->execute()');
253+
->returnCasef('$shot->execute()');
254254
},
255255
)
256256
->addDefault()

0 commit comments

Comments
 (0)