Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/PHPCfg/Op/Type/Union.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

/**
* This file is part of PHP-CFG, a Control flow graph implementation for PHP
*
* @copyright 2015 Anthony Ferrara. All rights reserved
* @license MIT See LICENSE at the root of the project for more info
*/

namespace PHPCfg\Op\Type;

use PHPCfg\Block;
use PHPCfg\Op\Type;

class Union extends Type
{
public array $subtypes;

public function __construct(array $subtypes, array $attributes = []) {
$this->subtypes = $subtypes;
}

public function getVariableNames(): array
{
return ['subtypes'];
}
}
11 changes: 11 additions & 0 deletions lib/PHPCfg/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,17 @@ protected function parseTypeNode(?Node $node): Op\Type {
$this->mapAttributes($node)
);
}
if ($node instanceof Node\UnionType) {
$parsedTypes = [];
foreach($node->types as $type) {
$parsedTypes[] = $this->parseTypeNode($type);
}

return new Op\Type\Union(
$parsedTypes,
$this->mapAttributes($node)
);
}
if ($node instanceof Node\Identifier) {
return new Op\Type\Literal(
$node->name,
Expand Down
12 changes: 12 additions & 0 deletions lib/PHPCfg/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,18 @@ protected function renderType(?Op\Type $type): string {
if ($type instanceof Op\Type\Nullable) {
return '?' . $this->renderType($type->subtype);
}
if ($type instanceof Op\Type\Union) {
$i = 1;
$strTypes = "";
foreach($type->subtypes as $subtype) {
$strTypes .= $this->renderType($subtype);
if($i < count($type->subtypes)) {
$strTypes .= "|";
}
$i ++;
}
return $strTypes;
}
if ($type instanceof Op\Type\Reference) {
return $this->renderOperand($type->declaration);
}
Expand Down
17 changes: 17 additions & 0 deletions test/code/unionType.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
function foo(array|string|null $string) {

}
-----
Block#1
Stmt_Function<foo>
Terminal_Return

Function foo(): mixed
Block#1
Expr_Param
declaredType: array|string|null
name: LITERAL('string')
result: Var#1<$string>
Terminal_Return