From 7cf3718a3051408a3342f992f82f052556422b6a Mon Sep 17 00:00:00 2001 From: Tim van Dijen <tvdijen@gmail.com> Date: Wed, 24 Nov 2021 23:27:43 +0100 Subject: [PATCH] Fix several Psalm issues --- lib/SimpleSAML/Command/RouterDebugCommand.php | 2 +- lib/SimpleSAML/Locale/TwigTranslator.php | 4 ++++ lib/SimpleSAML/Module.php | 13 +++++++------ lib/SimpleSAML/Utils/Random.php | 2 +- modules/core/lib/Storage/SQLPermanentStorage.php | 3 ++- modules/saml/lib/Auth/Source/SP.php | 2 +- 6 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/SimpleSAML/Command/RouterDebugCommand.php b/lib/SimpleSAML/Command/RouterDebugCommand.php index 0df7febce..a144b5681 100644 --- a/lib/SimpleSAML/Command/RouterDebugCommand.php +++ b/lib/SimpleSAML/Command/RouterDebugCommand.php @@ -15,7 +15,7 @@ use Symfony\Component\Routing\RouterInterface; class RouterDebugCommand extends Command { /** - * @var string + * @var string|null */ protected static $defaultName = 'debug:router'; diff --git a/lib/SimpleSAML/Locale/TwigTranslator.php b/lib/SimpleSAML/Locale/TwigTranslator.php index 4017bab9f..80592998d 100644 --- a/lib/SimpleSAML/Locale/TwigTranslator.php +++ b/lib/SimpleSAML/Locale/TwigTranslator.php @@ -14,6 +14,10 @@ use Symfony\Contracts\Translation\TranslatorInterface; class TwigTranslator implements TranslatorInterface { + /** @var string|null $locale */ + private ?string $locale = null; + + /** @var callable $translator */ private $translator; /** diff --git a/lib/SimpleSAML/Module.php b/lib/SimpleSAML/Module.php index 8b1d9dde8..7d84ddd15 100644 --- a/lib/SimpleSAML/Module.php +++ b/lib/SimpleSAML/Module.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace SimpleSAML; +use Exception; use SimpleSAML\Assert\Assert; use SimpleSAML\Kernel; use SimpleSAML\Utils; @@ -348,7 +349,7 @@ class Module return $mod_config[$module]; } - throw new \Exception("Invalid module.enable value for the '$module' module."); + throw new Exception("Invalid module.enable value for the '$module' module."); } $core_module = array_key_exists($module, self::$core_modules) ? true : false; @@ -375,7 +376,7 @@ class Module $dh = scandir($path); if ($dh === false) { - throw new \Exception('Unable to open module directory "' . $path . '".'); + throw new Exception('Unable to open module directory "' . $path . '".'); } foreach ($dh as $f) { @@ -420,14 +421,14 @@ class Module // no module involved $className = $tmp[0]; if (!class_exists($className)) { - throw new \Exception("Could not resolve '$id': no class named '$className'."); + throw new Exception("Could not resolve '$id': no class named '$className'."); } } elseif (!in_array($tmp[0], self::getModules())) { // Module not installed - throw new \Exception('No module named \'' . $tmp[0] . '\' has been installed.'); + throw new Exception('No module named \'' . $tmp[0] . '\' has been installed.'); } elseif (!self::isModuleEnabled($tmp[0])) { // Module installed, but not enabled - throw new \Exception('The module \'' . $tmp[0] . '\' is not enabled.'); + throw new Exception('The module \'' . $tmp[0] . '\' is not enabled.'); } else { // should be a module // make sure empty types are handled correctly @@ -437,7 +438,7 @@ class Module } if ($subclass !== null && !is_subclass_of($className, $subclass)) { - throw new \Exception( + throw new Exception( 'Could not resolve \'' . $id . '\': The class \'' . $className . '\' isn\'t a subclass of \'' . $subclass . '\'.' ); diff --git a/lib/SimpleSAML/Utils/Random.php b/lib/SimpleSAML/Utils/Random.php index d5c7bc896..f0b303c1a 100644 --- a/lib/SimpleSAML/Utils/Random.php +++ b/lib/SimpleSAML/Utils/Random.php @@ -24,6 +24,6 @@ class Random */ public function generateID(): string { - return '_' . bin2hex(openssl_random_pseudo_bytes((int) ((self::ID_LENGTH - 1) / 2))); + return '_' . bin2hex(openssl_random_pseudo_bytes((self::ID_LENGTH - 1) / 2)); } } diff --git a/modules/core/lib/Storage/SQLPermanentStorage.php b/modules/core/lib/Storage/SQLPermanentStorage.php index 7c21976aa..b5e20ebd8 100644 --- a/modules/core/lib/Storage/SQLPermanentStorage.php +++ b/modules/core/lib/Storage/SQLPermanentStorage.php @@ -47,7 +47,8 @@ class SQLPermanentStorage } $dbfile = 'sqlite:' . $sqllitedir . $name . '.sqlite'; - if ($this->db = new PDO($dbfile)) { + $this->db = new PDO($dbfile); + if ($this->db) { $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT); $q = @$this->db->query('SELECT key1 FROM data LIMIT 1'); if ($q === false) { diff --git a/modules/saml/lib/Auth/Source/SP.php b/modules/saml/lib/Auth/Source/SP.php index 94f67f8f5..4424b9c66 100644 --- a/modules/saml/lib/Auth/Source/SP.php +++ b/modules/saml/lib/Auth/Source/SP.php @@ -512,7 +512,7 @@ class SP extends \SimpleSAML\Auth\Source $policy = null; if (is_string($state['saml:NameIDPolicy'])) { $policy = [ - 'Format' => (string) $state['saml:NameIDPolicy'], + 'Format' => $state['saml:NameIDPolicy'], 'AllowCreate' => true, ]; } elseif (is_array($state['saml:NameIDPolicy'])) { -- GitLab