Skip to content

Commit

Permalink
Further PHP 7.1 compatibility additions
Browse files Browse the repository at this point in the history
  • Loading branch information
tpunt committed Jun 12, 2017
1 parent e55ae37 commit 29822ef
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 29 deletions.
50 changes: 37 additions & 13 deletions src/AstReverter/AstReverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class AstReverter
{
private $indentationLevel = 0;
private $inCatchBlock = false;

const INDENTATION_CHAR = ' ';
const INDENTATION_SIZE = 4;
Expand Down Expand Up @@ -283,7 +284,7 @@ private function abstractProp(Node $lhs, $rhs, string $op) : string
private function argList(Node $node) : string
{
return '('
. $this->commaSeparatedValues($node)
. $this->commaSeparatedValues($node, ', ')
. ')';
}

Expand Down Expand Up @@ -312,7 +313,7 @@ private function array(Node $node) : string
}

if ($node->children !== []) {
$code .= $this->commaSeparatedValues($node);
$code .= $this->commaSeparatedValues($node, ', ');
}

$code .= $closing;
Expand Down Expand Up @@ -580,10 +581,14 @@ private function catchList(Node $node) : string
{
$code = '';

$this->inCatchBlock = true;

foreach ($node->children as $catch) {
$code .= $this->revertAST($catch);
}

$this->inCatchBlock = false;

return $code;
}

Expand Down Expand Up @@ -702,7 +707,7 @@ private function closure(Node $node) : string

if ($node->children['uses'] !== null) {
$code .= ' use ('
. $this->commaSeparatedValues($node->children['uses'])
. $this->commaSeparatedValues($node->children['uses'], ', ')
. ')';
}

Expand Down Expand Up @@ -753,15 +758,15 @@ private function coalesce(Node $node) : string
*
* The NULL check is required for list(,,, $a).
*/
private function commaSeparatedValues(Node $node, bool $space = true) : string
private function commaSeparatedValues(Node $node, string $separator) : string
{
$aggregator = [];

foreach ($node->children as $child) {
$aggregator[] = ($child === null) ? null : $this->revertAST($child);
}

return implode(',' . ($space ? ' ' : ''), $aggregator);
return implode($separator, $aggregator);
}

private function conditional(Node $node) : string
Expand All @@ -784,11 +789,28 @@ private function constDecl(Node $node, $setConst = true) : string
{
$code = '';

switch ($node->flags) {
case \ast\flags\MODIFIER_PUBLIC:
$code = 'public ';
break;
case \ast\flags\MODIFIER_PROTECTED:
$code = 'protected ';
break;
case \ast\flags\MODIFIER_PRIVATE:
$code = 'private ';
break;
case 0:
// nothing to do - for PHP 7.0
break;
default:
assert(false, "Unknown flag ({$node->flags}) for AST_CONST_DECL found.");
}

if ($setConst) {
$code .= 'const ';
}

$code .= $this->commaSeparatedValues($node);
$code .= $this->commaSeparatedValues($node, ', ');

return $code;
}
Expand Down Expand Up @@ -913,7 +935,7 @@ private function exit(Node $node) : string

private function exprList(Node $node) : string
{
return $this->commaSeparatedValues($node);
return $this->commaSeparatedValues($node, ', ');
}

private function for(Node $node) : string
Expand Down Expand Up @@ -1189,7 +1211,7 @@ private function label(Node $node) : string

private function list(Node $node) : string
{
return 'list(' . $this->commaSeparatedValues($node) . ')';
return 'list(' . $this->commaSeparatedValues($node, ', ') . ')';
}

private function magicConst(Node $node)
Expand Down Expand Up @@ -1431,7 +1453,7 @@ private function name(Node $node) : string

private function nameList(Node $node) : string
{
return $this->commaSeparatedValues($node);
return $this->commaSeparatedValues($node, $this->inCatchBlock ? ' | ' : ', ');
}

private function namespace(Node $node) : string
Expand Down Expand Up @@ -1507,7 +1529,7 @@ private function param(Node $node) : string
private function paramList(Node $node) : string
{
return '('
. $this->commaSeparatedValues($node)
. $this->commaSeparatedValues($node, ', ')
. ')';
}

Expand Down Expand Up @@ -1573,7 +1595,7 @@ private function propDecl(Node $node) : string
assert(false, "Unknown flag(s) ({$node->flags}) for AST_PROP_DECL found.");
}

$code .= $scope . $this->commaSeparatedValues($node, false);
$code .= $scope . $this->commaSeparatedValues($node, ',');

return $code;
}
Expand Down Expand Up @@ -1853,6 +1875,8 @@ private function type(Node $node) : string
return 'iterable';
case \ast\flags\TYPE_DOUBLE:
return 'float';
case \ast\flags\TYPE_VOID:
return 'void';
default:
assert(false, "Unknown flag ({$node->flags}) for AST_TYPE found.");
}
Expand Down Expand Up @@ -1900,7 +1924,7 @@ private function unset(Node $node) : string
private function use(Node $node, $setUse = true) : string
{
return $this->useAbstract($node, $setUse)
. $this->commaSeparatedValues($node);
. $this->commaSeparatedValues($node, ', ');
}

/**
Expand Down Expand Up @@ -1968,7 +1992,7 @@ private function useTrait(Node $node) : string
{
$code = 'use ';

$code .= $this->commaSeparatedValues($node->children['traits']);
$code .= $this->commaSeparatedValues($node->children['traits'], ', ');

if ($node->children['adaptations'] !== null) {
$code .= ' {' . PHP_EOL;
Expand Down
14 changes: 0 additions & 14 deletions tests/AstReverterTests/classConstantDefinitions.test

This file was deleted.

18 changes: 16 additions & 2 deletions tests/AstReverterTests/php71.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,24 @@ Nullable types
<=======>
<?php

function foo(?iterable $x, ?callable $y, int $z, iterable $i) : ?callable {}
function foo(?iterable $x, ?callable $y, int $z, iterable $i) : void {}
class Something
{
const PUBLIC_CONST_A = 1;
public const PUBLIC_CONST_B = 2;
protected const PROTECTED_CONST = 3;
private const PRIVATE_CONST = 4;
}
<=======>
<?php

function foo(?iterable $x, ?callable $y, int $z, iterable $i) : ?callable
function foo(?iterable $x, ?callable $y, int $z, iterable $i) : void
{
}
class Something
{
public const PUBLIC_CONST_A = 1;
public const PUBLIC_CONST_B = 2;
protected const PROTECTED_CONST = 3;
private const PRIVATE_CONST = 4;
}

0 comments on commit 29822ef

Please sign in to comment.