Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / psy / psysh / src / CodeCleaner / ValidClassNamePass.php
index 5de75d5f9154853cffb5b033b0dc960b46e72f70..9578c9a86f7939e971cf1c3d32f422c900294636 100644 (file)
@@ -38,14 +38,12 @@ class ValidClassNamePass extends NamespaceAwarePass
     const INTERFACE_TYPE = 'interface';
     const TRAIT_TYPE     = 'trait';
 
-    protected $checkTraits;
     private $conditionalScopes = 0;
     private $atLeastPhp55;
 
     public function __construct()
     {
-        $this->checkTraits = function_exists('trait_exists');
-        $this->atLeastPhp55 = version_compare(PHP_VERSION, '5.5', '>=');
+        $this->atLeastPhp55 = \version_compare(PHP_VERSION, '5.5', '>=');
     }
 
     /**
@@ -166,7 +164,7 @@ class ValidClassNamePass extends NamespaceAwarePass
     protected function validateClassConstFetchExpression(ClassConstFetch $stmt)
     {
         // there is no need to check exists for ::class const for php 5.5 or newer
-        if (strtolower($stmt->name) === 'class' && $this->atLeastPhp55) {
+        if (\strtolower($stmt->name) === 'class' && $this->atLeastPhp55) {
             return;
         }
 
@@ -212,12 +210,12 @@ class ValidClassNamePass extends NamespaceAwarePass
         }
 
         if ($errorType !== null) {
-            throw $this->createError(sprintf('%s named %s already exists', ucfirst($errorType), $name), $stmt);
+            throw $this->createError(\sprintf('%s named %s already exists', \ucfirst($errorType), $name), $stmt);
         }
 
         // Store creation for the rest of this code snippet so we can find local
         // issue too
-        $this->currentScope[strtolower($name)] = $scopeType;
+        $this->currentScope[\strtolower($name)] = $scopeType;
     }
 
     /**
@@ -231,7 +229,7 @@ class ValidClassNamePass extends NamespaceAwarePass
     protected function ensureClassExists($name, $stmt)
     {
         if (!$this->classExists($name)) {
-            throw $this->createError(sprintf('Class \'%s\' not found', $name), $stmt);
+            throw $this->createError(\sprintf('Class \'%s\' not found', $name), $stmt);
         }
     }
 
@@ -246,7 +244,22 @@ class ValidClassNamePass extends NamespaceAwarePass
     protected function ensureClassOrInterfaceExists($name, $stmt)
     {
         if (!$this->classExists($name) && !$this->interfaceExists($name)) {
-            throw $this->createError(sprintf('Class \'%s\' not found', $name), $stmt);
+            throw $this->createError(\sprintf('Class \'%s\' not found', $name), $stmt);
+        }
+    }
+
+    /**
+     * Ensure that a referenced class _or trait_ exists.
+     *
+     * @throws FatalErrorException
+     *
+     * @param string $name
+     * @param Stmt   $stmt
+     */
+    protected function ensureClassOrTraitExists($name, $stmt)
+    {
+        if (!$this->classExists($name) && !$this->traitExists($name)) {
+            throw $this->createError(\sprintf('Class \'%s\' not found', $name), $stmt);
         }
     }
 
@@ -261,10 +274,10 @@ class ValidClassNamePass extends NamespaceAwarePass
      */
     protected function ensureMethodExists($class, $name, $stmt)
     {
-        $this->ensureClassExists($class, $stmt);
+        $this->ensureClassOrTraitExists($class, $stmt);
 
         // let's pretend all calls to self, parent and static are valid
-        if (in_array(strtolower($class), ['self', 'parent', 'static'])) {
+        if (\in_array(\strtolower($class), ['self', 'parent', 'static'])) {
             return;
         }
 
@@ -278,8 +291,8 @@ class ValidClassNamePass extends NamespaceAwarePass
             return;
         }
 
-        if (!method_exists($class, $name) && !method_exists($class, '__callStatic')) {
-            throw $this->createError(sprintf('Call to undefined method %s::%s()', $class, $name), $stmt);
+        if (!\method_exists($class, $name) && !\method_exists($class, '__callStatic')) {
+            throw $this->createError(\sprintf('Call to undefined method %s::%s()', $class, $name), $stmt);
         }
     }
 
@@ -297,7 +310,7 @@ class ValidClassNamePass extends NamespaceAwarePass
             /** @var string $name */
             $name = $this->getFullyQualifiedName($interface);
             if (!$this->interfaceExists($name)) {
-                throw $this->createError(sprintf('Interface \'%s\' not found', $name), $stmt);
+                throw $this->createError(\sprintf('Interface \'%s\' not found', $name), $stmt);
             }
         }
     }
@@ -337,11 +350,11 @@ class ValidClassNamePass extends NamespaceAwarePass
         // Give `self`, `static` and `parent` a pass. This will actually let
         // some errors through, since we're not checking whether the keyword is
         // being used in a class scope.
-        if (in_array(strtolower($name), ['self', 'static', 'parent'])) {
+        if (\in_array(\strtolower($name), ['self', 'static', 'parent'])) {
             return true;
         }
 
-        return class_exists($name) || $this->findInScope($name) === self::CLASS_TYPE;
+        return \class_exists($name) || $this->findInScope($name) === self::CLASS_TYPE;
     }
 
     /**
@@ -353,7 +366,7 @@ class ValidClassNamePass extends NamespaceAwarePass
      */
     protected function interfaceExists($name)
     {
-        return interface_exists($name) || $this->findInScope($name) === self::INTERFACE_TYPE;
+        return \interface_exists($name) || $this->findInScope($name) === self::INTERFACE_TYPE;
     }
 
     /**
@@ -365,7 +378,7 @@ class ValidClassNamePass extends NamespaceAwarePass
      */
     protected function traitExists($name)
     {
-        return $this->checkTraits && (trait_exists($name) || $this->findInScope($name) === self::TRAIT_TYPE);
+        return \trait_exists($name) || $this->findInScope($name) === self::TRAIT_TYPE;
     }
 
     /**
@@ -377,7 +390,7 @@ class ValidClassNamePass extends NamespaceAwarePass
      */
     protected function findInScope($name)
     {
-        $name = strtolower($name);
+        $name = \strtolower($name);
         if (isset($this->currentScope[$name])) {
             return $this->currentScope[$name];
         }