Skip to content

Commit

Permalink
Ast: PropertyNode, Parameter - parse union types
Browse files Browse the repository at this point in the history
  • Loading branch information
janpecha committed Mar 19, 2024
1 parent 4dfc773 commit 29979be
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 83 deletions.
6 changes: 3 additions & 3 deletions src/Ast/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Parameter implements IFunctionBody
/** @var IPropertyModifier[] */
private $promotedPropertyModifiers;

/** @var NamedType|NULL */
/** @var Type|NULL */
private $type;

/** @var VariableName */
Expand All @@ -33,7 +33,7 @@ class Parameter implements IFunctionBody
public function __construct(
$indentation,
array $promotedPropertyModifiers,
NamedType $type = NULL,
Type $type = NULL,
VariableName $name,
DefaultValue $defaultValue = NULL
)
Expand Down Expand Up @@ -102,7 +102,7 @@ public static function parse(NodeParser $parser)
}

if (!$parser->isCurrent(T_VARIABLE, T_ELLIPSIS, '&', PhpToken::T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG())) {
$type = NamedType::parse($parser->createSubParser());
$type = Type::parse($parser->createSubParser());
$parser->tryConsumeWhitespaceAndComments();
}

Expand Down
8 changes: 4 additions & 4 deletions src/Ast/PropertyNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class PropertyNode implements INode
/** @var IPropertyModifier[] */
private $modifiers;

/** @var NamedType|NULL */
/** @var Type|NULL */
private $type;

/** @var string */
Expand All @@ -41,7 +41,7 @@ class PropertyNode implements INode
public function __construct(
$indentation,
array $modifiers,
NamedType $type = NULL,
Type $type = NULL,
$namePrefix,
$name,
DefaultValue $defaultValue = NULL,
Expand Down Expand Up @@ -95,8 +95,8 @@ public static function parse(Modifiers $modifiers, NodeParser $parser)
$type = NULL;

if (!$parser->isCurrent(T_VARIABLE)) {
$type = NamedType::parse($parser->createSubParser());
$parser->consumeWhitespace();
$type = Type::parse($parser->createSubParser());
$parser->tryConsumeWhitespace();
}

$nodeIndentation = $parser->consumeNodeIndentation() . $parser->flushIndentation();
Expand Down
22 changes: 14 additions & 8 deletions tests/PhpSimpleAst/fixtures/PHP-8.0/80000.promoted-properties.dump
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@ CzProject\PhpSimpleAst\Ast\PhpString
| | | | | | | | | | 0 => CzProject\PhpSimpleAst\Ast\Visibility
| | | | | | | | | | | indentation: ''
| | | | | | | | | | | visibility: 'protected'
| | | | | | | | | type: CzProject\PhpSimpleAst\Ast\NamedType
| | | | | | | | | type: CzProject\PhpSimpleAst\Ast\Type
| | | | | | | | | | indentation: ' '
| | | | | | | | | | nullableSign: ''
| | | | | | | | | | typeIndentation: ''
| | | | | | | | | | type: 'int'
| | | | | | | | | | types: array (1)
| | | | | | | | | | | 0 => CzProject\PhpSimpleAst\Ast\NamedType
| | | | | | | | | | | | indentation: ''
| | | | | | | | | | | | nullableSign: ''
| | | | | | | | | | | | typeIndentation: ''
| | | | | | | | | | | | type: 'int'
| | | | | | | | | name: CzProject\PhpSimpleAst\Ast\VariableName
| | | | | | | | | | indentation: ' '
| | | | | | | | | | referenceSign: ''
Expand All @@ -57,11 +60,14 @@ CzProject\PhpSimpleAst\Ast\PhpString
| | | | | | | | | | 0 => CzProject\PhpSimpleAst\Ast\Visibility
| | | | | | | | | | | indentation: ''
| | | | | | | | | | | visibility: 'protected'
| | | | | | | | | type: CzProject\PhpSimpleAst\Ast\NamedType
| | | | | | | | | type: CzProject\PhpSimpleAst\Ast\Type
| | | | | | | | | | indentation: ' '
| | | | | | | | | | nullableSign: ''
| | | | | | | | | | typeIndentation: ''
| | | | | | | | | | type: 'int'
| | | | | | | | | | types: array (1)
| | | | | | | | | | | 0 => CzProject\PhpSimpleAst\Ast\NamedType
| | | | | | | | | | | | indentation: ''
| | | | | | | | | | | | nullableSign: ''
| | | | | | | | | | | | typeIndentation: ''
| | | | | | | | | | | | type: 'int'
| | | | | | | | | name: CzProject\PhpSimpleAst\Ast\VariableName
| | | | | | | | | | indentation: ' '
| | | | | | | | | | referenceSign: ''
Expand Down
118 changes: 118 additions & 0 deletions tests/PhpSimpleAst/fixtures/PHP-8.0/80000.union-types.dump
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
CzProject\PhpSimpleAst\Ast\PhpString
children: array (1)
| 0 => CzProject\PhpSimpleAst\Ast\PhpNode
| | openTag: '<?php\n'
| | children: array (2)
| | | 0 => CzProject\PhpSimpleAst\Ast\ClassNode
| | | | indentation: '\n'
| | | | keyword: 'class'
| | | | name: CzProject\PhpSimpleAst\Ast\Name
| | | | | indentation: ' '
| | | | | name: 'Point'
| | | | constructorValues: null
| | | | extends: null
| | | | implements: null
| | | | blockOpener: ' {'
| | | | children: array (2)
| | | | | 0 => CzProject\PhpSimpleAst\Ast\PropertyNode
| | | | | | indentation: string
| | | | | | | '\n
| | | | | | | \t '
| | | | | | modifiers: array (1)
| | | | | | | 0 => CzProject\PhpSimpleAst\Ast\Visibility
| | | | | | | | indentation: ''
| | | | | | | | visibility: 'public'
| | | | | | type: CzProject\PhpSimpleAst\Ast\Type
| | | | | | | indentation: ' '
| | | | | | | types: array (2)
| | | | | | | | 0 => CzProject\PhpSimpleAst\Ast\NamedType
| | | | | | | | | indentation: ''
| | | | | | | | | nullableSign: ''
| | | | | | | | | typeIndentation: ''
| | | | | | | | | type: 'string'
| | | | | | | | 1 => CzProject\PhpSimpleAst\Ast\NamedType
| | | | | | | | | indentation: '|'
| | | | | | | | | nullableSign: ''
| | | | | | | | | typeIndentation: ''
| | | | | | | | | type: 'bool'
| | | | | | namePrefix: ' '
| | | | | | name: '$prop'
| | | | | | defaultValue: null
| | | | | | closer: ';'
| | | | | 1 => CzProject\PhpSimpleAst\Ast\MethodNode
| | | | | | phpDocNode: null
| | | | | | indentation: string
| | | | | | | '\n
| | | | | | | \n
| | | | | | | \n
| | | | | | | \t '
| | | | | | modifiers: array (0)
| | | | | | keywordPrefix: ''
| | | | | | keyword: 'function'
| | | | | | name: CzProject\PhpSimpleAst\Ast\Name
| | | | | | | indentation: ' '
| | | | | | | name: 'func'
| | | | | | parameters: CzProject\PhpSimpleAst\Ast\Parameters
| | | | | | | indentation: ''
| | | | | | | opener: '('
| | | | | | | parameters: array (1)
| | | | | | | | 0 => CzProject\PhpSimpleAst\Ast\Parameter
| | | | | | | | | indentation: ''
| | | | | | | | | promotedPropertyModifiers: array (0)
| | | | | | | | | type: CzProject\PhpSimpleAst\Ast\Type
| | | | | | | | | | indentation: ''
| | | | | | | | | | types: array (2)
| | | | | | | | | | | 0 => CzProject\PhpSimpleAst\Ast\NamedType
| | | | | | | | | | | | indentation: ''
| | | | | | | | | | | | nullableSign: ''
| | | | | | | | | | | | typeIndentation: ''
| | | | | | | | | | | | type: 'string'
| | | | | | | | | | | 1 => CzProject\PhpSimpleAst\Ast\NamedType
| | | | | | | | | | | | indentation: '|'
| | | | | | | | | | | | nullableSign: ''
| | | | | | | | | | | | typeIndentation: ''
| | | | | | | | | | | | type: 'bool'
| | | | | | | | | name: CzProject\PhpSimpleAst\Ast\VariableName
| | | | | | | | | | indentation: ' '
| | | | | | | | | | referenceSign: ''
| | | | | | | | | | variadic: null
| | | | | | | | | | name: CzProject\PhpSimpleAst\Ast\Literal
| | | | | | | | | | | indentation: ''
| | | | | | | | | | | literal: '$x'
| | | | | | | | | defaultValue: null
| | | | | | | closer: ')'
| | | | | | returnType: CzProject\PhpSimpleAst\Ast\FunctionReturnType
| | | | | | | indentation: ' '
| | | | | | | nullable: false
| | | | | | | type: CzProject\PhpSimpleAst\Ast\Type
| | | | | | | | indentation: ''
| | | | | | | | types: array (2)
| | | | | | | | | 0 => CzProject\PhpSimpleAst\Ast\NamedType
| | | | | | | | | | indentation: ''
| | | | | | | | | | nullableSign: ''
| | | | | | | | | | typeIndentation: ''
| | | | | | | | | | type: 'string'
| | | | | | | | | 1 => CzProject\PhpSimpleAst\Ast\NamedType
| | | | | | | | | | indentation: '|'
| | | | | | | | | | nullableSign: ''
| | | | | | | | | | typeIndentation: ''
| | | | | | | | | | type: 'bool'
| | | | | | body: CzProject\PhpSimpleAst\Ast\FunctionBody
| | | | | | | indentation: string
| | | | | | | | '\n
| | | | | | | | \t '
| | | | | | | blockOpener: '{'
| | | | | | | children: array (1)
| | | | | | | | 0 => CzProject\PhpSimpleAst\Ast\UnknowNode
| | | | | | | | | content: string
| | | | | | | | | | '\n
| | | | | | | | | | \t \t return FALSE;'
| | | | | | | blockCloser: string
| | | | | | | | '\n
| | | | | | | | \t }'
| | | | blockCloser: string
| | | | | '\n
| | | | | }'
| | | 1 => CzProject\PhpSimpleAst\Ast\UnknowNode
| | | | content: '\n'
| | closeTag: null
11 changes: 11 additions & 0 deletions tests/PhpSimpleAst/fixtures/PHP-8.0/80000.union-types.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

class Point {
public string|bool $prop;


function func(string|bool $x): string|bool
{
return FALSE;
}
}
22 changes: 14 additions & 8 deletions tests/PhpSimpleAst/fixtures/PHP-8.1/80100.readonly-properties.dump
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ CzProject\PhpSimpleAst\Ast\PhpString
| | | | | | | 1 => CzProject\PhpSimpleAst\Ast\ReadOnlyModifier
| | | | | | | | indentation: ' '
| | | | | | | | modifier: 'readonly'
| | | | | | type: CzProject\PhpSimpleAst\Ast\NamedType
| | | | | | type: CzProject\PhpSimpleAst\Ast\Type
| | | | | | | indentation: ' '
| | | | | | | nullableSign: ''
| | | | | | | typeIndentation: ''
| | | | | | | type: 'Status'
| | | | | | | types: array (1)
| | | | | | | | 0 => CzProject\PhpSimpleAst\Ast\NamedType
| | | | | | | | | indentation: ''
| | | | | | | | | nullableSign: ''
| | | | | | | | | typeIndentation: ''
| | | | | | | | | type: 'Status'
| | | | | | namePrefix: ' '
| | | | | | name: '$status'
| | | | | | defaultValue: null
Expand All @@ -58,11 +61,14 @@ CzProject\PhpSimpleAst\Ast\PhpString
| | | | | | | | 0 => CzProject\PhpSimpleAst\Ast\Parameter
| | | | | | | | | indentation: ''
| | | | | | | | | promotedPropertyModifiers: array (0)
| | | | | | | | | type: CzProject\PhpSimpleAst\Ast\NamedType
| | | | | | | | | type: CzProject\PhpSimpleAst\Ast\Type
| | | | | | | | | | indentation: ''
| | | | | | | | | | nullableSign: ''
| | | | | | | | | | typeIndentation: ''
| | | | | | | | | | type: 'Status'
| | | | | | | | | | types: array (1)
| | | | | | | | | | | 0 => CzProject\PhpSimpleAst\Ast\NamedType
| | | | | | | | | | | | indentation: ''
| | | | | | | | | | | | nullableSign: ''
| | | | | | | | | | | | typeIndentation: ''
| | | | | | | | | | | | type: 'Status'
| | | | | | | | | name: CzProject\PhpSimpleAst\Ast\VariableName
| | | | | | | | | | indentation: ' '
| | | | | | | | | | referenceSign: ''
Expand Down
Loading

0 comments on commit 29979be

Please sign in to comment.