Skip to content
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
30 changes: 30 additions & 0 deletions src-dev/CodeGen/Attributes/Mixin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Alexandre Gomes Gaigalas <[email protected]>
*/

declare(strict_types=1);

namespace Respect\Dev\CodeGen\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
final readonly class Mixin
{
/**
* @param array<string> $exclude
* @param array<string> $include
*/
public function __construct(
public string|null $prefix = null,
public bool $prefixParameter = false,
public bool $requireInclusion = false,
public array $exclude = [],
public array $include = [],
) {
}
}
28 changes: 28 additions & 0 deletions src-dev/CodeGen/InterfaceConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Alexandre Gomes Gaigalas <[email protected]>
*/

declare(strict_types=1);

namespace Respect\Dev\CodeGen;

final readonly class InterfaceConfig
{
/**
* @param array<string> $rootExtends
* @param array<string> $rootUses
*/
public function __construct(
public string $suffix,
public string $returnType,
public bool $static = false,
public array $rootExtends = [],
public string|null $rootComment = null,
public array $rootUses = [],
) {
}
}
169 changes: 169 additions & 0 deletions src-dev/CodeGen/MethodBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php

/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Alexandre Gomes Gaigalas <[email protected]>
*/

declare(strict_types=1);

namespace Respect\Dev\CodeGen;

use Nette\PhpGenerator\Method;
use Nette\PhpGenerator\PhpNamespace;
use ReflectionClass;
use ReflectionNamedType;
use ReflectionParameter;
use ReflectionUnionType;

use function count;
use function implode;
use function in_array;
use function is_object;
use function lcfirst;
use function preg_replace;
use function sort;
use function str_starts_with;
use function ucfirst;

final class MethodBuilder
{
/**
* @param array<string> $excludedTypePrefixes
* @param array<string> $excludedTypeNames
*/
public function __construct(
private readonly array $excludedTypePrefixes = [],
private readonly array $excludedTypeNames = [],
) {
}

public function build(
PhpNamespace $namespace,
ReflectionClass $validatorReflection,
string $returnType,
string|null $prefix = null,
bool $static = false,
ReflectionParameter|null $prefixParameter = null,
): Method {
$originalName = $validatorReflection->getShortName();
$name = $prefix ? $prefix . ucfirst($originalName) : lcfirst($originalName);

$method = new Method($name);
$method->setPublic()->setReturnType($returnType);

if ($static) {
$method->setStatic();
}

if ($prefixParameter !== null) {
$this->addPrefixParameter($method, $prefixParameter);
}

$constructor = $validatorReflection->getConstructor();
if ($constructor === null) {
return $method;
}

$comment = $constructor->getDocComment();
if ($comment) {
$method->addComment(preg_replace('@(/\*\* *| +\* +| +\*/)@', '', $comment));
}

foreach ($constructor->getParameters() as $reflectionParameter) {
$this->addParameter($method, $reflectionParameter, $namespace);
}

return $method;
}

private function addPrefixParameter(Method $method, ReflectionParameter $reflectionParameter): void
{
$type = $reflectionParameter->getType();
$types = [];

if ($type instanceof ReflectionUnionType) {
foreach ($type->getTypes() as $subType) {
$types[] = $subType->getName();
}

sort($types);
} elseif ($type instanceof ReflectionNamedType) {
$types[] = $type->getName();
}

$method->addParameter($reflectionParameter->getName())->setType(implode('|', $types));
}

private function addParameter(
Method $method,
ReflectionParameter $reflectionParameter,
PhpNamespace $namespace,
): void {
if ($reflectionParameter->isVariadic()) {
$method->setVariadic();
}

$type = $reflectionParameter->getType();
$types = [];

if ($type instanceof ReflectionUnionType) {
foreach ($type->getTypes() as $subType) {
$types[] = $subType->getName();
if ($subType->isBuiltin()) {
continue;
}

$namespace->addUse($subType->getName());
}
} elseif ($type instanceof ReflectionNamedType) {
$types[] = $type->getName();

if ($this->isExcludedType($type->getName())) {
return;
}

if (!$type->isBuiltin()) {
$namespace->addUse($type->getName());
}
}

$parameter = $method->addParameter($reflectionParameter->getName());
$parameter->setType(implode('|', $types));

if (!$reflectionParameter->isDefaultValueAvailable()) {
$parameter->setNullable($reflectionParameter->isOptional());
}

if (count($types) > 1 || $reflectionParameter->isVariadic()) {
$parameter->setNullable(false);
}

if (!$reflectionParameter->isDefaultValueAvailable()) {
return;
}

$defaultValue = $reflectionParameter->getDefaultValue();
if (is_object($defaultValue)) {
$parameter->setDefaultValue(null);
$parameter->setNullable(true);

return;
}

$parameter->setDefaultValue($defaultValue);
$parameter->setNullable(false);
}

private function isExcludedType(string $typeName): bool
{
foreach ($this->excludedTypePrefixes as $excludedPrefix) {
if (str_starts_with($typeName, $excludedPrefix)) {
return true;
}
}

return in_array($typeName, $this->excludedTypeNames, true);
}
}
Loading