Skip to content

Commit e40ff6c

Browse files
committed
Add 'addVerbatim' to BaseCodeBuilder
fixes hhvm#107
1 parent eb2e5ff commit e40ff6c

File tree

3 files changed

+68
-15
lines changed

3 files changed

+68
-15
lines changed

src/BaseCodeBuilder.php

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,42 @@ final public function add(?string $code): this {
8181
return $this->addf('%s', $code);
8282
}
8383

84+
/**
85+
* Add the specified code with no additional processing.
86+
*
87+
* For example, if there is a newline, any following characters will not be
88+
* indented. This is useful for heredocs.
89+
*
90+
* @see addVerbatimf
91+
*/
92+
final public function addVerbatim(string $code): this {
93+
$this->code->append($code);
94+
return $this;
95+
}
96+
97+
/**
98+
* Add the specified code with a %-placeholder format string, but no further
99+
* processing.
100+
*
101+
* For example, if there is a newline, any following characters will not be
102+
* indented. This is useful for heredocs.
103+
*
104+
* @see addVerbatim
105+
*/
106+
final public function addVerbatimf(
107+
Str\SprintfFormatString $code,
108+
mixed ...$args
109+
): this {
110+
$this->code->append(\vsprintf($code, $args));
111+
return $this;
112+
}
113+
84114
/** Add code to the buffer, using a % placeholder format string. */
85-
final public function addf(Str\SprintfFormatString $code, mixed ...$args): this {
86-
return $this->addvf((string) $code, $args);
115+
final public function addf(
116+
Str\SprintfFormatString $code,
117+
mixed ...$args
118+
): this {
119+
return $this->addvf((string)$code, $args);
87120
}
88121

89122
/** Add code to the buffer, using a % placeholder format string and
@@ -111,10 +144,11 @@ final protected function addvf(string $code, array<mixed> $args): this {
111144
// if we're in a new line, insert indentation
112145
if ($this->isNewLine) {
113146
if ($this->indentationLevel !== 0) {
114-
if ($this->config->shouldUseTabs()){
147+
if ($this->config->shouldUseTabs()) {
115148
$this->code->append(Str\repeat("\t", $this->indentationLevel));
116149
} else {
117-
$n = $this->config->getSpacesPerIndentation() * $this->indentationLevel;
150+
$n =
151+
$this->config->getSpacesPerIndentation() * $this->indentationLevel;
118152
$this->code->append(Str\repeat(' ', $n));
119153
}
120154
}
@@ -129,10 +163,7 @@ final protected function addvf(string $code, array<mixed> $args): this {
129163
/**
130164
* If the condition is true, add code to the buffer; otherwise, do nothing.
131165
*/
132-
final public function addIf(
133-
bool $condition,
134-
string $code,
135-
): this {
166+
final public function addIf(bool $condition, string $code): this {
136167
if ($condition) {
137168
$this->add($code);
138169
}
@@ -149,7 +180,7 @@ final public function addIff(
149180
mixed ...$args
150181
): this {
151182
if ($condition) {
152-
$this->addvf((string) $code, $args);
183+
$this->addvf((string)$code, $args);
153184
}
154185
return $this;
155186
}
@@ -161,18 +192,15 @@ final protected function addLineImplvf(
161192
?string $code,
162193
array<mixed> $args,
163194
): this {
164-
return $this->addvf((string) $code, $args)->newLine();
195+
return $this->addvf((string)$code, $args)->newLine();
165196
}
166197

167198
/**
168199
* If the condition is true, append the code followed by a newline.
169200
*
170201
* @see addLineIff
171202
*/
172-
final public function addLineIf(
173-
bool $condition,
174-
string $code,
175-
): this {
203+
final public function addLineIf(bool $condition, string $code): this {
176204
return $this->addLineIff($condition, '%s', $code);
177205
}
178206

@@ -186,7 +214,7 @@ final public function addLineIff(
186214
mixed ...$args
187215
): this {
188216
if ($condition) {
189-
$this->addLineImplvf((string) $code, $args);
217+
$this->addLineImplvf((string)$code, $args);
190218
}
191219
return $this;
192220
}

tests/HackBuilderTest.codegen

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,14 @@ $foo = Vector {
235235
},
236236
};
237237

238+
!@#$%codegentest:testVerbatimString
239+
function foo(): void {
240+
$x = <<<EOF
241+
foo bar
242+
EOF;
243+
return $x;
244+
}
245+
238246
!@#$%codegentest:testWrappedStringDoNotIndent
239247
$this->callMethod(
240248
'This string is quite long so will spread across three lines but the user '.

tests/HackBuilderTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,23 @@ public function testWrappedStringMulti(): void {
155155
)->toBeUnchanged();
156156
}
157157

158+
public function testVerbatimString(): void {
159+
expect_with_context(
160+
static::class,
161+
$this
162+
->getHackBuilder()
163+
->addLine('function foo(): void {')
164+
->indent()
165+
->add('$x = ')
166+
->addVerbatim("<<<EOF\nfoo bar\nEOF;")
167+
->ensureNewLine()
168+
->addLine('return $x;')
169+
->unindent()
170+
->addLine('}')
171+
->getCode(),
172+
)->toBeUnchanged();
173+
}
174+
158175
public function testWrappedStringDoNotIndent(): void {
159176
expect_with_context(static::class,
160177
$this

0 commit comments

Comments
 (0)