-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheGenerator.php
More file actions
208 lines (167 loc) · 6.39 KB
/
CacheGenerator.php
File metadata and controls
208 lines (167 loc) · 6.39 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/*
* SPDX-License-Identifier: ISC
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
*/
declare(strict_types=1);
namespace Respect\FluentAnalysis;
use ReflectionClass;
use Respect\Fluent\Attributes\FluentNamespace;
use Respect\Fluent\Builders\FluentBuilder;
use function implode;
use function is_array;
use function is_int;
use function is_subclass_of;
use function ksort;
use function sprintf;
use function str_repeat;
use const PHP_EOL;
final readonly class CacheGenerator
{
public function __construct(
private MethodMapBuilder $mapBuilder,
) {
}
/** @param list<class-string> $builderClasses */
public function generate(array $builderClasses): string
{
$methodSections = [];
$assuranceSections = [];
$assertionSections = [];
$serviceSections = [];
foreach ($builderClasses as $builderClass) {
$attribute = $this->findAttribute($builderClass);
if ($attribute === null) {
continue;
}
$map = $this->mapBuilder->build($attribute);
ksort($map);
if ($map !== []) {
$methodSections[] = $this->formatMethodSection($builderClass, $map);
}
$assurances = $this->mapBuilder->buildAssurances($attribute);
ksort($assurances);
if ($assurances !== []) {
$assuranceSections[] = $this->formatAssuranceSection($builderClass, $assurances);
}
$assertions = $this->mapBuilder->buildAssertions($builderClass);
if ($assertions !== []) {
$assertionSections[] = sprintf(
'%s%s: [%s]',
str_repeat("\t", 3),
$builderClass,
implode(', ', $assertions),
);
}
if (is_subclass_of($builderClass, FluentBuilder::class)) {
continue;
}
$serviceSections[] = $this->formatServiceSection($builderClass);
}
$output = 'parameters:' . PHP_EOL . "\t" . 'fluent:' . PHP_EOL;
$output .= $this->formatParameterBlock('methods', $methodSections);
$output .= $this->formatParameterBlock('assurances', $assuranceSections);
$output .= $this->formatParameterBlock('assertions', $assertionSections);
if ($serviceSections !== []) {
$output .= PHP_EOL . 'services:' . PHP_EOL
. implode(PHP_EOL, $serviceSections) . PHP_EOL;
}
return $output;
}
/** @param class-string $builderClass */
private function findAttribute(string $builderClass): FluentNamespace|null
{
$reflection = new ReflectionClass($builderClass);
while (true) {
$attrs = $reflection->getAttributes(FluentNamespace::class);
if ($attrs !== []) {
return $attrs[0]->newInstance();
}
$parent = $reflection->getParentClass();
if ($parent === false) {
return null;
}
$reflection = $parent;
}
}
/** @param array<string, string> $map */
private function formatMethodSection(string $builderClass, array $map): string
{
$lines = [];
foreach ($map as $method => $fqcn) {
$lines[] = sprintf('%s%s: %s', str_repeat("\t", 4), $method, $fqcn);
}
return sprintf(
'%s%s:' . PHP_EOL . '%s',
str_repeat("\t", 3),
$builderClass,
implode(PHP_EOL, $lines),
);
}
/** @param array<string, array<string, array{int, int|null}|int|string>> $assurances */
private function formatAssuranceSection(string $builderClass, array $assurances): string
{
$lines = [];
foreach ($assurances as $method => $entry) {
$parts = [];
foreach ($entry as $key => $value) {
$parts[] = $key . ': ' . $this->formatNeonValue($value);
}
$lines[] = sprintf('%s%s: { %s }', str_repeat("\t", 4), $method, implode(', ', $parts));
}
return sprintf(
'%s%s:' . PHP_EOL . '%s',
str_repeat("\t", 3),
$builderClass,
implode(PHP_EOL, $lines),
);
}
/** @param list<string> $sections */
private function formatParameterBlock(string $name, array $sections): string
{
if ($sections === []) {
return "\t\t" . $name . ': []' . PHP_EOL;
}
return "\t\t" . $name . ':' . PHP_EOL
. implode(PHP_EOL, $sections) . PHP_EOL;
}
private function formatServiceSection(string $builderClass): string
{
$t = "\t";
return $t . '-' . PHP_EOL
. $t . $t . 'class: Respect\FluentAnalysis\FluentDynamicReturnTypeExtension' . PHP_EOL
. $t . $t . 'arguments:' . PHP_EOL
. $t . $t . $t . 'methodMap: @fluentMethodMap' . PHP_EOL
. $t . $t . $t . 'assuranceMap: @fluentAssuranceMap' . PHP_EOL
. $t . $t . $t . 'targetClass: ' . $builderClass . PHP_EOL
. $t . $t . 'tags:' . PHP_EOL
. $t . $t . $t . '- phpstan.broker.dynamicMethodReturnTypeExtension' . PHP_EOL
. $t . $t . $t . '- phpstan.broker.dynamicStaticMethodReturnTypeExtension' . PHP_EOL
. $t . '-' . PHP_EOL
. $t . $t . 'class: Respect\FluentAnalysis\FluentTypeSpecifyingExtension' . PHP_EOL
. $t . $t . 'arguments:' . PHP_EOL
. $t . $t . $t . 'assuranceMap: @fluentAssuranceMap' . PHP_EOL
. $t . $t . $t . 'targetClass: ' . $builderClass . PHP_EOL
. $t . $t . 'tags:' . PHP_EOL
. $t . $t . $t . '- phpstan.typeSpecifier.methodTypeSpecifyingExtension';
}
/** @param array{int, int|null}|int|string $value */
private function formatNeonValue(array|int|string $value): string
{
if (is_array($value)) {
$items = [];
foreach ($value as $item) {
$items[] = $item === null ? 'null' : (string) $item;
}
return '[' . implode(', ', $items) . ']';
}
if (is_int($value)) {
return (string) $value;
}
return match ($value) {
'true', 'false', 'null', 'yes', 'no', 'on', 'off' => "'" . $value . "'",
default => $value,
};
}
}