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

Commit 6efa476

Browse files
Add support for php and decl file types
1 parent b39cb55 commit 6efa476

File tree

3 files changed

+76
-8
lines changed

3 files changed

+76
-8
lines changed

src/CodegenFile.php

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,21 @@ enum CodegenFileResult: int {
1616
CREATE = 2;
1717
}
1818

19+
enum CodegenFileType: int {
20+
PHP = 0;
21+
HACK_DECL = 1;
22+
HACK_PARTIAL = 2;
23+
HACK_STRICT = 3;
24+
}
25+
1926
/**
2027
* File of generated code. The file is composed by classes.
2128
* The file will be signed, either as autogenerated or partially generated,
2229
* depending on whether there are manual sections.
2330
*/
2431
final class CodegenFile {
2532

26-
private bool $isStrict = false;
33+
private CodegenFileType $fileType = CodegenFileType::HACK_PARTIAL;
2734
private ?string $docBlock;
2835
private string $fileName;
2936
private string $relativeFileName;
@@ -175,10 +182,20 @@ public function rekeyManualSection(string $old_key, string $new_key): this {
175182
}
176183

177184
/**
178-
* Whether the generated file will be Hack strict mode
185+
* Whether the generated file will be Hack strict mode or partial mode.
186+
* For more flexibility, use setFileType.
179187
*/
180188
public function setIsStrict(bool $value): this {
181-
$this->isStrict = $value;
189+
if ($value) {
190+
$this->setFileType(CodegenFileType::HACK_STRICT);
191+
} else {
192+
$this->setFileType(CodegenFileType::HACK_PARTIAL);
193+
}
194+
return $this;
195+
}
196+
197+
public function setFileType(CodegenFileType $type): this {
198+
$this->fileType = $type;
182199
return $this;
183200
}
184201

@@ -202,14 +219,23 @@ public function getFormatter(): ?ICodegenFormatter {
202219
}
203220

204221

222+
private function getFileTypeDeclaration(): string {
223+
switch($this->fileType) {
224+
case CodegenFileType::PHP:
225+
return '<?php';
226+
case CodegenFileType::HACK_DECL:
227+
return '<?hh // decl';
228+
case CodegenFileType::HACK_PARTIAL:
229+
return '<?hh';
230+
case CodegenFileType::HACK_STRICT:
231+
return '<?hh // strict';
232+
}
233+
}
234+
205235
public function render(): string {
206236
$builder = hack_builder();
207237

208-
$first_line = '<?hh';
209-
if ($this->isStrict) {
210-
$first_line .= ' // strict';
211-
}
212-
$builder->addLine($first_line);
238+
$builder->addLine($this->getFileTypeDeclaration());
213239
$header = $this->config->getFileHeader();
214240
if ($header) {
215241
foreach ($header as $line) {

test/CodegenFileTestCase.codegen

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ class PartiallyGenerated {
8383
class PartiallyGeneratedLoader {
8484
}
8585

86+
!@#$%codegentest:testPhpFile
87+
<?php
88+
// Codegen Tests
89+
/**
90+
* This file is generated. Do not modify it manually!
91+
*
92+
* @-generated SignedSource<<75ccd5361b4a4337b9ded246a44f3b4c>>
93+
*/
94+
95+
class Foo {
96+
}
97+
8698
!@#$%codegentest:testSaveAutogenerated
8799
<?hh
88100
// Codegen Tests
@@ -122,3 +134,15 @@ class Demo {
122134
}
123135
}
124136

137+
!@#$%codegentest:testStrictFile
138+
<?hh // strict
139+
// Codegen Tests
140+
/**
141+
* This file is generated. Do not modify it manually!
142+
*
143+
* @-generated SignedSource<<5c0182dcd097a22f08d4dfbcc555c52c>>
144+
*/
145+
146+
class Foo {
147+
}
148+

test/CodegenFileTestCase.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,22 @@ public function testNamespace() {
245245
self::assertUnchanged($code);
246246
}
247247

248+
public function testStrictFile() {
249+
$code = test_codegen_file('no_file')
250+
->setIsStrict(true)
251+
->addClass(codegen_class('Foo'))
252+
->render();
253+
254+
self::assertUnchanged($code);
255+
}
256+
257+
public function testPhpFile() {
258+
$code = test_codegen_file('no_file')
259+
->setFileType(CodegenFileType::PHP)
260+
->addClass(codegen_class('Foo'))
261+
->render();
262+
263+
self::assertUnchanged($code);
264+
}
265+
248266
}

0 commit comments

Comments
 (0)