Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / psy / psysh / src / Command / ThrowUpCommand.php
index 4b7003d1042d1f10a4900abbfc01666fed6d5a55..b37f7573c10ffa240393cb0d928097a0887d0b2c 100644 (file)
 namespace Psy\Command;
 
 use PhpParser\Node\Arg;
+use PhpParser\Node\Expr\New_;
 use PhpParser\Node\Expr\StaticCall;
 use PhpParser\Node\Expr\Variable;
 use PhpParser\Node\Name\FullyQualified as FullyQualifiedName;
+use PhpParser\Node\Scalar\String_;
 use PhpParser\Node\Stmt\Throw_;
 use PhpParser\PrettyPrinter\Standard as Printer;
 use Psy\Context;
@@ -85,6 +87,7 @@ e.g.
 <return>>>> throw-up</return>
 <return>>>> throw-up $e</return>
 <return>>>> throw-up new Exception('WHEEEEEE!')</return>
+<return>>>> throw-up "bye!"</return>
 HELP
             );
     }
@@ -122,17 +125,28 @@ HELP
             return [new Arg(new Variable('_e'))];
         }
 
-        if (strpos('<?', $code) === false) {
+        if (\strpos('<?', $code) === false) {
             $code = '<?php ' . $code;
         }
 
-        $expr = $this->parse($code);
-
-        if (count($expr) !== 1) {
+        $nodes = $this->parse($code);
+        if (\count($nodes) !== 1) {
             throw new \InvalidArgumentException('No idea how to throw this');
         }
 
-        return [new Arg($expr[0])];
+        $node = $nodes[0];
+
+        // Make this work for PHP Parser v3.x
+        $expr = isset($node->expr) ? $node->expr : $node;
+
+        $args = [new Arg($expr, false, false, $node->getAttributes())];
+
+        // Allow throwing via a string, e.g. `throw-up "SUP"`
+        if ($expr instanceof String_) {
+            return [new New_(new FullyQualifiedName('Exception'), $args)];
+        }
+
+        return $args;
     }
 
     /**
@@ -147,7 +161,7 @@ HELP
         try {
             return $this->parser->parse($code);
         } catch (\PhpParser\Error $e) {
-            if (strpos($e->getMessage(), 'unexpected EOF') === false) {
+            if (\strpos($e->getMessage(), 'unexpected EOF') === false) {
                 throw $e;
             }