Skip to content

Add support for typed properties #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 54 additions & 11 deletions src/Code/PropertyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace OpenCodeModeling\CodeAst\Code;

use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Stmt\Property;

Expand All @@ -23,6 +24,11 @@
*/
final class PropertyGenerator extends AbstractMemberGenerator
{
/**
* @var TypeGenerator|null
*/
private $type;

/**
* @var ValueGenerator
*/
Expand All @@ -31,37 +37,61 @@ final class PropertyGenerator extends AbstractMemberGenerator
/**
* @var bool
*/
private $omitDefaultValue = false;
private $typed = false;

/**
* @param string $name
* @param ValueGenerator|string|array $defaultValue
* @param int $flags
*/
public function __construct($name = null, $defaultValue = null, $flags = self::FLAG_PRIVATE)
{
public function __construct(
string $name = null,
string $type = null,
$defaultValue = null,
bool $typed = false,
int $flags = self::FLAG_PRIVATE
) {
if (null !== $name) {
$this->setName($name);
}

if (null !== $type) {
$this->setType($type);
}

if (null !== $defaultValue) {
$this->setDefaultValue($defaultValue);
}

$this->typed = $typed;

if ($flags !== self::FLAG_PUBLIC) {
$this->setFlags($flags);
}
}

/**
* @param string $type
* @return ParameterGenerator
*/
public function setType($type): self
{
$this->type = TypeGenerator::fromTypeString($type);

return $this;
}

public function getType(): TypeGenerator
{
return $this->type;
}

/**
* @param ValueGenerator|mixed $defaultValue
* @param string $defaultValueType
* @param string $defaultValueType
*
* @return PropertyGenerator
*/
public function setDefaultValue(
$defaultValue,
$defaultValueType = ValueGenerator::TYPE_AUTO
): self {
if (! $defaultValue instanceof ValueGenerator) {
if (!$defaultValue instanceof ValueGenerator) {
$defaultValue = new ValueGenerator($defaultValue, $defaultValueType);
}

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

public function generate(): Property
{
$propComment = <<<EOF
/**
* @var {$this->type->type()}
*/
EOF;
$attributes = [];

if ($this->typed === false) {
$attributes = ['comments' => [new Doc($propComment)]];
}

return new Property(
$this->flags,
[
new Node\Stmt\PropertyProperty(
$this->name,
$this->defaultValue ? $this->defaultValue->generate() : null
),
]
],
$attributes,
$this->typed ? $this->type->type() : null
);
}
}
5 changes: 5 additions & 0 deletions src/Code/TypeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ private function __construct()
{
}

public function type(): string
{
return $this->type;
}

public function generate(): NodeAbstract
{
$nullable = $this->nullable ? '?' : '';
Expand Down