@@ -22,6 +22,7 @@ abstract class CodegenFunctionish implements ICodeBuilderRenderer {
2222 protected string $name ;
2323 protected ?string $body = null ;
2424 protected ?string $docBlock = null ;
25+ protected ?keyset < string > $contexts = null ;
2526 protected ?string $returnType = null ;
2627 private ?string $fixme = null ;
2728 protected bool $isAsync = false ;
@@ -53,6 +54,28 @@ abstract class CodegenFunctionish implements ICodeBuilderRenderer {
5354 return $this ;
5455 }
5556
57+ public function addContext (
58+ string $context ,
59+ ): this {
60+ if ($this -> contexts === null ) {
61+ $this -> contexts = keyset < string > [$context ];
62+ } else {
63+ $this -> contexts [] = $context ;
64+ }
65+
66+ return $this ;
67+ }
68+
69+ public function addContexts (
70+ Traversable <string > $contexts ,
71+ ): this {
72+ foreach ($contexts as $context ) {
73+ $this -> addContext($context );
74+ }
75+
76+ return $this ;
77+ }
78+
5679 public function setReturnType (string $type ): this {
5780 return $this -> setReturnTypef(' %s' , $type );
5881 }
@@ -129,6 +152,10 @@ abstract class CodegenFunctionish implements ICodeBuilderRenderer {
129152 return $this -> parameters ;
130153 }
131154
155+ public function getContexts (): ?keyset <string > {
156+ return $this -> contexts ;
157+ }
158+
132159 public function getReturnType (): ?string {
133160 return $this -> returnType ;
134161 }
@@ -151,6 +178,7 @@ abstract class CodegenFunctionish implements ICodeBuilderRenderer {
151178 $builder = (new HackBuilder ($this -> config ))
152179 -> add($keywords )
153180 -> addf(' %s(%s)' , $this -> name , Str \join ($this -> parameters , ' , ' ))
181+ -> addIf($this -> contexts !== null , ' [' . Str \join ($this -> contexts ?? keyset [], ' , ' ) . ' ]' )
154182 -> addIf($this -> returnType !== null , ' : ' . ($this -> returnType ?? ' ' ));
155183
156184 $code = $builder -> getCode();
@@ -183,6 +211,7 @@ abstract class CodegenFunctionish implements ICodeBuilderRenderer {
183211 -> addLines($parameter_lines )
184212 -> unindent()
185213 -> add(' )' )
214+ -> addIf($this -> contexts !== null , ' [' . Str \join ($this -> contexts ?? keyset [], ' , ' ) . ' ]' )
186215 -> addIf($this -> returnType !== null , ' : ' . ($this -> returnType ?? ' ' ));
187216
188217 return $multi_line_builder -> getCode();
0 commit comments