From 866df1b6857465b4d303660caefc80377609ecca Mon Sep 17 00:00:00 2001 From: Tim van Dijen <tvdijen@gmail.com> Date: Sun, 11 Aug 2019 20:44:14 +0200 Subject: [PATCH] Fully typehint lib/Store/*.php --- lib/SimpleSAML/Store/Memcache.php | 14 +++---------- lib/SimpleSAML/Store/Redis.php | 14 +++---------- lib/SimpleSAML/Store/SQL.php | 33 +++++++++---------------------- 3 files changed, 15 insertions(+), 46 deletions(-) diff --git a/lib/SimpleSAML/Store/Memcache.php b/lib/SimpleSAML/Store/Memcache.php index d704cf559..cff7bbc94 100644 --- a/lib/SimpleSAML/Store/Memcache.php +++ b/lib/SimpleSAML/Store/Memcache.php @@ -40,11 +40,8 @@ class Memcache extends Store * @param string $key The key. * @return mixed|null The value. */ - public function get($type, $key) + public function get(string $type, string $key) { - Assert::string($type); - Assert::string($key); - return \SimpleSAML\Memcache::get($this->prefix . '.' . $type . '.' . $key); } @@ -58,10 +55,8 @@ class Memcache extends Store * @param int|null $expire The expiration time (unix timestamp), or NULL if it never expires. * @return void */ - public function set($type, $key, $value, $expire = null) + public function set(string $type, string $key, $value, ?int $expire = null): void { - Assert::string($type); - Assert::string($key); Assert::nullOrGreaterThan($expire, 2592000); if ($expire === null) { @@ -79,11 +74,8 @@ class Memcache extends Store * @param string $key The key. * @return void */ - public function delete($type, $key) + public function delete(string $type, string $key): void { - Assert::string($type); - Assert::string($key); - \SimpleSAML\Memcache::delete($this->prefix . '.' . $type . '.' . $key); } } diff --git a/lib/SimpleSAML/Store/Redis.php b/lib/SimpleSAML/Store/Redis.php index f502977bc..85ee3562a 100644 --- a/lib/SimpleSAML/Store/Redis.php +++ b/lib/SimpleSAML/Store/Redis.php @@ -77,11 +77,8 @@ class Redis extends Store * * @return mixed|null The value associated with that key, or null if there's no such key. */ - public function get($type, $key) + public function get(string $type, string $key) { - Assert::string($type); - Assert::string($key); - $result = $this->redis->get("{$type}.{$key}"); if ($result === false || $result === null) { @@ -101,10 +98,8 @@ class Redis extends Store * @param int|null $expire The expiration time (unix timestamp), or null if it never expires. * @return void */ - public function set($type, $key, $value, $expire = null) + public function set(string $type, string $key, $value, ?int $expire = null): void { - Assert::string($type); - Assert::string($key); Assert::nullOrGreaterThan($expire, 2592000); $serialized = serialize($value); @@ -125,11 +120,8 @@ class Redis extends Store * @param string $key The key to delete. * @return void */ - public function delete($type, $key) + public function delete(string $type, string $key): void { - Assert::string($type); - Assert::string($key); - $this->redis->del("{$type}.{$key}"); } } diff --git a/lib/SimpleSAML/Store/SQL.php b/lib/SimpleSAML/Store/SQL.php index 501cc68aa..e9e8418bb 100644 --- a/lib/SimpleSAML/Store/SQL.php +++ b/lib/SimpleSAML/Store/SQL.php @@ -84,7 +84,7 @@ class SQL extends Store * Initialize the table-version table. * @return void */ - private function initTableVersionTable() + private function initTableVersionTable(): void { $this->tableVersions = []; @@ -108,7 +108,7 @@ class SQL extends Store * Initialize key-value table. * @return void */ - private function initKVTable() + private function initKVTable(): void { $current_version = $this->getTableVersion('kvstore'); @@ -188,10 +188,8 @@ class SQL extends Store * * @return int The table version, or 0 if the table doesn't exist. */ - public function getTableVersion($name) + public function getTableVersion(string $name): int { - Assert::string($name); - if (!isset($this->tableVersions[$name])) { return 0; } @@ -207,11 +205,8 @@ class SQL extends Store * @param int $version Table version. * @return void */ - public function setTableVersion($name, $version) + public function setTableVersion(string $name, int $version): void { - Assert::string($name); - Assert::integer($version); - $this->insertOrUpdate( $this->prefix . '_tableVersion', ['_name'], @@ -231,10 +226,8 @@ class SQL extends Store * @param array $data Associative array with columns. * @return void */ - public function insertOrUpdate($table, array $keys, array $data) + public function insertOrUpdate(string $table, array $keys, array $data): void { - Assert::string($table); - $colNames = '(' . implode(', ', array_keys($data)) . ')'; $values = 'VALUES(:' . implode(', :', array_keys($data)) . ')'; @@ -289,7 +282,7 @@ class SQL extends Store * Clean the key-value table of expired entries. * @return void */ - private function cleanKVStore() + private function cleanKVStore(): void { Logger::debug('store.sql: Cleaning key-value store.'); @@ -309,11 +302,8 @@ class SQL extends Store * * @return mixed|null The value associated with that key, or null if there's no such key. */ - public function get($type, $key) + public function get(string $type, string $key) { - Assert::string($type); - Assert::string($key); - if (strlen($key) > 50) { $key = sha1($key); } @@ -353,10 +343,8 @@ class SQL extends Store * @param int|null $expire The expiration time (unix timestamp), or null if it never expires. * @return void */ - public function set($type, $key, $value, $expire = null) + public function set(string $type, string $key, $value, ?int $expire = null): void { - Assert::string($type); - Assert::string($key); Assert::nullOrGreaterThan($expire, 2592000); if (rand(0, 1000) < 10) { @@ -392,11 +380,8 @@ class SQL extends Store * @param string $key The key to delete. * @return void */ - public function delete($type, $key) + public function delete(string $type, string $key): void { - Assert::string($type); - Assert::string($key); - if (strlen($key) > 50) { $key = sha1($key); } -- GitLab