Skip to content

Commit 7a528cf

Browse files
authored
Merge pull request #1 from open-code-modeling/feature/typed-property
Add support for typed properties
2 parents 1036210 + 9be0cbb commit 7a528cf

File tree

2 files changed

+59
-11
lines changed

2 files changed

+59
-11
lines changed

src/Code/PropertyGenerator.php

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace OpenCodeModeling\CodeAst\Code;
1212

13+
use PhpParser\Comment\Doc;
1314
use PhpParser\Node;
1415
use PhpParser\Node\Stmt\Property;
1516

@@ -23,6 +24,11 @@
2324
*/
2425
final class PropertyGenerator extends AbstractMemberGenerator
2526
{
27+
/**
28+
* @var TypeGenerator|null
29+
*/
30+
private $type;
31+
2632
/**
2733
* @var ValueGenerator
2834
*/
@@ -31,37 +37,61 @@ final class PropertyGenerator extends AbstractMemberGenerator
3137
/**
3238
* @var bool
3339
*/
34-
private $omitDefaultValue = false;
40+
private $typed = false;
3541

36-
/**
37-
* @param string $name
38-
* @param ValueGenerator|string|array $defaultValue
39-
* @param int $flags
40-
*/
41-
public function __construct($name = null, $defaultValue = null, $flags = self::FLAG_PRIVATE)
42-
{
42+
public function __construct(
43+
string $name = null,
44+
string $type = null,
45+
$defaultValue = null,
46+
bool $typed = false,
47+
int $flags = self::FLAG_PRIVATE
48+
) {
4349
if (null !== $name) {
4450
$this->setName($name);
4551
}
52+
53+
if (null !== $type) {
54+
$this->setType($type);
55+
}
56+
4657
if (null !== $defaultValue) {
4758
$this->setDefaultValue($defaultValue);
4859
}
60+
61+
$this->typed = $typed;
62+
4963
if ($flags !== self::FLAG_PUBLIC) {
5064
$this->setFlags($flags);
5165
}
5266
}
5367

68+
/**
69+
* @param string $type
70+
* @return ParameterGenerator
71+
*/
72+
public function setType($type): self
73+
{
74+
$this->type = TypeGenerator::fromTypeString($type);
75+
76+
return $this;
77+
}
78+
79+
public function getType(): TypeGenerator
80+
{
81+
return $this->type;
82+
}
83+
5484
/**
5585
* @param ValueGenerator|mixed $defaultValue
56-
* @param string $defaultValueType
86+
* @param string $defaultValueType
5787
*
5888
* @return PropertyGenerator
5989
*/
6090
public function setDefaultValue(
6191
$defaultValue,
6292
$defaultValueType = ValueGenerator::TYPE_AUTO
6393
): self {
64-
if (! $defaultValue instanceof ValueGenerator) {
94+
if (!$defaultValue instanceof ValueGenerator) {
6595
$defaultValue = new ValueGenerator($defaultValue, $defaultValueType);
6696
}
6797

@@ -80,14 +110,27 @@ public function getDefaultValue(): ValueGenerator
80110

81111
public function generate(): Property
82112
{
113+
$propComment = <<<EOF
114+
/**
115+
* @var {$this->type->type()}
116+
*/
117+
EOF;
118+
$attributes = [];
119+
120+
if ($this->typed === false) {
121+
$attributes = ['comments' => [new Doc($propComment)]];
122+
}
123+
83124
return new Property(
84125
$this->flags,
85126
[
86127
new Node\Stmt\PropertyProperty(
87128
$this->name,
88129
$this->defaultValue ? $this->defaultValue->generate() : null
89130
),
90-
]
131+
],
132+
$attributes,
133+
$this->typed ? $this->type->type() : null
91134
);
92135
}
93136
}

src/Code/TypeGenerator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ private function __construct()
108108
{
109109
}
110110

111+
public function type(): string
112+
{
113+
return $this->type;
114+
}
115+
111116
public function generate(): NodeAbstract
112117
{
113118
$nullable = $this->nullable ? '?' : '';

0 commit comments

Comments
 (0)