Skip to content

Commit e3f4347

Browse files
committed
Add high level API for properties - Close #16
1 parent 39c024a commit e3f4347

File tree

5 files changed

+323
-7
lines changed

5 files changed

+323
-7
lines changed

src/Builder/ClassBuilder.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ final class ClassBuilder
5757
/** @var ClassConstBuilder[] */
5858
private $constants = [];
5959

60+
/** @var ClassPropertyBuilder[] */
61+
private $properties = [];
62+
6063
private function __construct()
6164
{
6265
}
@@ -143,6 +146,13 @@ public function setConstants(ClassConstBuilder ...$constants): self
143146
return $this;
144147
}
145148

149+
public function setProperties(ClassPropertyBuilder ...$properties): self
150+
{
151+
$this->properties = $properties;
152+
153+
return $this;
154+
}
155+
146156
public function getNamespace(): ?string
147157
{
148158
return $this->namespace;
@@ -210,6 +220,14 @@ public function getConstants(): array
210220
return $this->constants;
211221
}
212222

223+
/**
224+
* @return ClassPropertyBuilder[]
225+
*/
226+
public function getProperties(): array
227+
{
228+
return $this->properties;
229+
}
230+
213231
/**
214232
* @return NodeVisitor[]
215233
*/
@@ -252,6 +270,17 @@ static function (ClassConstBuilder $const) {
252270
)
253271
);
254272
}
273+
if (\count($this->properties) > 0) {
274+
\array_push(
275+
$visitors,
276+
...\array_map(
277+
static function (ClassPropertyBuilder $property) {
278+
return $property->generate();
279+
},
280+
$this->properties
281+
)
282+
);
283+
}
255284

256285
return $visitors;
257286
}
@@ -316,6 +345,9 @@ static function (Node\Name $name) {
316345
case $node instanceof Node\Stmt\ClassConst:
317346
$this->constants[] = ClassConstBuilder::fromNode($node);
318347
break;
348+
case $node instanceof Node\Stmt\Property:
349+
$this->properties[] = ClassPropertyBuilder::fromNode($node);
350+
break;
319351
default:
320352
break;
321353
}

src/Builder/ClassConstBuilder.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use OpenCodeModeling\CodeAst\Code\ClassConstGenerator;
1414
use OpenCodeModeling\CodeAst\NodeVisitor\ClassConstant;
1515
use PhpParser\Node;
16+
use PhpParser\NodeTraverser;
1617
use PhpParser\NodeVisitor;
1718

1819
final class ClassConstBuilder
@@ -54,6 +55,19 @@ public static function fromScratch(string $name, $value, $visibility = ClassCons
5455
return $self;
5556
}
5657

58+
public function getName(): string
59+
{
60+
return $this->name;
61+
}
62+
63+
/**
64+
* @return mixed
65+
*/
66+
public function getValue()
67+
{
68+
return $this->value;
69+
}
70+
5771
public function setPrivate(): self
5872
{
5973
$this->visibility = ClassConstGenerator::FLAG_PRIVATE;
@@ -79,4 +93,9 @@ public function generate(): NodeVisitor
7993
{
8094
return ClassConstant::forClassConstant($this->name, $this->value, $this->visibility);
8195
}
96+
97+
public function injectVisitors(NodeTraverser $nodeTraverser): void
98+
{
99+
$nodeTraverser->addVisitor($this->generate());
100+
}
82101
}

src/Builder/ClassPropertyBuilder.php

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<?php
2+
3+
/**
4+
* @see https://github.com/open-code-modeling/php-code-ast for the canonical source repository
5+
* @copyright https://github.com/open-code-modeling/php-code-ast/blob/master/COPYRIGHT.md
6+
* @license https://github.com/open-code-modeling/php-code-ast/blob/master/LICENSE.md MIT License
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace OpenCodeModeling\CodeAst\Builder;
12+
13+
use OpenCodeModeling\CodeAst\Code\ClassConstGenerator;
14+
use OpenCodeModeling\CodeAst\Code\DocBlock\DocBlock;
15+
use OpenCodeModeling\CodeAst\Code\PropertyGenerator;
16+
use OpenCodeModeling\CodeAst\NodeVisitor\Property;
17+
use PhpParser\Node;
18+
use PhpParser\NodeTraverser;
19+
use PhpParser\NodeVisitor;
20+
21+
final class ClassPropertyBuilder
22+
{
23+
/** @var string */
24+
private $name;
25+
26+
/** @var string */
27+
private $type;
28+
29+
/** @var mixed */
30+
private $defaultValue;
31+
32+
/**
33+
* @var int
34+
*/
35+
private $visibility;
36+
37+
/** @var bool */
38+
private $typed = false;
39+
40+
/**
41+
* @var string|null
42+
*/
43+
private $docBlockComment;
44+
45+
/**
46+
* @var string|null
47+
*/
48+
private $typeDocBlockHint;
49+
50+
/**
51+
* @var DocBlock|null
52+
*/
53+
private $docBlock;
54+
55+
private function __construct()
56+
{
57+
}
58+
59+
public static function fromNode(Node\Stmt\Property $node): self
60+
{
61+
$self = new self();
62+
63+
$self->name = $node->props[0]->name->name;
64+
$self->defaultValue = $node->props[0]->default;
65+
$self->type = $node->type->toString();
66+
$self->visibility = $node->flags;
67+
68+
if ($self->type !== null) {
69+
$self->typed = true;
70+
}
71+
72+
return $self;
73+
}
74+
75+
public static function fromScratch(string $name, $type, bool $typed = true): self
76+
{
77+
$self = new self();
78+
$self->name = $name;
79+
$self->type = $type;
80+
$self->typed = $typed;
81+
$self->visibility = ClassConstGenerator::FLAG_PRIVATE;
82+
83+
return $self;
84+
}
85+
86+
public function getName(): string
87+
{
88+
return $this->name;
89+
}
90+
91+
public function getType(): string
92+
{
93+
return $this->type;
94+
}
95+
96+
public function isTyped(): bool
97+
{
98+
return $this->typed;
99+
}
100+
101+
public function setPrivate(): self
102+
{
103+
$this->visibility = ClassConstGenerator::FLAG_PRIVATE;
104+
105+
return $this;
106+
}
107+
108+
public function setProtected(): self
109+
{
110+
$this->visibility = ClassConstGenerator::FLAG_PROTECTED;
111+
112+
return $this;
113+
}
114+
115+
public function setPublic(): self
116+
{
117+
$this->visibility = ClassConstGenerator::FLAG_PUBLIC;
118+
119+
return $this;
120+
}
121+
122+
public function getDocBlockComment(): ?string
123+
{
124+
return $this->docBlockComment;
125+
}
126+
127+
public function setDocBlockComment(?string $docBlockComment): void
128+
{
129+
$this->docBlockComment = $docBlockComment;
130+
}
131+
132+
public function getTypeDocBlockHint(): string
133+
{
134+
return $this->typeDocBlockHint;
135+
}
136+
137+
public function setTypeDocBlockHint(?string $typeDocBlockHint): void
138+
{
139+
$this->typeDocBlockHint = $typeDocBlockHint;
140+
}
141+
142+
public function getDocBlock(): ?DocBlock
143+
{
144+
return $this->docBlock;
145+
}
146+
147+
public function overrideDocBlock(?DocBlock $docBlock): void
148+
{
149+
$this->docBlock = $docBlock;
150+
}
151+
152+
public function generate(): NodeVisitor
153+
{
154+
return new Property($this->propertyGenerator());
155+
}
156+
157+
private function propertyGenerator(): PropertyGenerator
158+
{
159+
$flags = $this->visibility;
160+
161+
$propertyGenerator = new PropertyGenerator($this->name, $this->type, $this->defaultValue, $this->typed, $flags);
162+
163+
$propertyGenerator->setDocBlockComment($this->docBlockComment);
164+
$propertyGenerator->setTypeDocBlockHint($this->typeDocBlockHint);
165+
$propertyGenerator->overrideDocBlock($this->docBlock);
166+
167+
return $propertyGenerator;
168+
}
169+
170+
public function injectVisitors(NodeTraverser $nodeTraverser): void
171+
{
172+
$nodeTraverser->addVisitor($this->generate());
173+
}
174+
}

src/Code/PropertyGenerator.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ final class PropertyGenerator extends AbstractMemberGenerator
4747
private $docBlockComment;
4848

4949
/**
50-
* @var string
50+
* @var string|null
5151
*/
5252
private $typeDocBlockHint;
5353

@@ -107,9 +107,9 @@ public function setDocBlockComment(?string $docBlockComment): void
107107
/**
108108
* Ignores generation of the doc block and uses provided doc block instead.
109109
*
110-
* @param DocBlock $docBlock
110+
* @param DocBlock|null $docBlock
111111
*/
112-
public function overrideDocBlock(DocBlock $docBlock): void
112+
public function overrideDocBlock(?DocBlock $docBlock): void
113113
{
114114
$this->docBlock = $docBlock;
115115
}
@@ -149,10 +149,7 @@ public function getTypeDocBlockHint(): ?string
149149
return $this->typeDocBlockHint;
150150
}
151151

152-
/**
153-
* @param string $typeDocBlockHint
154-
*/
155-
public function setTypeDocBlockHint(string $typeDocBlockHint): void
152+
public function setTypeDocBlockHint(?string $typeDocBlockHint): void
156153
{
157154
$this->typeDocBlockHint = $typeDocBlockHint;
158155
}

0 commit comments

Comments
 (0)