forked from hhvm/hack-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodegenClassWithInterfaces.hack
More file actions
80 lines (70 loc) · 2.03 KB
/
CodegenClassWithInterfaces.hack
File metadata and controls
80 lines (70 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
namespace Facebook\HackCodegen;
use namespace HH\Lib\{C, Vec};
/** Functionality shared by all class-like definitions that are able to
* implement interfaces.
*
* For example, classes and traits can implement interfaces, but enums
* can't.
*/
trait CodegenClassWithInterfaces {
require extends CodegenClassish;
private vec<CodegenImplementsInterface> $interfaces = vec[];
/** @selfdocumenting */
public function setInterfaces(
Traversable<CodegenImplementsInterface> $value,
): this {
invariant(
C\is_empty($this->interfaces),
'interfaces have already been set',
);
$this->interfaces = vec($value);
return $this;
}
/** @selfdocumenting */
public function addInterface(CodegenImplementsInterface $value): this {
$this->interfaces[] = $value;
return $this;
}
/** @selfdocumenting */
public function addInterfaces(
Traversable<CodegenImplementsInterface> $interfaces,
): this {
$this->interfaces = Vec\concat($this->interfaces, vec($interfaces));
return $this;
}
/** Return the list of interfaces implemented by the generated class */
public function getImplements(): vec<string> {
// Interface<T> becomes Interface
return Vec\map(
$this->interfaces,
$interface ==> {
$name = $interface->getName();
return \strstr($name, '<', true) ?: $name;
},
);
}
/** @selfdocumenting */
protected function renderInterfaceList(
HackBuilder $builder,
string $introducer,
): void {
if (!C\is_empty($this->interfaces)) {
$builder->newLine()->indent()->addLine($introducer);
$i = 0;
foreach ($this->interfaces as $interface) {
$i++;
$builder->addRenderer($interface);
$builder->addLineIf($i !== C\count($this->interfaces), ',');
}
$builder->unindent();
}
}
}