From 4ba906174fec8a2564a508cdc1693612b22b2ec1 Mon Sep 17 00:00:00 2001
From: Tim van Dijen <tvdijen@gmail.com>
Date: Tue, 9 Jul 2019 20:18:22 +0200
Subject: [PATCH] Fix Psalm-issues in Memcache.php

---
 lib/SimpleSAML/Memcache.php | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)

diff --git a/lib/SimpleSAML/Memcache.php b/lib/SimpleSAML/Memcache.php
index 904649775..35f862981 100644
--- a/lib/SimpleSAML/Memcache.php
+++ b/lib/SimpleSAML/Memcache.php
@@ -2,6 +2,8 @@
 
 namespace SimpleSAML;
 
+use Memcache as _Memcache;
+use Memcached as _Memcached;
 use SimpleSAML\Utils;
 
 /**
@@ -26,7 +28,7 @@ class Memcache
     /**
      * Cache of the memcache servers we are using.
      *
-     * @var \Memcache[]|null
+     * @var \Memcache[]|\Memcached[]|null
      */
     private static $serverGroups = null;
 
@@ -212,7 +214,7 @@ class Memcache
      *    The timeout for contacting this server, in seconds.
      *    The default value is 3 seconds.
      *
-     * @param \Memcache $memcache The Memcache object we should add this server to.
+     * @param \Memcache|\Memcached $memcache The Memcache object we should add this server to.
      * @param array    $server An associative array with the configuration options for the server to add.
      * @return void
      *
@@ -290,7 +292,7 @@ class Memcache
         }
 
         // add this server to the Memcache object
-        if (self::$extension === '\memcached') {
+        if ($memcache instanceof \Memcached) {
             $memcache->addServer($hostname, $port);
         } else {
             $memcache->addServer($hostname, $port, true, $weight, $timeout, $timeout, true);
@@ -304,24 +306,25 @@ class Memcache
      *
      * @param array $group Array of servers which should be created as a group.
      *
-     * @return \Memcache A Memcache object of the servers in the group
+     * @return \Memcache|\Memcached A Memcache object of the servers in the group
      *
      * @throws \Exception If the servers configuration is invalid.
      */
     private static function loadMemcacheServerGroup(array $group)
     {
-        $class = class_exists('\Memcache') ? '\Memcache' : (class_exists('\Memcached') ? '\Memcached' : false);
-        if (!$class) {
+        if (class_exists(_Memcache::class)) {
+            $memcache = new _Memcache();
+            self::$extension = strtolower(_Memcache::class);
+        } elseif (class_exists(_Memcached::class)) {
+            $memcache = new _Memcached();
+            self::$extension = strtolower(_Memcached::class);
+        } else {
             throw new \Exception(
                 'Missing Memcached implementation. You must install either the Memcache or Memcached extension.'
             );
         } elseif (strtolower($class) === '\memcache') {
             Logger::warning("The use of PHP-extension memcache is deprecated. Please migrate to the memcached extension.");
         }
-        self::$extension = strtolower($class);
-
-        // create the \Memcache object
-        $memcache = new $class();
 
         // iterate over all the servers in the group and add them to the Memcache object
         foreach ($group as $index => $server) {
@@ -346,6 +349,7 @@ class Memcache
             self::addMemcacheServer($memcache, $server);
         }
 
+        /** @var \Memcache|\Memcached */
         return $memcache;
     }
 
@@ -354,7 +358,7 @@ class Memcache
      * This function gets a list of all configured memcache servers. This list is initialized based
      * on the content of 'memcache_store.servers' in the configuration.
      *
-     * @return \Memcache[] Array with Memcache objects.
+     * @return \Memcache[]|\Memcached[] Array with Memcache objects.
      *
      * @throws \Exception If the servers configuration is invalid.
      */
-- 
GitLab