forked from Respect/Validation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodBuilder.php
More file actions
169 lines (136 loc) · 4.71 KB
/
MethodBuilder.php
File metadata and controls
169 lines (136 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
*/
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);
}
}