Skip to content

Commit

Permalink
FPPP: Add support for removal from list nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Nov 4, 2017
1 parent 361398b commit 56bc8eb
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
26 changes: 24 additions & 2 deletions lib/PhpParser/PrettyPrinterAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -766,8 +766,24 @@ protected function pArray(
$result .= $insertStr;
}
} else if ($diffType === DiffElem::TYPE_REMOVE) {
// TODO Support remove
return null;
if ($i === 0) {
// TODO Handle removal at the start
return null;
}

if (!$origArrItem instanceof Node) {
// We only support removal for nodes
return null;
}

$itemEndPos = $origArrItem->getEndTokenPos();
if ($itemEndPos < 0) {
// Shouldn't happen
return null;
}

$pos = $itemEndPos + 1;
continue;
} else {
throw new \Exception("Shouldn't happen");
}
Expand Down Expand Up @@ -1206,5 +1222,11 @@ protected function initializeModifierChangeMap() {
'Stmt_Property->flags' => T_VARIABLE,
//'Stmt_TraitUseAdaptation_Alias->newModifier' => 0, // TODO
];

// List of integer subnodes that are not modifiers:
// Expr_Include->type
// Stmt_GroupUse->type
// Stmt_Use->type
// Stmt_UseUse->type
}
}
6 changes: 3 additions & 3 deletions test/code/formatPreservation/abc1.test
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,12 @@ call2(
$tmp = $stmts[0]->stmts[0];
$stmts[0]->stmts[0] = $stmts[1];
$stmts[1] = $tmp;
// Same test, but also appending to $stms, triggering fallback
$stmts[] = new Stmt\Echo_([new Scalar\LNumber(42)]);
// Same test, but also prepending to $stmts, triggering fallback
array_unshift($stmts, new Stmt\Echo_([new Scalar\LNumber(42)]));
-----
<?php

echo 42;
function test() {
call2(
$foo
Expand All @@ -114,7 +115,6 @@ function test() {
call1(
$bar
);
echo 42;
-----
<?php
echo 1;
Expand Down
40 changes: 40 additions & 0 deletions test/code/formatPreservation/listRemoval.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Removing from list nodes
-----
<?php $foo; $bar; $baz;
-----
array_splice($stmts, 1, 1, []);
-----
<?php $foo; $baz;
-----
<?php
function foo(
$a,
$b,
$c
) {}
-----
array_pop($stmts[0]->params);
-----
<?php
function foo(
$a,
$b
) {}
-----
<?php
function foo(
$a,
$b,
$c
) {}
-----
array_pop($stmts[0]->params);
$stmts[0]->params[] = new Node\Param(new Expr\Variable('x'));
$stmts[0]->params[] = new Node\Param(new Expr\Variable('y'));
/* TODO The insertion here should try to to honor the style */
-----
<?php
function foo(
$a,
$b, $x, $y
) {}

0 comments on commit 56bc8eb

Please sign in to comment.