From 1ec55915746fee2325045f7460140508c8fd3758 Mon Sep 17 00:00:00 2001
From: Nikita Popov <nikita.ppv@googlemail.com>
Date: Sat, 25 Mar 2017 21:21:10 +0100
Subject: [PATCH] Tweak node insertion formatting

---
 lib/PhpParser/PrettyPrinterAbstract.php               | 8 ++++++--
 test/code/formatPreservation/insertionOfNullable.test | 8 ++++----
 2 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/lib/PhpParser/PrettyPrinterAbstract.php b/lib/PhpParser/PrettyPrinterAbstract.php
index 9ebba623..1f6efaa6 100644
--- a/lib/PhpParser/PrettyPrinterAbstract.php
+++ b/lib/PhpParser/PrettyPrinterAbstract.php
@@ -521,6 +521,10 @@ abstract class PrettyPrinterAbstract
                 } else {
                     $subStartPos = $pos;
                 }
+                if (null === $extraLeft && null !== $extraRight) {
+                    // If inserting on the right only, skipping whitespace looks better
+                    $subStartPos = $this->skipRightWhitespace($subStartPos);
+                }
                 $subEndPos = $subStartPos - 1;
             }
 
@@ -1128,7 +1132,7 @@ abstract class PrettyPrinterAbstract
             'Expr_ArrayItem->key' => [null, null, ' => '],
             'Expr_Closure->returnType' => [')', ' : ', null],
             'Expr_Ternary->if' => ['?', ' ', ' '],
-            'Expr_Yield->key' => [T_YIELD, ' ', ' => '],
+            'Expr_Yield->key' => [T_YIELD, null, ' => '],
             'Expr_Yield->value' => [T_YIELD, ' ', null],
             'Param->type' => [null, null, ' '],
             'Param->default' => [null, ' = ', null],
@@ -1136,7 +1140,7 @@ abstract class PrettyPrinterAbstract
             'Stmt_ClassMethod->returnType' => [')', ' : ', null],
             'Stmt_Class->extends' => [null, ' extends ', null],
             'Stmt_Continue->num' => [T_CONTINUE, ' ', null],
-            'Stmt_Foreach->keyVar' => [T_AS, ' ', ' => '],
+            'Stmt_Foreach->keyVar' => [T_AS, null, ' => '],
             'Stmt_Function->returnType' => [')', ' : ', null],
             //'Stmt_If->else' => [null, ' ', null], // TODO
             'Stmt_Namespace->name' => [T_NAMESPACE, ' ', null],
diff --git a/test/code/formatPreservation/insertionOfNullable.test b/test/code/formatPreservation/insertionOfNullable.test
index 2bb34192..3469a8bb 100644
--- a/test/code/formatPreservation/insertionOfNullable.test
+++ b/test/code/formatPreservation/insertionOfNullable.test
@@ -114,8 +114,8 @@ $x
 :
 $y;
 
-yield $k =>
-$v  ;
+yield
+$k => $v  ;
 yield $v  ;
 
 break 2
@@ -139,8 +139,8 @@ X extends \Bar
 
 foreach (
     $x
-    as $z =>
-    $y
+    as
+    $z => $y
 ) {}
 
 static