Skip to content

Commit 5346bbe

Browse files
committed
Improve pointers generation
1 parent 4c3f7a9 commit 5346bbe

File tree

1 file changed

+81
-12
lines changed

1 file changed

+81
-12
lines changed

src/PhpStormMetadataGenerator/GenerateStructOverrides.php

Lines changed: 81 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,29 @@ public function before(NamespaceNode $ctx, iterable $nodes): iterable
138138
return [];
139139
}
140140

141+
/**
142+
* Additionally, an autocomplete with a pointer will be generated if the
143+
* specified type is a structure-like or a typedef for a structure-like.
144+
*
145+
* @param non-empty-string $name
146+
*
147+
* @psalm-suppress RedundantConditionGivenDocblockType
148+
*/
149+
private function isStructureLike(NamespaceNode $ctx, string $name): bool
150+
{
151+
if (!isset($ctx->types[$name])) {
152+
return false;
153+
}
154+
155+
$type = $ctx->types[$name];
156+
157+
if ($type instanceof RecordTypeNode) {
158+
return true;
159+
}
160+
161+
return $type instanceof TypeDefinitionNode && $type->type instanceof RecordTypeNode;
162+
}
163+
141164
public function after(NamespaceNode $ctx, iterable $nodes): iterable
142165
{
143166
//
@@ -155,9 +178,12 @@ public function after(NamespaceNode $ctx, iterable $nodes): iterable
155178
);
156179

157180
foreach ($this->names as $name) {
158-
$registerArgumentsSet->args[] = new Arg(new String_(
159-
value: $name,
160-
));
181+
$registerArgumentsSet->args[] = new Arg(new String_($name));
182+
183+
if ($this->isStructureLike($ctx, $name)) {
184+
$registerArgumentsSet->args[] = new Arg(new String_($name . '*'));
185+
$registerArgumentsSet->args[] = new Arg(new String_($name . '**'));
186+
}
161187
}
162188

163189
yield new Expression($registerArgumentsSet);
@@ -183,6 +209,23 @@ class: new Name\FullyQualified($this->naming->getEntrypoint()),
183209
]
184210
));
185211

212+
yield new Expression(new FuncCall(
213+
name: new Name('expectedArguments'),
214+
args: [
215+
new Arg(new StaticCall(
216+
class: new Name\FullyQualified($this->naming->getEntrypoint()),
217+
name: 'cast',
218+
)),
219+
new Arg(new LNumber(0)),
220+
new Arg(new FuncCall(
221+
name: new Name('argumentsSet'),
222+
args: [new Arg(new String_(
223+
value: $this->argumentSetPrefix . $this->globalArgumentSetSuffix
224+
))]
225+
))
226+
]
227+
));
228+
186229
yield new Expression(new FuncCall(
187230
name: new Name('expectedArguments'),
188231
args: [
@@ -206,7 +249,7 @@ class: new Name\FullyQualified($this->naming->getEntrypoint()),
206249

207250
$internalName = \rtrim($this->naming->getName('@', new StructTypeNode('@')), '@') . '@';
208251

209-
$arrayItems = [new ArrayItem(
252+
$map = [new ArrayItem(
210253
value: new String_('\\' . $internalName),
211254
key: new String_(''),
212255
attributes: ['comments' => [new Comment('// structures autocompletion')]]
@@ -218,13 +261,38 @@ class: new Name\FullyQualified($this->naming->getEntrypoint()),
218261
continue;
219262
}
220263

221-
$arrayItems[] = new ArrayItem(
222-
value: new String_('\\' . $this->naming->getName(
223-
name: $node->name,
224-
type: $node->type,
225-
)),
264+
$mappedType = $this->naming->getName(
265+
name: $node->name,
266+
type: $node->type,
267+
);
268+
269+
$map[] = new ArrayItem(
270+
value: new String_("\\$mappedType"),
226271
key: new String_($node->name),
227272
);
273+
274+
if ($this->isStructureLike($ctx, $node->name)) {
275+
$map[] = new ArrayItem(
276+
value: new String_("\\{$mappedType}[]"),
277+
key: new String_( $node->name . '*'),
278+
);
279+
$map[] = new ArrayItem(
280+
value: new String_("\\{$mappedType}"),
281+
key: new String_( $node->name . '*'),
282+
);
283+
$map[] = new ArrayItem(
284+
value: new String_("\\{$mappedType}[][]"),
285+
key: new String_( $node->name . '**'),
286+
);
287+
$map[] = new ArrayItem(
288+
value: new String_("\\{$mappedType}[]"),
289+
key: new String_( $node->name . '**'),
290+
);
291+
$map[] = new ArrayItem(
292+
value: new String_("\\{$mappedType}"),
293+
key: new String_( $node->name . '**'),
294+
);
295+
}
228296
}
229297
}
230298

@@ -237,9 +305,10 @@ class: new Name\FullyQualified($this->naming->getEntrypoint()),
237305
)),
238306
new Arg(new FuncCall(
239307
name: new Name('map'),
240-
args: [
241-
new Arg(new Array_($arrayItems, ['kind' => Array_::KIND_SHORT]))
242-
]
308+
args: [new Arg(new Array_(
309+
items: $map,
310+
attributes: ['kind' => Array_::KIND_SHORT],
311+
))]
243312
))
244313
]
245314
));

0 commit comments

Comments
 (0)