Updated from some -dev modules to alpha, beta or full releases
[yaffs-website] / vendor / psy / psysh / src / Command / ReflectingCommand.php
index 0bf8327c919a52d088249b8cf87e9057996a9f12..8e1091d63cdfbfd5b8a4c6b124c76d9f17896a66 100644 (file)
@@ -14,6 +14,7 @@ namespace Psy\Command;
 use Psy\CodeCleaner\NoReturnValue;
 use Psy\Context;
 use Psy\ContextAware;
+use Psy\Exception\ErrorException;
 use Psy\Exception\RuntimeException;
 use Psy\Util\Mirror;
 
@@ -84,6 +85,8 @@ abstract class ReflectingCommand extends Command implements ContextAware
     /**
      * Resolve a class or function name (with the current shell namespace).
      *
+     * @throws ErrorException when `self` or `static` is used in a non-class scope
+     *
      * @param string $name
      * @param bool   $includeFunctions (default: false)
      *
@@ -91,11 +94,27 @@ abstract class ReflectingCommand extends Command implements ContextAware
      */
     protected function resolveName($name, $includeFunctions = false)
     {
+        $shell = $this->getApplication();
+
+        // While not *technically* 100% accurate, let's treat `self` and `static` as equivalent.
+        if (in_array(strtolower($name), ['self', 'static'])) {
+            if ($boundClass = $shell->getBoundClass()) {
+                return $boundClass;
+            }
+
+            if ($boundObject = $shell->getBoundObject()) {
+                return get_class($boundObject);
+            }
+
+            $msg = sprintf('Cannot use "%s" when no class scope is active', strtolower($name));
+            throw new ErrorException($msg, 0, E_USER_ERROR, "eval()'d code", 1);
+        }
+
         if (substr($name, 0, 1) === '\\') {
             return $name;
         }
 
-        if ($namespace = $this->getApplication()->getNamespace()) {
+        if ($namespace = $shell->getNamespace()) {
             $fullName = $namespace . '\\' . $name;
 
             if (class_exists($fullName) || interface_exists($fullName) || ($includeFunctions && function_exists($fullName))) {