Skip to content

Commit

Permalink
some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdarin committed Jan 27, 2024
1 parent 4957364 commit b0c8f2f
Show file tree
Hide file tree
Showing 18 changed files with 251 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# php-entity-renderer
Renderer helpers to make PHP code for classes, intefaces, traits and enums
Renderer helpers to make PHP code for classes, interfaces, traits and enums
19 changes: 1 addition & 18 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "darkdarin/php-entity-renderer",
"description": "Renderer helpers to make PHP code for classes, intefaces, traits and enums",
"description": "Renderer helpers to make PHP code for classes, interfaces, traits and enums",
"license": "BSD-3-Clause",
"type": "package",
"authors": [
Expand All @@ -19,23 +19,6 @@
},
"require-dev": {
"laravel/pint": "^1.1",
"illuminate/support": "^10.0",
"hyperf/di": "~3.0.0",
"roave/security-advisories": "dev-latest"
},
"extra": {
"laravel": {
"providers": [
"DarkDarin\\VkOrdSdk\\VkOrdSdkServiceProvider"
]
},
"hyperf": {
"config": "DarkDarin\\VkOrdSdk\\ConfigProvider"
}
},
"config": {
"allow-plugins": {
"php-http/discovery": false
}
}
}
3 changes: 3 additions & 0 deletions src/Contracts/TypeRendererInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@

interface TypeRendererInterface extends EntityRendererInterface, DocBlockRendererInterface
{
public function setNullable(bool $nullable = true): TypeRendererInterface;

public function isNullable(): bool;
}
3 changes: 2 additions & 1 deletion src/Renderers/AbstractEntityRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public function render(EntityAliases $entityAliases): string
$namespace = 'namespace ' . $namespace . ';' . PHP_EOL . PHP_EOL;
}

$uses = $this->renderUses($entityAliases, $this->className);
if (method_exists($this, 'renderDynamicProperties')) {
$this->renderDynamicProperties($entityAliases, $this->docBlock);
}
Expand All @@ -61,6 +60,8 @@ public function render(EntityAliases $entityAliases): string
$header = $this->renderHeader($entityAliases);
$body = IndentsHelper::indent($this->renderBody($entityAliases));

$uses = $this->renderUses($entityAliases, $this->className);

return <<<PHP
<?php
Expand Down
11 changes: 8 additions & 3 deletions src/Renderers/AttributeRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

class AttributeRenderer implements EntityRendererInterface
{
/**
* @param list<ValueRenderer>|array<string, ValueRenderer> $arguments
*/
public function __construct(
private readonly string $className,
private readonly array $arguments = [],
Expand All @@ -27,20 +30,22 @@ public function getArguments(): array
public function render(EntityAliases $entityAliases): string
{
$alias = $entityAliases->addAlias($this->className);
$arguments = $this->renderArguments();
$arguments = $this->renderArguments($entityAliases);

return sprintf('#[%s%s]', $alias, $arguments);
}

protected function renderArguments(): string
protected function renderArguments(EntityAliases $entityAliases): string
{
if (empty($this->arguments)) {
return '';
}

$argumentList = [];
foreach ($this->arguments as $name => $value) {
$argumentList[] = is_string($name) ? $name . ': ' . $value : $value;
$argumentList[] = is_string($name) ? $name . ': ' . $value->render($entityAliases) : $value->render(
$entityAliases
);
}
$arguments = '(' . implode(', ', $argumentList) . ')';
if (strlen($arguments) > 80) {
Expand Down
4 changes: 2 additions & 2 deletions src/Renderers/ConstantRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ConstantRenderer implements EntityRendererInterface

public function __construct(
private readonly string $name,
private readonly string $value,
private readonly ValueRenderer $value,
) {
$this->docBlock = new DocBlockRenderer();
}
Expand All @@ -26,7 +26,7 @@ public function render(EntityAliases $entityAliases): string
$modifiers .= ' ';
}

$result = $modifiers . 'const ' . $this->name . ' = ' . $this->value . ';';
$result = $modifiers . 'const ' . $this->name . ' = ' . $this->value->render($entityAliases) . ';';
$docBlock = $this->docBlock->render($entityAliases);

return $docBlock . $result;
Expand Down
4 changes: 2 additions & 2 deletions src/Renderers/EnumCaseRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class EnumCaseRenderer implements EntityRendererInterface

public function __construct(
private readonly string $name,
private readonly string $value,
private readonly ValueRenderer $value,
) {
$this->docBlock = new DocBlockRenderer();
}
Expand All @@ -29,6 +29,6 @@ public function setDescription(string $description): self
public function render(EntityAliases $entityAliases): string
{
$docBlock = $this->docBlock->render($entityAliases);
return $docBlock . 'case ' . $this->name . ' = ' . $this->value . ';';
return $docBlock . 'case ' . $this->name . ' = ' . $this->value->render($entityAliases) . ';';
}
}
5 changes: 3 additions & 2 deletions src/Renderers/EnumRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ protected function renderHeader(EntityAliases $entityAliases): string
$result = [];

$result[] = 'enum';
$result[] = $this->getBaseClassName();

if ($this->type !== null) {
$result[] = ': ' . $this->type->value;
$result[] = $this->getBaseClassName() . ': ' . $this->type->value;
} else {
$result[] = $this->getBaseClassName();
}

if (!empty($this->implements)) {
Expand Down
20 changes: 12 additions & 8 deletions src/Renderers/MethodRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class MethodRenderer implements EntityRendererInterface

/** @var list<ParameterRenderer> */
private array $parameters = [];
private TypeRendererInterface $returnType = BuiltinTypeRenderer::Mixed;
private ?TypeRendererInterface $returnType = null;
private ?string $methodBody = null;

public function __construct(
Expand Down Expand Up @@ -95,7 +95,9 @@ protected function renderDocBlock(EntityAliases $entityAliases): string
$this->docBlock->addLine($line);
}

$this->docBlock->addLine('@return ' . $this->returnType->renderDocBlock($entityAliases));
if ($this->returnType !== null) {
$this->docBlock->addLine('@return ' . $this->returnType->renderDocBlock($entityAliases));
}

return $this->docBlock->render($entityAliases);
}
Expand All @@ -111,11 +113,11 @@ protected function renderMethod(EntityAliases $entityAliases, string $methodBody
$result[] = 'function';
$result[] = $this->name;

$signature = $this->renderSignatureInline($entityAliases, $methodBody, $withoutBody);
$signature = $this->renderSignatureInline($entityAliases);
$method = $withoutBody ? ';' : PHP_EOL . '{' . $methodBody . '}';

if (strlen($signature) > 80) {
$signature = $this->renderSignatureColumn($entityAliases, $methodBody, $withoutBody);
$signature = $this->renderSignatureColumn($entityAliases);
$method = $withoutBody ? ';' : ' {' . $methodBody . '}';
}

Expand All @@ -130,8 +132,9 @@ protected function renderSignatureInline(EntityAliases $entityAliases,): string
}

$signature = implode(', ', $parameters);
$returnType = $this->returnType->render($entityAliases);
return '(' . $signature . '): ' . $returnType;
$returnType = $this->returnType !== null ? ': ' . $this->returnType->render($entityAliases) : '';

return '(' . $signature . ')' . $returnType;
}

protected function renderSignatureColumn(EntityAliases $entityAliases): string
Expand All @@ -142,8 +145,9 @@ protected function renderSignatureColumn(EntityAliases $entityAliases): string
}

$signature = IndentsHelper::indent(implode(',' . PHP_EOL, $parameters));
$returnType = $this->returnType->render($entityAliases);
return '(' . PHP_EOL . $signature . PHP_EOL . '): ' . $returnType;
$returnType = $this->returnType !== null ? ': ' . $this->returnType->render($entityAliases) : '';

return '(' . PHP_EOL . $signature . PHP_EOL . ')' . $returnType;
}

protected function renderBody(): string
Expand Down
26 changes: 21 additions & 5 deletions src/Renderers/ParameterRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ParameterRenderer implements EntityRendererInterface, DocBlockRendererInte
use HasModifierVisibility;

private ?string $description = null;
private ?string $default = null;
private ?ValueRenderer $default = null;

public function __construct(
private readonly string $name,
Expand Down Expand Up @@ -45,12 +45,12 @@ public function getDescription(): ?string
return $this->description;
}

public function setDefault(string $default): void
public function setDefault(ValueRenderer $default): void
{
$this->default = $default;
}

public function getDefault(): ?string
public function getDefault(): ?ValueRenderer
{
return $this->default;
}
Expand All @@ -67,9 +67,25 @@ public function renderDocBlock(EntityAliases $entityAliases): string

public function render(EntityAliases $entityAliases, bool $inline = false): string
{
$result = $this->type->render($entityAliases) . ' $' . $this->name;
$attributes = $this->renderAttributes($entityAliases, $inline);

$modifiers = $this->renderModifiers();
if (!empty($modifiers)) {
$result[] = $modifiers;
}

$result[] = $this->type->render($entityAliases);
$result[] = '$' . $this->name;

if ($this->default !== null) {
$result .= ' = ' . $this->default;
$result[] = '=';
$result[] = $this->default->render($entityAliases);
}

$result = implode(' ', $result);

if (!empty($attributes)) {
return $inline ? $attributes . ' ' . $result : $attributes . PHP_EOL . $result;
}

return $result;
Expand Down
11 changes: 8 additions & 3 deletions src/Renderers/PropertyRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class PropertyRenderer implements EntityRendererInterface, DocBlockRendererInter
use HasModifierVisibility;

private ?string $description = null;
private ?string $default = null;
private ?ValueRenderer $default = null;

public function __construct(
private readonly string $name,
Expand Down Expand Up @@ -51,12 +51,12 @@ public function getDescription(): ?string
return $this->description;
}

public function setDefault(string $default): void
public function setDefault(ValueRenderer $default): void
{
$this->default = $default;
}

public function getDefault(): ?string
public function getDefault(): ?ValueRenderer
{
return $this->default;
}
Expand Down Expand Up @@ -87,6 +87,11 @@ public function render(EntityAliases $entityAliases): string
$result[] = $this->type->render($entityAliases);
$result[] = '$' . $this->name;

if ($this->default !== null) {
$result [] = '=';
$result [] = $this->default->render($entityAliases);
}

return $docBlock . implode(' ', $result) . ';';
}
}
14 changes: 14 additions & 0 deletions src/Renderers/Types/ArrayTypeRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,18 @@ public function render(EntityAliases $entityAliases): string
{
return 'array';
}

public function setNullable(bool $nullable = true): TypeRendererInterface
{
if ($nullable) {
return new NullableTypeRenderer($this);
}

return $this;
}

public function isNullable(): bool
{
return false;
}
}
17 changes: 17 additions & 0 deletions src/Renderers/Types/BuiltinTypeRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,21 @@ public function render(EntityAliases $entityAliases): string
{
return $this->value;
}

public function setNullable(bool $nullable = true): TypeRendererInterface
{
if (!$nullable) {
return $this;
}

return match ($this) {
self::Never, self::Void, self::Null, self::Mixed => $this,
default => new NullableTypeRenderer($this),
};
}

public function isNullable(): bool
{
return $this === self::Null || $this === self::Mixed;
}
}
14 changes: 14 additions & 0 deletions src/Renderers/Types/ClassTypeRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,18 @@ public function render(EntityAliases $entityAliases): string
{
return $entityAliases->addAlias($this->className);
}

public function setNullable(bool $nullable = true): TypeRendererInterface
{
if ($nullable) {
return new NullableTypeRenderer($this);
}

return $this;
}

public function isNullable(): bool
{
return false;
}
}
14 changes: 12 additions & 2 deletions src/Renderers/Types/IntersectTypeRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

readonly class IntersectTypeRenderer implements TypeRendererInterface
{
/** @var list<TypeRendererInterface> */
/** @var list<ClassTypeRenderer> */
public array $types;

public function __construct(TypeRendererInterface ...$types)
public function __construct(ClassTypeRenderer | (UnionTypeRenderer & TypeRendererInterface) ...$types)
{
$this->types = $types;
}
Expand All @@ -30,4 +30,14 @@ public function render(EntityAliases $entityAliases): string
)
);
}

public function setNullable(bool $nullable = true): TypeRendererInterface
{
return $this;
}

public function isNullable(): bool
{
return false;
}
}
Loading

0 comments on commit b0c8f2f

Please sign in to comment.