Skip to content

Commit fa9d71a

Browse files
committed
Property::setValue() automatically turns on initialized state [Closes #71]
1 parent fe54415 commit fa9d71a

File tree

5 files changed

+13
-3
lines changed

5 files changed

+13
-3
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,9 @@ public function getProperty(string $name): Property
434434
*/
435435
public function addProperty(string $name, $value = null): Property
436436
{
437-
return $this->properties[$name] = (new Property($name))->setValue($value);
437+
return $this->properties[$name] = func_num_args() > 1
438+
? (new Property($name))->setValue($value)
439+
: new Property($name);
438440
}
439441

440442

src/PhpGenerator/Factory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ public function fromPropertyReflection(\ReflectionProperty $from): Property
200200
$prop->setType((string) $from->getType());
201201
}
202202
$prop->setInitialized($from->hasType() && array_key_exists($prop->getName(), $defaults));
203+
} else {
204+
$prop->setInitialized(false);
203205
}
204206
$prop->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
205207
$prop->setAttributes(self::getAttributes($from));

src/PhpGenerator/Property.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ final class Property
4545
public function setValue($val): self
4646
{
4747
$this->value = $val;
48+
$this->initialized = true;
4849
return $this;
4950
}
5051

@@ -107,6 +108,6 @@ public function setInitialized(bool $state = true): self
107108

108109
public function isInitialized(): bool
109110
{
110-
return $this->initialized;
111+
return $this->initialized || $this->value !== null;
111112
}
112113
}

tests/PhpGenerator/ClassType.phpt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ $class->addProperty('typed2')
6565
->setNullable()
6666
->setInitialized();
6767

68+
$class->addProperty('typed3')
69+
->setType(Type::ARRAY)
70+
->setValue(null);
71+
6872
$p = $class->addProperty('sections', ['first' => true])
6973
->setStatic(true);
7074

@@ -141,7 +145,7 @@ $class->setMethods(array_values($methods));
141145
Assert::same($methods, $class->getMethods());
142146

143147
$properties = $class->getProperties();
144-
Assert::count(5, $properties);
148+
Assert::count(6, $properties);
145149
$class->setProperties(array_values($properties));
146150
Assert::same($properties, $class->getProperties());
147151

tests/PhpGenerator/expected/ClassType.expect

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ abstract class Example extends ParentClass implements IExample, IOne
2222
public $order = RecursiveIteratorIterator::SELF_FIRST;
2323
public array $typed1;
2424
public ?array $typed2 = null;
25+
public array $typed3 = null;
2526
public static $sections = ['first' => true];
2627

2728

0 commit comments

Comments
 (0)