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

Commit 3d750a4

Browse files
lexidorfredemmott
authored andcommitted
Use Str\join(), instead of implode() (#120)
HackBuilder::addMultilineCall passes $params `Traversable<string>` to `implode()`. `Implode()` is untyped. Passing a `Generator<string>` results in a warning and a return of a null. `Str\join()` correctly checks for `Container<_>` before passing the value on to `implode()`.
1 parent d7d460c commit 3d750a4

9 files changed

+19
-20
lines changed

src/BaseCodeBuilder.hack

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ abstract class BaseCodeBuilder {
331331
}
332332
}
333333
}
334-
return $this->add(\implode("\n ", $final_lines));
334+
return $this->add(Str\join($final_lines, "\n "));
335335
}
336336

337337
/**

src/CodegenClass.hack

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ final class CodegenClass extends CodegenClassish {
125125
$param_names[] = $match[0];
126126
}
127127
}
128-
$params_str = \implode(', ', $param_names);
128+
$params_str = Str\join( $param_names, ', ');
129129
$body = 'return new '.$this->getName().'('.$params_str.');';
130130

131131
$this->wrapperFunc = (

src/CodegenClassish.hack

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ abstract class CodegenClassish implements ICodeBuilderRenderer {
286286
$$,
287287
$decl ==> ' '.$decl.",\n",
288288
)
289-
|> \implode("", $$)
289+
|> Str\join($$, '')
290290
|> Str\strip_suffix($$, ",\n")
291291
|> "\n <\n".$$."\n >";
292292
}
@@ -383,7 +383,7 @@ abstract class CodegenClassish implements ICodeBuilderRenderer {
383383
$doc_block_parts = \array_filter(varray[$this->docBlock, $generated_from]);
384384

385385
if ($doc_block_parts) {
386-
$builder->addDocBlock(\implode("\n\n", $doc_block_parts));
386+
$builder->addDocBlock(Str\join($doc_block_parts, "\n\n"));
387387
}
388388

389389
$wrapper_func = $this->wrapperFunc;

src/CodegenFile.hack

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ final class CodegenFile {
485485
$all_content[] = $content;
486486
}
487487
}
488-
return \implode('', $all_content);
488+
return Str\join($all_content, '');
489489
}
490490

491491
public function setGeneratedFrom(CodegenGeneratedFrom $from): this {

src/CodegenFunctionish.hack

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ abstract class CodegenFunctionish implements ICodeBuilderRenderer {
150150
): string {
151151
$builder = (new HackBuilder($this->config))
152152
->add($keywords)
153-
->addf('%s(%s)', $this->name, \implode(', ', $this->parameters))
153+
->addf('%s(%s)', $this->name, Str\join($this->parameters, ', '))
154154
->addIf($this->returnType !== null, ': '.$this->returnType);
155155

156156
$code = $builder->getCode();

src/CodegenWithAttributes.hack

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99
namespace Facebook\HackCodegen;
1010

11-
use namespace HH\Lib\{C, Dict, Vec};
11+
use namespace HH\Lib\{C, Dict, Str, Vec};
1212

1313
trait CodegenWithAttributes {
1414
protected IHackCodegenConfig $config;
@@ -49,17 +49,17 @@ trait CodegenWithAttributes {
4949
}
5050

5151
return '<<'.
52-
\implode(
53-
', ',
52+
Str\join(
5453
Dict\map_with_key(
5554
$attributes,
5655
($name, $params) ==> {
5756
if (C\is_empty($params)) {
5857
return $name;
5958
}
60-
return $name.'('.\implode(', ', $params).')';
59+
return $name.'('.Str\join($params, ', ').')';
6160
},
6261
),
62+
', '
6363
).
6464
'>>';
6565
}

src/HackBuilder.hack

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ final class HackBuilder extends BaseCodeBuilder {
5555
$max_length = $this->getMaxCodeLength() - 4;
5656

5757
// Let's put everything in a single line
58-
$args = '('.\implode(', ', $params).')';
58+
$args = '('.Str\join($params, ', ').')';
5959
$composite_line = $func_call_line.$args;
6060
// Ignore suggested line breaks within individual args; otherwise we could
6161
// split in the middle of arguments rather than after each parameter.

src/HackfmtFormatter.hack

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ final class HackfmtFormatter implements ICodegenFormatter {
4646
$exit_code === 0,
4747
'Failed to invoke hackfmt',
4848
);
49-
return \implode("\n", $output)."\n";
49+
return Str\join($output, "\n")."\n";
5050
}
5151

5252
<<__Memoize>>

src/PartiallyGeneratedCode.hack

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace Facebook\HackCodegen;
1111

12-
use namespace HH\Lib\C;
12+
use namespace HH\Lib\{C, Str};
1313

1414
/**
1515
* Manage partially generated code. The main operation is to merge existing
@@ -79,7 +79,7 @@ final class PartiallyGeneratedCode {
7979
}
8080
}
8181
if ($content) {
82-
$merged[] = \implode("\n\n", $content);
82+
$merged[] = Str\join($content, "\n\n");
8383
} else {
8484
// This manual section is new, so insert inside it the chunk from
8585
// the generated code (e.g. the generated code can have a comment
@@ -89,7 +89,7 @@ final class PartiallyGeneratedCode {
8989
}
9090
}
9191
}
92-
return \implode("\n", \array_filter($merged));
92+
return Str\join(\array_filter($merged), "\n");
9393
}
9494

9595
/**
@@ -117,7 +117,7 @@ final class PartiallyGeneratedCode {
117117
$generated[] = $chunk;
118118
}
119119
}
120-
return \implode("\n", $generated);
120+
return Str\join($generated, "\n");
121121
}
122122

123123
/**
@@ -150,7 +150,7 @@ final class PartiallyGeneratedCode {
150150
$lines = \explode("\n", $code);
151151
foreach ($lines as $line) {
152152
if (\strpos($line, self::$manualEnd) !== false) {
153-
yield tuple($current_id, \implode("\n", $chunk));
153+
yield tuple($current_id, Str\join($chunk, "\n"));
154154
$chunk = varray[$line];
155155
$current_id = null;
156156

@@ -167,7 +167,7 @@ final class PartiallyGeneratedCode {
167167
}
168168

169169
$chunk[] = $line;
170-
yield tuple(null, \implode("\n", $chunk));
170+
yield tuple(null, Str\join($chunk, "\n"));
171171
$chunk = varray[];
172172
$current_id = \trim(\preg_replace($begin, '\\1', $line));
173173

@@ -187,11 +187,10 @@ final class PartiallyGeneratedCode {
187187
);
188188
}
189189
if ($code !== '') {
190-
yield tuple(null, \implode("\n", $chunk));
190+
yield tuple(null, Str\join($chunk, "\n"));
191191
}
192192
}
193193
}
194194

195195
final class PartiallyGeneratedCodeException extends \Exception {
196196
}
197-
;

0 commit comments

Comments
 (0)