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

Commit cb5b172

Browse files
author
amarcu
committed
nits
1 parent 632d490 commit cb5b172

File tree

7 files changed

+75
-55
lines changed

7 files changed

+75
-55
lines changed

src/BaseCodeBuilder.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,11 @@ final public function addIf(bool $condition, string $code, ...): this {
9999
return $this;
100100
}
101101

102-
final protected function addLineImpl(?string $code, array<mixed> $args): this {
103-
return $this
104-
->addv($code, $args)
105-
->newLine();
102+
final protected function addLineImpl(
103+
?string $code,
104+
array<mixed> $args,
105+
): this {
106+
return $this->addv($code, $args)->newLine();
106107
}
107108

108109
/**
@@ -135,6 +136,10 @@ final public function addLines(ConstVector<string> $lines): this {
135136
return $this;
136137
}
137138

139+
/**
140+
* Indicate that the code that will follow will be inside a function, so that
141+
* the indentation is taken into account for the max line length.
142+
*/
138143
final public function setIsInsideFunction(): this {
139144
$this->isInsideFunction = true;
140145
return $this;
@@ -208,6 +213,10 @@ final public function addWithSuggestedLineBreaks(
208213
return $this->add(implode("\n ", $final_lines->toArray()));
209214
}
210215

216+
/**
217+
* Similar to addWithSuggestedLineBreaks but allows to add more than one
218+
* line at a time. See that method for more information.
219+
*/
211220
final public function addLinesWithSuggestedLineBreaks(
212221
Vector<string> $lines,
213222
): this {

src/CodegenClass.php

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ final class CodegenClass
3030
private bool $isAbstract = false;
3131
private ?CodegenConstructor $constructor = null;
3232

33-
public function setIsFinal(): this {
34-
$this->isFinal = true;
33+
public function setIsFinal(bool $value = true): this {
34+
$this->isFinal = $value;
3535
return $this;
3636
}
3737

@@ -41,7 +41,6 @@ public function setIsAbstract(bool $value = true): this {
4141
}
4242

4343
public function setExtends(string $name): this {
44-
invariant($this->extendsClass === null, 'extends has already been set');
4544
$this->extendsClass = $name;
4645
return $this;
4746
}
@@ -50,48 +49,34 @@ public function getExtends(): ?string {
5049
return $this->extendsClass;
5150
}
5251

53-
public function getInheritedMethods(): Set<string> {
54-
$classname_to_methods = $classname ==> {
55-
try {
56-
return (new Vector((new ReflectionClass($classname))->getMethods()))
57-
->filter($m ==> !$m->isPrivate())
58-
->map($m ==> $m->getName());
59-
} catch (ReflectionException $e) {
60-
// The class doesn't exist (often seen in unit tests).
61-
// Well, I guess it doesn't have any methods then.
62-
return Set {};
63-
}
64-
};
65-
66-
$methods = Set {};
67-
68-
$methods->addAll($classname_to_methods($this->getExtends()));
69-
foreach ($this->getImplements() as $interface) {
70-
$methods->addAll($classname_to_methods($interface));
71-
}
72-
foreach ($this->getUses() as $trait) {
73-
$methods->addAll($classname_to_methods($trait));
74-
}
75-
76-
$dynamic_yield_methods = $methods->filter(
77-
$method_name ==> Str::startsWith($method_name, 'gen')
78-
)->map($gen_method_name ==>
79-
'get' . Str::substr($gen_method_name, 3, Str::len($gen_method_name)-3)
80-
);
81-
82-
return $methods->addAll($dynamic_yield_methods);
83-
}
8452

8553
public function setConstructor(CodegenConstructor $constructor): this {
8654
$this->constructor = $constructor;
8755
return $this;
8856
}
8957

58+
/**
59+
* Add a comment before the class. Notice that you need to pass the
60+
* comment characters. Use this just for HH_FIXME or other ad-hoc uses.
61+
* For commenting the class, use method setDocBlock.
62+
* Example (a fake space was included between / and * to avoid a hack error
63+
* in this comment, but normally you won't have it):
64+
*
65+
* $class->addDeclComment('/ * HH_FIXME[4040] * /');
66+
*/
9067
final public function addDeclComment(string $comment): this {
9168
$this->declComment .= $comment."\n";
9269
return $this;
9370
}
9471

72+
/**
73+
* Add a function to wrap calls to the class. E.g., for MyClass accepting
74+
* a string parameter it would generate:
75+
*
76+
* function MyClass(string $s): MyClass {
77+
* return new MyClass($s);
78+
* }
79+
*/
9580
public function addConstructorWrapperFunc(
9681
?Vector<string> $params = null,
9782
): this {

src/CodegenClassBase.php

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ public function getName(): string {
4343
return $this->name;
4444
}
4545

46+
/**
47+
* E.g.
48+
* $class->setGenericsDecl(Map {'TRead' => null, 'TWrite' => 'T'})
49+
*
50+
* Will generate:
51+
* class MyClass<TRead, TWrite as T> {
52+
*/
4653
public function setGenericsDecl(Map<string, ?string> $generics_decl): this {
4754
$this->genericsDecl = $generics_decl;
4855
return $this;
@@ -66,14 +73,12 @@ public function setDocBlock(string $comment): this {
6673
return $this;
6774
}
6875

69-
public function setGeneratedFrom(
70-
CodegenGeneratedFrom $from
71-
): this {
76+
public function setGeneratedFrom(CodegenGeneratedFrom $from): this {
7277
$this->generatedFrom = $from;
7378
return $this;
7479
}
7580

76-
public function getUses(): Vector<string> {
81+
protected function getTraits(): Vector<string> {
7782
// Trait<T> becomes Trait
7883
return $this->traits
7984
->map($trait ==> {
@@ -135,17 +140,27 @@ public function addAbstractTypeConst(
135140
public function addClassNameConst(
136141
string $type,
137142
string $name,
138-
string $value,
139143
?string $comment = null,
140144
): this {
141145
return $this->addConst(
142-
'classname<'.$type.'> '.$name,
143-
$value,
146+
sprintf('classname<%s> %s', $type, $name),
147+
sprintf('%s::class', $type),
144148
$comment,
145149
HackBuilderValues::LITERAL,
146150
);
147151
}
148152

153+
public function addAbstractClassNameConst(
154+
string $type,
155+
string $name,
156+
?string $comment = null,
157+
): this {
158+
return $this->addAbstractConst(
159+
sprintf('classname<%s> %s', $type, $name),
160+
$comment,
161+
);
162+
}
163+
149164
public function addConst(
150165
string $name,
151166
mixed $value,
@@ -172,6 +187,13 @@ public function addVar(CodegenMemberVar $var): this {
172187
return $this;
173188
}
174189

190+
/**
191+
* If value is set to true, the class will have a section for manually adding
192+
* methods. You may specify a name for the section, which will appear in
193+
* the comment and is used to merge the code when re-generating it.
194+
* You may also specify a default content for the manual section, e.g.
195+
* a comment indicating that additional methods should be placed there.
196+
*/
175197
public function setHasManualMethodSection(
176198
bool $value = true,
177199
?string $name = null,
@@ -183,6 +205,13 @@ public function setHasManualMethodSection(
183205
return $this;
184206
}
185207

208+
/**
209+
* If value is set to true, the class will have a section for manually adding
210+
* declarations. You may specify a name for the section, which will appear in
211+
* the comment and is used to merge the code when re-generating it.
212+
* You may also specify a default content for the manual section, e.g.
213+
* a comment indicating that additional declarations should be placed there.
214+
*/
186215
public function setHasManualDeclarations(
187216
bool $value = true,
188217
?string $name = null,

src/CodegenFunctionBase.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
/**
12-
* Base class to generate a function
12+
* Base class to generate a function or a method.
1313
*/
1414
abstract class CodegenFunctionBase
1515
implements ICodeBuilderRenderer {
@@ -104,9 +104,7 @@ public function setDocBlock(string $comment): this {
104104
return $this;
105105
}
106106

107-
public function setGeneratedFrom(
108-
CodegenGeneratedFrom $from
109-
): this {
107+
public function setGeneratedFrom(CodegenGeneratedFrom $from): this {
110108
$this->generatedFrom = $from;
111109
return $this;
112110
}

src/CodegenImplementsInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ public function setComment(string $format, ...): this {
4040
return $this;
4141
}
4242

43-
public function setGeneratedFrom(
44-
CodegenGeneratedFrom $from
45-
): this {
43+
public function setGeneratedFrom(CodegenGeneratedFrom $from): this {
4644
$this->setComment($from->render());
4745
return $this;
4846
}

src/CodegenTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public function addRequireInterface(string $interface): this {
3434
}
3535

3636
protected function buildDeclaration(HackBuilder $builder): void {
37-
$builder->add('trait ' . $this->name);
37+
$generics_dec = $this->buildGenericsDeclaration();
38+
$builder->add('trait ' . $this->name.$generics_dec);
3839
$this->renderInterfaceList($builder, 'implements');
3940
}
4041

src/HackBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ public function endForeachLoop(): this {
511511
*
512512
* example:
513513
*
514-
* php_builder()
514+
* hack_builder()
515515
* ->startSwitch('$soccer_player')
516516
* ->addCaseBlocks(
517517
* $players,
@@ -759,6 +759,6 @@ private function assertIsVariable(string $name): void {
759759
}
760760
}
761761

762-
function hack_builder() : HackBuilder {
762+
function hack_builder(): HackBuilder {
763763
return new HackBuilder(HackCodegenConfig::getInstance());
764764
}

0 commit comments

Comments
 (0)