Skip to content

Commit 18b81cd

Browse files
author
Kirill Nesmeyanov
committed
Add parser ^2.0 compatibility
1 parent 862b111 commit 18b81cd

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@
1010
},
1111
"require": {
1212
"php": "^8.1",
13-
"type-lang/parser": "^1.0.1"
13+
"type-lang/parser": "^2.0"
1414
},
1515
"autoload": {
1616
"psr-4": {
1717
"TypeLang\\Printer\\": "src"
1818
}
1919
},
2020
"require-dev": {
21-
"friendsofphp/php-cs-fixer": "^3.35",
22-
"phpunit/phpunit": "^10.4",
21+
"friendsofphp/php-cs-fixer": "^3.42",
22+
"phpunit/phpunit": "^10.5",
2323
"rector/rector": "^0.18",
24-
"vimeo/psalm": "^5.15"
24+
"vimeo/psalm": "^5.18"
2525
},
2626
"autoload-dev": {
2727
"psr-4": {

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
44
colors="true"
55
backupGlobals="true"
66
stopOnFailure="false"

src/NativeTypePrinter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
/**
2222
* @psalm-suppress UndefinedAttributeClass : For "Override" attribute support.
23+
* @psalm-suppress InvalidAttribute : For "Override" attribute support.
2324
*/
2425
class NativeTypePrinter extends PrettyPrinter
2526
{

src/PrettyPrinter.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,12 @@ class: LogicalTypeNode::class,
235235
// Break on non-empty template parameters.
236236
$isInTemplate = $node instanceof NamedTypeNode
237237
&& $node->arguments !== null
238-
&& $node->arguments->list !== [];
238+
&& $node->arguments->items !== [];
239239

240240
// Break on non-empty shape fields.
241241
$isInShape = $node instanceof NamedTypeNode
242242
&& $node->fields !== null
243-
&& $node->fields->list !== [];
243+
&& $node->fields->items !== [];
244244

245245
return $isInTemplate || $isInShape;
246246
},
@@ -347,10 +347,8 @@ protected function printNamedTypeNode(NamedTypeNode $node): string
347347

348348
if ($node->fields !== null) {
349349
$result .= $this->printShapeFieldsNode($node, $node->fields);
350-
} else {
351-
if ($node->arguments !== null) {
352-
$result .= $this->printTemplateArgumentsNode($node->arguments);
353-
}
350+
} elseif ($node->arguments !== null) {
351+
$result .= $this->printTemplateArgumentsNode($node->arguments);
354352
}
355353

356354
/** @var non-empty-string */
@@ -364,7 +362,7 @@ protected function printTemplateArgumentsNode(TemplateArgumentsListNode $params)
364362
{
365363
$result = [];
366364

367-
foreach ($params->list as $param) {
365+
foreach ($params->items as $param) {
368366
$result[] = $this->printTemplateArgumentNode($param);
369367
}
370368

@@ -385,7 +383,7 @@ protected function printTemplateArgumentNode(TemplateArgumentNode $param): strin
385383
*/
386384
protected function printShapeFieldsNode(NamedTypeNode $node, FieldsListNode $shape): string
387385
{
388-
if (\count($shape->list) <= $this->multilineShape) {
386+
if (\count($shape->items) <= $this->multilineShape) {
389387
/** @var non-empty-string */
390388
return \vsprintf('{%s}', [
391389
\implode(', ', $this->getShapeFieldsNodes($node, $shape)),
@@ -411,7 +409,7 @@ private function getShapeFieldsNodes(NamedTypeNode $node, FieldsListNode $shape)
411409

412410
$fields = [];
413411

414-
foreach ($shape->list as $field) {
412+
foreach ($shape->items as $field) {
415413
$fields[] = $prefix . $this->printShapeFieldNode($field);
416414
}
417415

@@ -444,19 +442,20 @@ protected function printShapeFieldNode(FieldNode $field): string
444442
/** @var non-empty-string */
445443
return \vsprintf('%s: %s', [
446444
$name,
447-
$this->make($field->getValue()),
445+
$this->make($field->getType()),
448446
]);
449447
}
450448

451-
return $this->make($field->getValue());
449+
/** @var non-empty-string */
450+
return $this->make($field->getType());
452451
}
453452

454453
protected function printShapeFieldName(FieldNode $field): string
455454
{
456455
return match (true) {
457-
$field instanceof StringNamedFieldNode => $field->name->getRawValue(),
458-
$field instanceof NumericFieldNode => $field->index->getRawValue(),
459-
$field instanceof NamedFieldNode => $field->name->toString(),
456+
$field instanceof StringNamedFieldNode,
457+
$field instanceof NumericFieldNode => $field->key->getRawValue(),
458+
$field instanceof NamedFieldNode => $field->key->toString(),
460459
default => '',
461460
};
462461
}

0 commit comments

Comments
 (0)