From 300f74eef8bcda3d17e22d67d60e3df7453bafe2 Mon Sep 17 00:00:00 2001
From: Tim van Dijen <tvdijen@gmail.com>
Date: Sat, 22 Feb 2020 00:15:16 +0100
Subject: [PATCH] Psalm fixes

---
 composer.lock                                 | 37 ++++++++++---------
 lib/SimpleSAML/Auth/Simple.php                |  1 +
 lib/SimpleSAML/Command/RouterDebugCommand.php | 12 +++---
 lib/SimpleSAML/Configuration.php              |  2 +-
 lib/SimpleSAML/Database.php                   |  8 +---
 lib/SimpleSAML/Error/Exception.php            |  6 +--
 .../Error/UnserializableException.php         |  4 +-
 lib/SimpleSAML/Locale/Translate.php           |  3 +-
 lib/SimpleSAML/Memcache.php                   | 18 ++-------
 lib/SimpleSAML/Metadata/SAMLBuilder.php       |  2 +
 lib/SimpleSAML/Metadata/SAMLParser.php        |  1 -
 lib/SimpleSAML/Metadata/Sources/MDQ.php       |  4 +-
 lib/SimpleSAML/Module.php                     |  6 ++-
 13 files changed, 47 insertions(+), 57 deletions(-)

diff --git a/composer.lock b/composer.lock
index aafe266a6..0ea0c1f76 100644
--- a/composer.lock
+++ b/composer.lock
@@ -2926,34 +2926,35 @@
         },
         {
             "name": "ocramius/package-versions",
-            "version": "1.4.2",
+            "version": "1.5.1",
             "source": {
                 "type": "git",
                 "url": "https://github.com/Ocramius/PackageVersions.git",
-                "reference": "44af6f3a2e2e04f2af46bcb302ad9600cba41c7d"
+                "reference": "1d32342b8c1eb27353c8887c366147b4c2da673c"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/Ocramius/PackageVersions/zipball/44af6f3a2e2e04f2af46bcb302ad9600cba41c7d",
-                "reference": "44af6f3a2e2e04f2af46bcb302ad9600cba41c7d",
+                "url": "https://api.github.com/repos/Ocramius/PackageVersions/zipball/1d32342b8c1eb27353c8887c366147b4c2da673c",
+                "reference": "1d32342b8c1eb27353c8887c366147b4c2da673c",
                 "shasum": ""
             },
             "require": {
                 "composer-plugin-api": "^1.0.0",
-                "php": "^7.1.0"
+                "php": "^7.3.0"
             },
             "require-dev": {
-                "composer/composer": "^1.6.3",
-                "doctrine/coding-standard": "^5.0.1",
+                "composer/composer": "^1.8.6",
+                "doctrine/coding-standard": "^6.0.0",
                 "ext-zip": "*",
-                "infection/infection": "^0.7.1",
-                "phpunit/phpunit": "^7.5.17"
+                "infection/infection": "^0.13.4",
+                "phpunit/phpunit": "^8.2.5",
+                "vimeo/psalm": "^3.4.9"
             },
             "type": "composer-plugin",
             "extra": {
                 "class": "PackageVersions\\Installer",
                 "branch-alias": {
-                    "dev-master": "2.0.x-dev"
+                    "dev-master": "1.6.x-dev"
                 }
             },
             "autoload": {
@@ -2972,7 +2973,7 @@
                 }
             ],
             "description": "Composer plugin that provides efficient querying for installed package versions (no runtime IO)",
-            "time": "2019-11-15T16:17:10+00:00"
+            "time": "2019-07-17T15:49:50+00:00"
         },
         {
             "name": "openlss/lib-array2xml",
@@ -4482,16 +4483,16 @@
         },
         {
             "name": "vimeo/psalm",
-            "version": "3.8.5",
+            "version": "3.9.3",
             "source": {
                 "type": "git",
                 "url": "https://github.com/vimeo/psalm.git",
-                "reference": "e6ec5fa22a7b9e61670a24d07b3119aff80dcd89"
+                "reference": "2e4154d76e24d1b4e59e6cc2bebef7790cb9e550"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/vimeo/psalm/zipball/e6ec5fa22a7b9e61670a24d07b3119aff80dcd89",
-                "reference": "e6ec5fa22a7b9e61670a24d07b3119aff80dcd89",
+                "url": "https://api.github.com/repos/vimeo/psalm/zipball/2e4154d76e24d1b4e59e6cc2bebef7790cb9e550",
+                "reference": "2e4154d76e24d1b4e59e6cc2bebef7790cb9e550",
                 "shasum": ""
             },
             "require": {
@@ -4522,10 +4523,10 @@
             "require-dev": {
                 "bamarni/composer-bin-plugin": "^1.2",
                 "ext-curl": "*",
-                "phpmyadmin/sql-parser": "^5.0",
+                "phpmyadmin/sql-parser": "5.1.0",
                 "phpspec/prophecy": ">=1.9.0",
                 "phpunit/phpunit": "^7.5.16 || ^8.0",
-                "psalm/plugin-phpunit": "^0.6",
+                "psalm/plugin-phpunit": "^0.9",
                 "slevomat/coding-standard": "^5.0",
                 "squizlabs/php_codesniffer": "^3.5",
                 "symfony/process": "^4.3"
@@ -4573,7 +4574,7 @@
                 "inspection",
                 "php"
             ],
-            "time": "2020-02-07T17:15:50+00:00"
+            "time": "2020-02-19T01:30:37+00:00"
         },
         {
             "name": "webmozart/glob",
diff --git a/lib/SimpleSAML/Auth/Simple.php b/lib/SimpleSAML/Auth/Simple.php
index 42194223e..2c14c8ffb 100644
--- a/lib/SimpleSAML/Auth/Simple.php
+++ b/lib/SimpleSAML/Auth/Simple.php
@@ -46,6 +46,7 @@ class Simple
             $config = Configuration::getInstance();
         }
         $this->authSource = $authSource;
+        /** @psalm-var \SimpleSAML\Configuration $this->app_config */
         $this->app_config = $config->getConfigItem('application');
 
         if ($session === null) {
diff --git a/lib/SimpleSAML/Command/RouterDebugCommand.php b/lib/SimpleSAML/Command/RouterDebugCommand.php
index d25385374..3f6124dc6 100644
--- a/lib/SimpleSAML/Command/RouterDebugCommand.php
+++ b/lib/SimpleSAML/Command/RouterDebugCommand.php
@@ -40,7 +40,7 @@ class RouterDebugCommand extends Command
     /**
      * @return void
      */
-    protected function configure()
+    protected function configure(): void
     {
         $this
             ->setDescription('Displays current routes for a module')
@@ -58,16 +58,16 @@ EOF
     /**
      * @param \Symfony\Component\Console\Input\InputInterface $input
      * @param \Symfony\Component\Console\Output\OutputInterface $output
-     * @return void
+     * @return int
      */
-    protected function execute(InputInterface $input, OutputInterface $output)
+    protected function execute(InputInterface $input, OutputInterface $output): int
     {
         $io = new SymfonyStyle($input, $output);
         $routes = $this->router->getRouteCollection();
 
-        $tableHeaders = array('Name', 'Method', 'Scheme', 'Host', 'Path', 'Controller');
+        $tableHeaders = ['Name', 'Method', 'Scheme', 'Host', 'Path', 'Controller'];
 
-        $tableRows = array();
+        $tableRows = [];
         foreach ($routes->all() as $name => $route) {
             $row = [
                 $name,
@@ -92,5 +92,7 @@ EOF
         $table->setHeaders($tableHeaders)->setRows($tableRows);
         $table->setStyle('compact');
         $table->render();
+
+        return 0;
     }
 }
diff --git a/lib/SimpleSAML/Configuration.php b/lib/SimpleSAML/Configuration.php
index 0a3639738..384e6ef89 100644
--- a/lib/SimpleSAML/Configuration.php
+++ b/lib/SimpleSAML/Configuration.php
@@ -1028,7 +1028,7 @@ class Configuration implements Utils\ClearableState
         string $endpointType,
         array $bindings,
         $default = self::REQUIRED_OPTION
-    ): array {
+    ) {
         $endpoints = $this->getEndpoints($endpointType);
 
         foreach ($bindings as $binding) {
diff --git a/lib/SimpleSAML/Database.php b/lib/SimpleSAML/Database.php
index e5604ca5e..59771d0cc 100644
--- a/lib/SimpleSAML/Database.php
+++ b/lib/SimpleSAML/Database.php
@@ -253,13 +253,7 @@ class Database
      */
     public function write(string $stmt, array $params = [])
     {
-        $db = $this->dbMaster;
-
-        if (is_array($params)) {
-            return $this->query($db, $stmt, $params)->rowCount();
-        } else {
-            return $this->exec($db, $stmt);
-        }
+        return $this->query($this->dbMaster, $stmt, $params)->rowCount();
     }
 
 
diff --git a/lib/SimpleSAML/Error/Exception.php b/lib/SimpleSAML/Error/Exception.php
index 90e1b881f..943e9122f 100644
--- a/lib/SimpleSAML/Error/Exception.php
+++ b/lib/SimpleSAML/Error/Exception.php
@@ -32,7 +32,7 @@ class Exception extends \Exception
     /**
      * The cause of this exception.
      *
-     * @var \Exception|null
+     * @var \SimpleSAML\Error\Exception|null
      */
     private $cause = null;
 
@@ -121,9 +121,9 @@ class Exception extends \Exception
     /**
      * Retrieve the cause of this exception.
      *
-     * @return \Exception|null The cause of this exception.
+     * @return \SimpleSAML\Error\Exception|null The cause of this exception.
      */
-    public function getCause(): ?\Exception
+    public function getCause(): ?Exception
     {
         return $this->cause;
     }
diff --git a/lib/SimpleSAML/Error/UnserializableException.php b/lib/SimpleSAML/Error/UnserializableException.php
index 971d906a3..6661d473c 100644
--- a/lib/SimpleSAML/Error/UnserializableException.php
+++ b/lib/SimpleSAML/Error/UnserializableException.php
@@ -41,8 +41,8 @@ class UnserializableException extends Exception
         $msg = $original->getMessage();
         $code = $original->getCode();
 
-        if ($original instanceof PDOException) {
-            // PDOException uses a string as the code. Filter it out here.
+        if (!is_int($code)) {
+            // PDOException and possibly others use a string for the code. Filter it out here.
             $code = -1;
         }
 
diff --git a/lib/SimpleSAML/Locale/Translate.php b/lib/SimpleSAML/Locale/Translate.php
index 0fad76cac..0d1188c98 100644
--- a/lib/SimpleSAML/Locale/Translate.php
+++ b/lib/SimpleSAML/Locale/Translate.php
@@ -397,7 +397,7 @@ class Translate
 
         $text = BaseTranslator::$current->gettext($original);
 
-        if (func_num_args() === 1 || $original === null) {
+        if (func_num_args() === 1) {
             return $text;
         }
 
@@ -456,6 +456,7 @@ class Translate
 
         // we don't have a translation for the current language, load alternative priorities
         $sspcfg = Configuration::getInstance();
+        /** @psalm-var \SimpleSAML\Configuration $langcfg */
         $langcfg = $sspcfg->getConfigItem('language');
         $priorities = $langcfg->getArray('priorities', []);
 
diff --git a/lib/SimpleSAML/Memcache.php b/lib/SimpleSAML/Memcache.php
index 07302815e..b54eef3cf 100644
--- a/lib/SimpleSAML/Memcache.php
+++ b/lib/SimpleSAML/Memcache.php
@@ -33,12 +33,6 @@ class Memcache
      */
     private static $serverGroups = null;
 
-    /**
-     * The flavor of memcache PHP extension we are using.
-     *
-     * @var string
-     */
-    private static $extension = '';
 
     /**
      * Find data stored with a given key.
@@ -168,11 +162,7 @@ class Memcache
 
         // store this object to all groups of memcache servers
         foreach (self::getMemcacheServers() as $server) {
-            if (self::$extension === \Memcached::class) {
-                $server->set($key, $savedInfoSerialized, $expire);
-            } else {
-                $server->set($key, $savedInfoSerialized, 0, $expire);
-            }
+            $server->set($key, $savedInfoSerialized, $expire);
         }
     }
 
@@ -307,7 +297,6 @@ class Memcache
     private static function loadMemcacheServerGroup(array $group)
     {
         $memcache = new \Memcached();
-        self::$extension = \Memcached::class;
 
         // iterate over all the servers in the group and add them to the Memcache object
         foreach ($group as $index => $server) {
@@ -445,13 +434,14 @@ class Memcache
         $ret = [];
 
         foreach (self::getMemcacheServers() as $sg) {
-            $stats = method_exists($sg, 'getExtendedStats') ? $sg->getExtendedStats() : $sg->getStats();
+            $stats = $sg->getStats();
             foreach ($stats as $server => $data) {
                 if ($data === false) {
                     throw new \Exception('Failed to get memcache server status.');
                 }
             }
 
+            /** @psalm-var array $stats */
             $stats = Utils\Arrays::transpose($stats);
 
             $ret = array_merge_recursive($ret, $stats);
@@ -472,7 +462,7 @@ class Memcache
         $ret = [];
 
         foreach (self::getMemcacheServers() as $sg) {
-            $stats = method_exists($sg, 'getExtendedStats') ? $sg->getExtendedStats() : $sg->getStats();
+            $stats = $sg->getStats();
             $ret[] = $stats;
         }
 
diff --git a/lib/SimpleSAML/Metadata/SAMLBuilder.php b/lib/SimpleSAML/Metadata/SAMLBuilder.php
index d7ae63676..5c95059ca 100644
--- a/lib/SimpleSAML/Metadata/SAMLBuilder.php
+++ b/lib/SimpleSAML/Metadata/SAMLBuilder.php
@@ -152,6 +152,8 @@ class SAMLBuilder
 
         $metadata = Configuration::loadFromArray($metadata, $metadata['entityid']);
         $defaultEndpoint = $metadata->getDefaultEndpoint('SingleSignOnService');
+
+        /** @psalm-suppress UndefinedClass */
         $e = new SecurityTokenServiceType();
         $e->setLocation($defaultEndpoint['Location']);
 
diff --git a/lib/SimpleSAML/Metadata/SAMLParser.php b/lib/SimpleSAML/Metadata/SAMLParser.php
index bf036e0ed..f1b0a1c85 100644
--- a/lib/SimpleSAML/Metadata/SAMLParser.php
+++ b/lib/SimpleSAML/Metadata/SAMLParser.php
@@ -973,7 +973,6 @@ class SAMLParser
 
                     foreach ($e->getKeywords() as $uiItem) {
                         $keywords = $uiItem->getKeywords();
-                        /** @psalm-var string|null $language */
                         $language = $uiItem->getLanguage();
                         if (($keywords === [])) {
                             continue;
diff --git a/lib/SimpleSAML/Metadata/Sources/MDQ.php b/lib/SimpleSAML/Metadata/Sources/MDQ.php
index 00750c95b..373cb6f59 100644
--- a/lib/SimpleSAML/Metadata/Sources/MDQ.php
+++ b/lib/SimpleSAML/Metadata/Sources/MDQ.php
@@ -216,9 +216,7 @@ class MDQ extends \SimpleSAML\Metadata\MetaDataStorageSource
             case 'saml20-sp-remote':
                 return $entity->getMetadata20SP();
             case 'attributeauthority-remote':
-                $ret = $entity->getAttributeAuthorities();
-                return $ret[0];
-
+                return = $entity->getAttributeAuthorities();
             default:
                 Logger::warning(__CLASS__ . ': unknown metadata set: \'' . $set . '\'.');
         }
diff --git a/lib/SimpleSAML/Module.php b/lib/SimpleSAML/Module.php
index 8183a369f..d6f5c79ca 100644
--- a/lib/SimpleSAML/Module.php
+++ b/lib/SimpleSAML/Module.php
@@ -198,7 +198,7 @@ class Module
             $request->request->all(),
             $request->attributes->all(),
             $request->cookies->all(),
-            $request_files ?? [],
+            $request_files,
             $request->server->all(),
             $request->getContent()
         );
@@ -300,14 +300,16 @@ class Module
             }
         }
 
+        /** @psalm-var \SimpleSAML\Configuration $assetConfig */
         $assetConfig = $config->getConfigItem('assets');
+        /** @psalm-var \SimpleSAML\Configuration $cacheConfig */
         $cacheConfig = $assetConfig->getConfigItem('caching');
         $response = new BinaryFileResponse($path);
         $response->setCache([
             // "public" allows response caching even if the request was authenticated,
             // which is exactly what we want for static resources
             'public' => true,
-            'max_age' => (string)$cacheConfig->getInteger('max_age', 86400)
+            'max_age' => strval($cacheConfig->getInteger('max_age', 86400))
         ]);
         $response->setAutoLastModified();
         if ($cacheConfig->getBoolean('etag', false)) {
-- 
GitLab