Skip to content

Commit a416ee6

Browse files
amarcugauravkdeo
authored andcommitted
nits
1 parent 3a7c07e commit a416ee6

7 files changed

Lines changed: 75 additions & 56 deletions

src/BaseCodeBuilder.php

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

104-
final protected function addLineImpl(?string $code, array<mixed> $args): this {
105-
return $this
106-
->addv($code, $args)
107-
->newLine();
104+
final protected function addLineImpl(
105+
?string $code,
106+
array<mixed> $args,
107+
): this {
108+
return $this->addv($code, $args)->newLine();
108109
}
109110

110111
/**
@@ -137,6 +138,10 @@ final public function addLines(\ConstVector<string> $lines): this {
137138
return $this;
138139
}
139140

141+
/**
142+
* Indicate that the code that will follow will be inside a function, so that
143+
* the indentation is taken into account for the max line length.
144+
*/
140145
final public function setIsInsideFunction(): this {
141146
$this->isInsideFunction = true;
142147
return $this;
@@ -210,6 +215,10 @@ final public function addWithSuggestedLineBreaks(
210215
return $this->add(implode("\n ", $final_lines->toArray()));
211216
}
212217

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

src/CodegenClass.php

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

35-
public function setIsFinal(): this {
36-
$this->isFinal = true;
35+
public function setIsFinal(bool $value = true): this {
36+
$this->isFinal = $value;
3737
return $this;
3838
}
3939

@@ -43,7 +43,6 @@ public function setIsAbstract(bool $value = true): this {
4343
}
4444

4545
public function setExtends(string $name): this {
46-
invariant($this->extendsClass === null, 'extends has already been set');
4746
$this->extendsClass = $name;
4847
return $this;
4948
}
@@ -52,48 +51,33 @@ public function getExtends(): ?string {
5251
return $this->extendsClass;
5352
}
5453

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

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

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

src/CodegenClassBase.php

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

48+
/**
49+
* E.g.
50+
* $class->setGenericsDecl(Map {'TRead' => null, 'TWrite' => 'T'})
51+
*
52+
* Will generate:
53+
* class MyClass<TRead, TWrite as T> {
54+
*/
4855
public function setGenericsDecl(Map<string, ?string> $generics_decl): this {
4956
$this->genericsDecl = $generics_decl;
5057
return $this;
@@ -68,14 +75,12 @@ public function setDocBlock(string $comment): this {
6875
return $this;
6976
}
7077

71-
public function setGeneratedFrom(
72-
CodegenGeneratedFrom $from
73-
): this {
78+
public function setGeneratedFrom(CodegenGeneratedFrom $from): this {
7479
$this->generatedFrom = $from;
7580
return $this;
7681
}
7782

78-
public function getUses(): Vector<string> {
83+
protected function getTraits(): Vector<string> {
7984
// Trait<T> becomes Trait
8085
return $this->traits
8186
->map($trait ==> {
@@ -137,17 +142,27 @@ public function addAbstractTypeConst(
137142
public function addClassNameConst(
138143
string $type,
139144
string $name,
140-
string $value,
141145
?string $comment = null,
142146
): this {
143147
return $this->addConst(
144-
'classname<'.$type.'> '.$name,
145-
$value,
148+
sprintf('classname<%s> %s', $type, $name),
149+
sprintf('%s::class', $type),
146150
$comment,
147151
HackBuilderValues::LITERAL,
148152
);
149153
}
150154

155+
public function addAbstractClassNameConst(
156+
string $type,
157+
string $name,
158+
?string $comment = null,
159+
): this {
160+
return $this->addAbstractConst(
161+
sprintf('classname<%s> %s', $type, $name),
162+
$comment,
163+
);
164+
}
165+
151166
public function addConst(
152167
string $name,
153168
mixed $value,
@@ -174,6 +189,13 @@ public function addVar(CodegenMemberVar $var): this {
174189
return $this;
175190
}
176191

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

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

src/CodegenFunctionBase.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namespace Facebook\HackCodegen;
1212

1313
/**
14-
* Base class to generate a function
14+
* Base class to generate a function or a method.
1515
*/
1616
abstract class CodegenFunctionBase
1717
implements ICodeBuilderRenderer {
@@ -106,9 +106,7 @@ public function setDocBlock(string $comment): this {
106106
return $this;
107107
}
108108

109-
public function setGeneratedFrom(
110-
CodegenGeneratedFrom $from
111-
): this {
109+
public function setGeneratedFrom(CodegenGeneratedFrom $from): this {
112110
$this->generatedFrom = $from;
113111
return $this;
114112
}

src/CodegenImplementsInterface.php

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

45-
public function setGeneratedFrom(
46-
CodegenGeneratedFrom $from
47-
): this {
45+
public function setGeneratedFrom(CodegenGeneratedFrom $from): this {
4846
$this->setComment($from->render());
4947
return $this;
5048
}

src/CodegenTrait.php

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

3838
protected function buildDeclaration(HackBuilder $builder): void {
39-
$builder->add('trait ' . $this->name);
39+
$generics_dec = $this->buildGenericsDeclaration();
40+
$builder->add('trait ' . $this->name.$generics_dec);
4041
$this->renderInterfaceList($builder, 'implements');
4142
}
4243

src/HackBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ public function endForeachLoop(): this {
513513
*
514514
* example:
515515
*
516-
* php_builder()
516+
* hack_builder()
517517
* ->startSwitch('$soccer_player')
518518
* ->addCaseBlocks(
519519
* $players,
@@ -761,6 +761,6 @@ private function assertIsVariable(string $name): void {
761761
}
762762
}
763763

764-
function hack_builder() : HackBuilder {
764+
function hack_builder(): HackBuilder {
765765
return new HackBuilder(HackCodegenConfig::getInstance());
766766
}

0 commit comments

Comments
 (0)