This repository was archived by the owner on Mar 6, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathMethodBuilder.php
More file actions
169 lines (135 loc) · 3.62 KB
/
Copy pathMethodBuilder.php
File metadata and controls
169 lines (135 loc) · 3.62 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace Phpactor\CodeBuilder\Domain\Builder;
use Phpactor\CodeBuilder\Domain\Prototype\UpdatePolicy;
use Phpactor\CodeBuilder\Domain\Prototype\Visibility;
use Phpactor\CodeBuilder\Domain\Prototype\Parameters;
use Phpactor\CodeBuilder\Domain\Prototype\Method;
use Phpactor\CodeBuilder\Domain\Prototype\ReturnType;
use Phpactor\CodeBuilder\Domain\Prototype\Docblock;
use Phpactor\CodeBuilder\Domain\Builder\Exception\InvalidBuilderException;
class MethodBuilder extends AbstractBuilder implements NamedBuilder
{
/**
* @var SourceCodeBuilder
*/
private $parent;
/**
* @var string
*/
protected $name;
/**
* @var Visibility
*/
protected $visibility;
/**
* @var ReturnType
*/
protected $returnType;
/**
* @var ParameterBuilder[]
*/
protected $parameters = [];
/**
* @var Docblock
*/
protected $docblock;
/**
* @var bool
*/
protected $static = false;
/**
* @var bool
*/
protected $abstract = false;
/**
* @var MethodBodyBuilder
*/
protected $bodyBuilder;
public function __construct(ClassLikeBuilder $parent, string $name)
{
$this->parent = $parent;
$this->name = $name;
$this->bodyBuilder = new MethodBodyBuilder($this);
}
public static function childNames(): array
{
return [
'parameters',
];
}
public function add(NamedBuilder $builder)
{
if ($builder instanceof ParameterBuilder) {
$this->parameters[$builder->builderName()] = $builder;
}
throw new InvalidBuilderException($this, $builder);
}
public function visibility(string $visibility): MethodBuilder
{
$this->visibility = Visibility::fromString($visibility);
return $this;
}
public function returnType(string $returnType): MethodBuilder
{
$this->returnType = ReturnType::fromString($returnType);
return $this;
}
public function parameter(string $name): ParameterBuilder
{
if (isset($this->parameters[$name])) {
return $this->parameters[$name];
}
$this->parameters[$name] = $builder = new ParameterBuilder($this, $name);
return $builder;
}
public function docblock(string $docblock): MethodBuilder
{
$this->docblock = Docblock::fromString($docblock);
return $this;
}
public function build()
{
$modifiers = 0;
if ($this->static) {
$modifiers = $modifiers|Method::IS_STATIC;
}
if ($this->abstract) {
$modifiers = $modifiers|Method::IS_ABSTRACT;
}
$methodBody = $this->bodyBuilder->build();
return new Method(
$this->name,
$this->visibility,
Parameters::fromParameters(array_map(function (ParameterBuilder $builder) {
return $builder->build();
}, $this->parameters)),
$this->returnType,
$this->docblock,
$modifiers,
$methodBody,
UpdatePolicy::fromModifiedState($this->isModified())
);
}
public function static(): MethodBuilder
{
$this->static = true;
return $this;
}
public function abstract(): MethodBuilder
{
$this->abstract = true;
return $this;
}
public function end(): ClassLikeBuilder
{
return $this->parent;
}
public function body(): MethodBodyBuilder
{
return $this->bodyBuilder;
}
public function builderName(): string
{
return $this->name;
}
}