From d14e7a27e800f8e10f7616229c24ef235b5d7158 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Mon, 21 Feb 2022 12:55:28 +0000 Subject: [PATCH 1/3] feature: type safe getters closes #81 --- composer.json | 7 +- composer.lock | 216 +++++++++++------- src/FileHandler.php | 12 +- src/Handler.php | 16 +- src/Session.php | 40 ++-- src/SessionStore.php | 15 +- .../Helper/FunctionOverride/session_start.php | 4 +- test/phpunit/SessionStoreTest.php | 55 ++++- test/phpunit/SessionTest.php | 65 +++++- 9 files changed, 305 insertions(+), 125 deletions(-) diff --git a/composer.json b/composer.json index 889a99e..d157df0 100644 --- a/composer.json +++ b/composer.json @@ -4,11 +4,12 @@ "license": "MIT", "require": { - "php": ">=7.2" + "php": ">=8.0", + "phpgt/typesafegetter": "^v1.2" }, "require-dev": { - "phpunit/phpunit": "9.*", - "phpstan/phpstan": "1.0.2" + "phpunit/phpunit": "^9.5", + "phpstan/phpstan": "^v1.4" }, "autoload": { diff --git a/composer.lock b/composer.lock index c8bac1b..1b8982d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,58 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "2e4b232fb6d83b62e1c30a262a97fa21", - "packages": [], + "content-hash": "f32a6d1ec518670b8c827c0fcd60c47f", + "packages": [ + { + "name": "phpgt/typesafegetter", + "version": "v1.2.2", + "source": { + "type": "git", + "url": "https://github.com/PhpGt/TypeSafeGetter.git", + "reference": "ffeb5d847b3a4b36081a5cd180587fc4d83c6e31" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PhpGt/TypeSafeGetter/zipball/ffeb5d847b3a4b36081a5cd180587fc4d83c6e31", + "reference": "ffeb5d847b3a4b36081a5cd180587fc4d83c6e31", + "shasum": "" + }, + "require": { + "php": ">=8.0" + }, + "require-dev": { + "phpstan/phpstan": ">=0.12.42" + }, + "type": "library", + "autoload": { + "psr-4": { + "Gt\\TypeSafeGetter\\": "./src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Greg Bowler", + "email": "greg.bowler@g105b.com" + } + ], + "description": "An interface for objects that expose type-safe getter methods.", + "support": { + "issues": "https://github.com/PhpGt/TypeSafeGetter/issues", + "source": "https://github.com/PhpGt/TypeSafeGetter/tree/v1.2.2" + }, + "funding": [ + { + "url": "https://github.com/sponsors/PhpGt", + "type": "github" + } + ], + "time": "2021-01-30T12:18:23+00:00" + } + ], "packages-dev": [ { "name": "doctrine/instantiator", @@ -93,9 +143,6 @@ "require": { "php": "^7.1 || ^8.0" }, - "replace": { - "myclabs/deep-copy": "self.version" - }, "require-dev": { "doctrine/collections": "^1.0", "doctrine/common": "^2.6", @@ -103,12 +150,12 @@ }, "type": "library", "autoload": { - "psr-4": { - "DeepCopy\\": "src/DeepCopy/" - }, "files": [ "src/DeepCopy/deep_copy.php" - ] + ], + "psr-4": { + "DeepCopy\\": "src/DeepCopy/" + } }, "notification-url": "https://packagist.org/downloads/", "license": [ @@ -136,16 +183,16 @@ }, { "name": "nikic/php-parser", - "version": "v4.13.1", + "version": "v4.13.2", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "63a79e8daa781cac14e5195e63ed8ae231dd10fd" + "reference": "210577fe3cf7badcc5814d99455df46564f3c077" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/63a79e8daa781cac14e5195e63ed8ae231dd10fd", - "reference": "63a79e8daa781cac14e5195e63ed8ae231dd10fd", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/210577fe3cf7badcc5814d99455df46564f3c077", + "reference": "210577fe3cf7badcc5814d99455df46564f3c077", "shasum": "" }, "require": { @@ -186,9 +233,9 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v4.13.1" + "source": "https://github.com/nikic/PHP-Parser/tree/v4.13.2" }, - "time": "2021-11-03T20:52:16+00:00" + "time": "2021-11-30T19:35:32+00:00" }, { "name": "phar-io/manifest", @@ -252,16 +299,16 @@ }, { "name": "phar-io/version", - "version": "3.1.0", + "version": "3.2.1", "source": { "type": "git", "url": "https://github.com/phar-io/version.git", - "reference": "bae7c545bef187884426f042434e561ab1ddb182" + "reference": "4f7fd7836c6f332bb2933569e566a0d6c4cbed74" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phar-io/version/zipball/bae7c545bef187884426f042434e561ab1ddb182", - "reference": "bae7c545bef187884426f042434e561ab1ddb182", + "url": "https://api.github.com/repos/phar-io/version/zipball/4f7fd7836c6f332bb2933569e566a0d6c4cbed74", + "reference": "4f7fd7836c6f332bb2933569e566a0d6c4cbed74", "shasum": "" }, "require": { @@ -297,9 +344,9 @@ "description": "Library for handling version information and constraints", "support": { "issues": "https://github.com/phar-io/version/issues", - "source": "https://github.com/phar-io/version/tree/3.1.0" + "source": "https://github.com/phar-io/version/tree/3.2.1" }, - "time": "2021-02-23T14:00:09+00:00" + "time": "2022-02-21T01:04:05+00:00" }, { "name": "phpdocumentor/reflection-common", @@ -413,16 +460,16 @@ }, { "name": "phpdocumentor/type-resolver", - "version": "1.5.1", + "version": "1.6.0", "source": { "type": "git", "url": "https://github.com/phpDocumentor/TypeResolver.git", - "reference": "a12f7e301eb7258bb68acd89d4aefa05c2906cae" + "reference": "93ebd0014cab80c4ea9f5e297ea48672f1b87706" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/a12f7e301eb7258bb68acd89d4aefa05c2906cae", - "reference": "a12f7e301eb7258bb68acd89d4aefa05c2906cae", + "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/93ebd0014cab80c4ea9f5e297ea48672f1b87706", + "reference": "93ebd0014cab80c4ea9f5e297ea48672f1b87706", "shasum": "" }, "require": { @@ -457,22 +504,22 @@ "description": "A PSR-5 based resolver of Class names, Types and Structural Element Names", "support": { "issues": "https://github.com/phpDocumentor/TypeResolver/issues", - "source": "https://github.com/phpDocumentor/TypeResolver/tree/1.5.1" + "source": "https://github.com/phpDocumentor/TypeResolver/tree/1.6.0" }, - "time": "2021-10-02T14:08:47+00:00" + "time": "2022-01-04T19:58:01+00:00" }, { "name": "phpspec/prophecy", - "version": "1.14.0", + "version": "v1.15.0", "source": { "type": "git", "url": "https://github.com/phpspec/prophecy.git", - "reference": "d86dfc2e2a3cd366cee475e52c6bb3bbc371aa0e" + "reference": "bbcd7380b0ebf3961ee21409db7b38bc31d69a13" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpspec/prophecy/zipball/d86dfc2e2a3cd366cee475e52c6bb3bbc371aa0e", - "reference": "d86dfc2e2a3cd366cee475e52c6bb3bbc371aa0e", + "url": "https://api.github.com/repos/phpspec/prophecy/zipball/bbcd7380b0ebf3961ee21409db7b38bc31d69a13", + "reference": "bbcd7380b0ebf3961ee21409db7b38bc31d69a13", "shasum": "" }, "require": { @@ -524,22 +571,22 @@ ], "support": { "issues": "https://github.com/phpspec/prophecy/issues", - "source": "https://github.com/phpspec/prophecy/tree/1.14.0" + "source": "https://github.com/phpspec/prophecy/tree/v1.15.0" }, - "time": "2021-09-10T09:02:12+00:00" + "time": "2021-12-08T12:19:24+00:00" }, { "name": "phpstan/phpstan", - "version": "1.0.2", + "version": "1.4.6", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "e9e2a501102ba0b126b2f63a7f0a3b151056fe91" + "reference": "8a7761f1c520e0dad6e04d862fdc697445457cfe" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/e9e2a501102ba0b126b2f63a7f0a3b151056fe91", - "reference": "e9e2a501102ba0b126b2f63a7f0a3b151056fe91", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/8a7761f1c520e0dad6e04d862fdc697445457cfe", + "reference": "8a7761f1c520e0dad6e04d862fdc697445457cfe", "shasum": "" }, "require": { @@ -555,7 +602,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.0-dev" + "dev-master": "1.4-dev" } }, "autoload": { @@ -570,7 +617,7 @@ "description": "PHPStan - PHP Static Analysis Tool", "support": { "issues": "https://github.com/phpstan/phpstan/issues", - "source": "https://github.com/phpstan/phpstan/tree/1.0.2" + "source": "https://github.com/phpstan/phpstan/tree/1.4.6" }, "funding": [ { @@ -590,20 +637,20 @@ "type": "tidelift" } ], - "time": "2021-11-03T16:09:51+00:00" + "time": "2022-02-06T12:56:13+00:00" }, { "name": "phpunit/php-code-coverage", - "version": "9.2.8", + "version": "9.2.11", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "cf04e88a2e3c56fc1a65488afd493325b4c1bc3e" + "reference": "665a1ac0a763c51afc30d6d130dac0813092b17f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/cf04e88a2e3c56fc1a65488afd493325b4c1bc3e", - "reference": "cf04e88a2e3c56fc1a65488afd493325b4c1bc3e", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/665a1ac0a763c51afc30d6d130dac0813092b17f", + "reference": "665a1ac0a763c51afc30d6d130dac0813092b17f", "shasum": "" }, "require": { @@ -659,7 +706,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.8" + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.11" }, "funding": [ { @@ -667,20 +714,20 @@ "type": "github" } ], - "time": "2021-10-30T08:01:38+00:00" + "time": "2022-02-18T12:46:09+00:00" }, { "name": "phpunit/php-file-iterator", - "version": "3.0.5", + "version": "3.0.6", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-file-iterator.git", - "reference": "aa4be8575f26070b100fccb67faabb28f21f66f8" + "reference": "cf1c2e7c203ac650e352f4cc675a7021e7d1b3cf" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/aa4be8575f26070b100fccb67faabb28f21f66f8", - "reference": "aa4be8575f26070b100fccb67faabb28f21f66f8", + "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/cf1c2e7c203ac650e352f4cc675a7021e7d1b3cf", + "reference": "cf1c2e7c203ac650e352f4cc675a7021e7d1b3cf", "shasum": "" }, "require": { @@ -719,7 +766,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/php-file-iterator/issues", - "source": "https://github.com/sebastianbergmann/php-file-iterator/tree/3.0.5" + "source": "https://github.com/sebastianbergmann/php-file-iterator/tree/3.0.6" }, "funding": [ { @@ -727,7 +774,7 @@ "type": "github" } ], - "time": "2020-09-28T05:57:25+00:00" + "time": "2021-12-02T12:48:52+00:00" }, { "name": "phpunit/php-invoker", @@ -912,16 +959,16 @@ }, { "name": "phpunit/phpunit", - "version": "9.5.10", + "version": "9.5.14", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "c814a05837f2edb0d1471d6e3f4ab3501ca3899a" + "reference": "1883687169c017d6ae37c58883ca3994cfc34189" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/c814a05837f2edb0d1471d6e3f4ab3501ca3899a", - "reference": "c814a05837f2edb0d1471d6e3f4ab3501ca3899a", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/1883687169c017d6ae37c58883ca3994cfc34189", + "reference": "1883687169c017d6ae37c58883ca3994cfc34189", "shasum": "" }, "require": { @@ -972,11 +1019,11 @@ } }, "autoload": { - "classmap": [ - "src/" - ], "files": [ "src/Framework/Assert/Functions.php" + ], + "classmap": [ + "src/" ] }, "notification-url": "https://packagist.org/downloads/", @@ -999,11 +1046,11 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.5.10" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.5.14" }, "funding": [ { - "url": "https://phpunit.de/donate.html", + "url": "https://phpunit.de/sponsors.html", "type": "custom" }, { @@ -1011,7 +1058,7 @@ "type": "github" } ], - "time": "2021-09-25T07:38:51+00:00" + "time": "2022-02-18T12:54:07+00:00" }, { "name": "sebastian/cli-parser", @@ -1442,16 +1489,16 @@ }, { "name": "sebastian/exporter", - "version": "4.0.3", + "version": "4.0.4", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/exporter.git", - "reference": "d89cc98761b8cb5a1a235a6b703ae50d34080e65" + "reference": "65e8b7db476c5dd267e65eea9cab77584d3cfff9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/d89cc98761b8cb5a1a235a6b703ae50d34080e65", - "reference": "d89cc98761b8cb5a1a235a6b703ae50d34080e65", + "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/65e8b7db476c5dd267e65eea9cab77584d3cfff9", + "reference": "65e8b7db476c5dd267e65eea9cab77584d3cfff9", "shasum": "" }, "require": { @@ -1500,14 +1547,14 @@ } ], "description": "Provides the functionality to export PHP variables for visualization", - "homepage": "http://www.github.com/sebastianbergmann/exporter", + "homepage": "https://www.github.com/sebastianbergmann/exporter", "keywords": [ "export", "exporter" ], "support": { "issues": "https://github.com/sebastianbergmann/exporter/issues", - "source": "https://github.com/sebastianbergmann/exporter/tree/4.0.3" + "source": "https://github.com/sebastianbergmann/exporter/tree/4.0.4" }, "funding": [ { @@ -1515,20 +1562,20 @@ "type": "github" } ], - "time": "2020-09-28T05:24:23+00:00" + "time": "2021-11-11T14:18:36+00:00" }, { "name": "sebastian/global-state", - "version": "5.0.3", + "version": "5.0.5", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/global-state.git", - "reference": "23bd5951f7ff26f12d4e3242864df3e08dec4e49" + "reference": "0ca8db5a5fc9c8646244e629625ac486fa286bf2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/23bd5951f7ff26f12d4e3242864df3e08dec4e49", - "reference": "23bd5951f7ff26f12d4e3242864df3e08dec4e49", + "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/0ca8db5a5fc9c8646244e629625ac486fa286bf2", + "reference": "0ca8db5a5fc9c8646244e629625ac486fa286bf2", "shasum": "" }, "require": { @@ -1571,7 +1618,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/global-state/issues", - "source": "https://github.com/sebastianbergmann/global-state/tree/5.0.3" + "source": "https://github.com/sebastianbergmann/global-state/tree/5.0.5" }, "funding": [ { @@ -1579,7 +1626,7 @@ "type": "github" } ], - "time": "2021-06-11T13:31:12+00:00" + "time": "2022-02-14T08:28:10+00:00" }, { "name": "sebastian/lines-of-code", @@ -1979,21 +2026,24 @@ }, { "name": "symfony/polyfill-ctype", - "version": "v1.23.0", + "version": "v1.24.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "46cd95797e9df938fdd2b03693b5fca5e64b01ce" + "reference": "30885182c981ab175d4d034db0f6f469898070ab" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/46cd95797e9df938fdd2b03693b5fca5e64b01ce", - "reference": "46cd95797e9df938fdd2b03693b5fca5e64b01ce", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/30885182c981ab175d4d034db0f6f469898070ab", + "reference": "30885182c981ab175d4d034db0f6f469898070ab", "shasum": "" }, "require": { "php": ">=7.1" }, + "provide": { + "ext-ctype": "*" + }, "suggest": { "ext-ctype": "For best performance" }, @@ -2038,7 +2088,7 @@ "portable" ], "support": { - "source": "https://github.com/symfony/polyfill-ctype/tree/v1.23.0" + "source": "https://github.com/symfony/polyfill-ctype/tree/v1.24.0" }, "funding": [ { @@ -2054,7 +2104,7 @@ "type": "tidelift" } ], - "time": "2021-02-19T12:13:01+00:00" + "time": "2021-10-20T20:35:02+00:00" }, { "name": "theseer/tokenizer", @@ -2171,8 +2221,8 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": ">=7.2" + "php": ">=8.0" }, "platform-dev": [], - "plugin-api-version": "2.0.0" + "plugin-api-version": "2.2.0" } diff --git a/src/FileHandler.php b/src/FileHandler.php index 838a404..4c3a087 100644 --- a/src/FileHandler.php +++ b/src/FileHandler.php @@ -13,7 +13,7 @@ class FileHandler extends Handler { * @param string $save_path The path where to store/retrieve the session. * @param string $name The session name. */ - public function open($save_path, $name):bool { + public function open(string $save_path, string $name):bool { $success = true; $save_path = str_replace( @@ -45,7 +45,7 @@ public function close():bool { * @link http://php.net/manual/en/sessionhandlerinterface.read.php * @param string $session_id */ - public function read($session_id):string { + public function read(string $session_id):string { if(isset($this->cache[$session_id])) { return $this->cache[$session_id]; } @@ -65,7 +65,7 @@ public function read($session_id):string { * @param string $session_id * @param string $session_data */ - public function write($session_id, $session_data):bool { + public function write(string $session_id, string $session_data):bool { if($session_data === self::EMPTY_PHP_ARRAY) { return true; } @@ -76,10 +76,10 @@ public function write($session_id, $session_data):bool { /** * @link http://php.net/manual/en/sessionhandlerinterface.destroy.php - * @param string $session_id + * @param string $id */ - public function destroy($session_id):bool { - $filePath = $this->getFilePath($session_id); + public function destroy($id):bool { + $filePath = $this->getFilePath($id); if(file_exists($filePath)) { return unlink($filePath); diff --git a/src/Handler.php b/src/Handler.php index ffbe95a..a3f7a11 100644 --- a/src/Handler.php +++ b/src/Handler.php @@ -12,33 +12,33 @@ abstract public function close():bool; /** * @link http://php.net/manual/en/sessionhandlerinterface.destroy.php - * @param string $session_id + * @param string $id */ - abstract public function destroy($session_id); + abstract public function destroy(string $id = ""):bool; /** * @link http://php.net/manual/en/sessionhandlerinterface.gc.php - * @param int $maxlifetime + * @param int $max_lifetime */ - abstract public function gc($maxlifetime):bool; + abstract public function gc(int $max_lifetime):int|false; /** * @link http://php.net/manual/en/sessionhandlerinterface.open.php * @param string $save_path The path where to store/retrieve the session. * @param string $name The session name. */ - abstract public function open($save_path, $name):bool; + abstract public function open(string $save_path, string $name):bool; /** * @link http://php.net/manual/en/sessionhandlerinterface.read.php * @param string $session_id */ - abstract public function read($session_id):string; + abstract public function read(string $session_id):string; /** * @link http://php.net/manual/en/sessionhandlerinterface.write.php * @param string $session_id * @param string $session_data */ - abstract public function write($session_id, $session_data):bool; -} \ No newline at end of file + abstract public function write(string $session_id, string $session_data):bool; +} diff --git a/src/Session.php b/src/Session.php index 1f9b3eb..8b850a6 100644 --- a/src/Session.php +++ b/src/Session.php @@ -1,9 +1,14 @@ $sessionPath, - "name" => $sessionName, - "serialize_handler" => "php_serialize", - "cookie_lifetime" => $config["cookie_lifetime"] ?? self::DEFAULT_SESSION_LIFETIME, - "cookie_path" => $config["cookie_path"] ?? self::DEFAULT_COOKIE_PATH, - "cookie_domain" => $config["cookie_domain"] ?? self::DEFAULT_SESSION_DOMAIN, - "cookie_secure" => $config["cookie_secure"] ?? self::DEFAULT_SESSION_SECURE, - "cookie_httponly" => $config["cookie_httponly"] ?? self::DEFAULT_SESSION_HTTPONLY, - ]); - - if(!$success) { -// TODO: Throw exception after #131 investigated. - var_dump($sessionPath, $sessionName, $this->id);die("Session starting failed"); + do { + $success = session_start([ + "save_path" => $sessionPath, + "name" => $sessionName, + "serialize_handler" => "php_serialize", + "cookie_lifetime" => $config["cookie_lifetime"] ?? self::DEFAULT_SESSION_LIFETIME, + "cookie_path" => $config["cookie_path"] ?? self::DEFAULT_COOKIE_PATH, + "cookie_domain" => $config["cookie_domain"] ?? self::DEFAULT_SESSION_DOMAIN, + "cookie_secure" => $config["cookie_secure"] ?? self::DEFAULT_SESSION_SECURE, + "cookie_httponly" => $config["cookie_httponly"] ?? self::DEFAULT_SESSION_HTTPONLY, + ]); + + if(!$success) { + session_destroy(); + } } + while(!$success); $this->sessionHandler->open($sessionPath, $sessionName); $this->store = $this->readSessionData() ?: null; @@ -84,10 +91,9 @@ public function getStore( ); } - public function get(string $key) { + public function get(string $key):mixed { return $this->store->get($key); } - public function set(string $key, $value):void { $this->store->set($key, $value); } diff --git a/src/SessionStore.php b/src/SessionStore.php index b40ce8b..a21c4d3 100644 --- a/src/SessionStore.php +++ b/src/SessionStore.php @@ -1,7 +1,14 @@ data = []; } - public function setData(string $key, $value):void { + public function setData(string $key, mixed $value):void { $this->data[$key] = $value; } @@ -117,7 +124,7 @@ public function createStore(string $namespace):SessionStore { return $this->getStore($namespace); } - public function get(string $key) { + public function get(string $key):mixed { $store = $this; $lastDotPosition = strrpos($key, "."); @@ -220,4 +227,4 @@ protected function getNamespaceFromKey(string $key):?string { return substr($key, 0, $lastDotPostition); } -} \ No newline at end of file +} diff --git a/test/phpunit/Helper/FunctionOverride/session_start.php b/test/phpunit/Helper/FunctionOverride/session_start.php index 0765a79..1ac8361 100644 --- a/test/phpunit/Helper/FunctionOverride/session_start.php +++ b/test/phpunit/Helper/FunctionOverride/session_start.php @@ -6,9 +6,9 @@ function session_start() { FunctionMocker::$mockCalls["session_start"] []= func_get_args(); - if(FunctionMocker::$callState["session_start__fail"]) { + if(isset(FunctionMocker::$callState["session_start__fail"])) { return false; } return true; -} \ No newline at end of file +} diff --git a/test/phpunit/SessionStoreTest.php b/test/phpunit/SessionStoreTest.php index 6ee3aa5..cb1d74e 100644 --- a/test/phpunit/SessionStoreTest.php +++ b/test/phpunit/SessionStoreTest.php @@ -1,6 +1,7 @@ createMock(Session::class); + $sut = new SessionStore("test", $session); + + $numericValue = rand(1000, 9999); + $sut->set("test.value", $numericValue); + + self::assertSame((string)$numericValue, $sut->getString("test.value")); + } + + public function testGetInt():void { + $session = $this->createMock(Session::class); + $sut = new SessionStore("test", $session); + + $numericStringValue = (string)rand(1000, 9999); + $sut->set("test.value", $numericStringValue); + + self::assertSame((int)$numericStringValue, $sut->getInt("test.value")); + } + + public function testGetFloat():void { + $session = $this->createMock(Session::class); + $sut = new SessionStore("test", $session); + + $numericStringValue = (string)(rand(1000, 9999) - 0.105); + $sut->set("test.value", $numericStringValue); + + self::assertSame((float)$numericStringValue, $sut->getFloat("test.value")); + } + + public function testGetBool():void { + $session = $this->createMock(Session::class); + $sut = new SessionStore("test", $session); + + $numericValue = rand(0, 1); + $sut->set("test.value", $numericValue); + + self::assertSame((bool)$numericValue, $sut->getBool("test.value")); + } + + public function testGetDateTime():void { + $session = $this->createMock(Session::class); + $sut = new SessionStore("test", $session); + + $numericValue = time(); + $sut->set("test.value", $numericValue); + + $dateTime = new DateTime(); + $dateTime->setTimestamp($numericValue); + self::assertEquals($dateTime, $sut->getDateTime("test.value")); + } +} diff --git a/test/phpunit/SessionTest.php b/test/phpunit/SessionTest.php index f9a19ed..eee23f4 100644 --- a/test/phpunit/SessionTest.php +++ b/test/phpunit/SessionTest.php @@ -1,6 +1,7 @@ getMockBuilder(Handler::class) + ->getMock(); + $sut = new Session($handler); + + $numericValue = rand(1000, 9999); + $sut->set("test.value", $numericValue); + + self::assertSame((string)$numericValue, $sut->getString("test.value")); + } + + public function testGetInt():void { + /** @var Handler|MockObject $handler */ + $handler = $this->getMockBuilder(Handler::class) + ->getMock(); + $sut = new Session($handler); + + $numericStringValue = (string)rand(1000, 9999); + $sut->set("test.value", $numericStringValue); + + self::assertSame((int)$numericStringValue, $sut->getInt("test.value")); + } + + public function testGetFloat():void { + /** @var Handler|MockObject $handler */ + $handler = $this->getMockBuilder(Handler::class) + ->getMock(); + $sut = new Session($handler); + + $numericStringValue = (string)(rand(1000, 9999) - 0.105); + $sut->set("test.value", $numericStringValue); + + self::assertSame((float)$numericStringValue, $sut->getFloat("test.value")); + } + + public function testGetBool():void { + /** @var Handler|MockObject $handler */ + $handler = $this->getMockBuilder(Handler::class) + ->getMock(); + $sut = new Session($handler); + + $numericValue = rand(0, 1); + $sut->set("test.value", $numericValue); + + self::assertSame((bool)$numericValue, $sut->getBool("test.value")); + } + + public function testGetDateTime():void { + /** @var Handler|MockObject $handler */ + $handler = $this->getMockBuilder(Handler::class) + ->getMock(); + $sut = new Session($handler); + + $numericValue = time(); + $sut->set("test.value", $numericValue); + + $dateTime = new DateTime(); + $dateTime->setTimestamp($numericValue); + self::assertEquals($dateTime, $sut->getDateTime("test.value")); + } +} From 04e4bf5275675c5aa7937a675ccc2c5560126e6b Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Wed, 2 Mar 2022 13:56:11 +0000 Subject: [PATCH 2/3] fix: remove deprecation notices, improve type safety closes #153 --- src/FileHandler.php | 13 ++++++++----- src/Session.php | 12 +++++++----- src/SessionArrayWrapper.php | 10 ++++++---- src/SessionContainer.php | 6 +++--- src/SessionStore.php | 24 +++++++++--------------- 5 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/FileHandler.php b/src/FileHandler.php index 4c3a087..76d2fd7 100644 --- a/src/FileHandler.php +++ b/src/FileHandler.php @@ -5,8 +5,9 @@ class FileHandler extends Handler { const EMPTY_PHP_ARRAY = "a:0:{}"; - protected $path; - protected $cache; + protected string $path; + /** @var array> */ + protected array $cache; /** * @link http://php.net/manual/en/sessionhandlerinterface.open.php @@ -78,7 +79,7 @@ public function write(string $session_id, string $session_data):bool { * @link http://php.net/manual/en/sessionhandlerinterface.destroy.php * @param string $id */ - public function destroy($id):bool { + public function destroy(string $id = ""):bool { $filePath = $this->getFilePath($id); if(file_exists($filePath)) { @@ -92,9 +93,10 @@ public function destroy($id):bool { * @link http://php.net/manual/en/sessionhandlerinterface.gc.php * @param int $maxlifetime */ - public function gc($maxlifetime):bool { + public function gc($maxlifetime):int|false { $now = time(); $expired = $now - $maxlifetime; + $num = 0; foreach(new DirectoryIterator($this->path) as $fileInfo) { if(!$fileInfo->isFile()) { @@ -106,10 +108,11 @@ public function gc($maxlifetime):bool { if(!unlink($fileInfo->getPathname())) { return false; } + $num++; } } - return true; + return $num; } protected function getFilePath(string $id):string { diff --git a/src/Session.php b/src/Session.php index 8b850a6..cb7ff71 100644 --- a/src/Session.php +++ b/src/Session.php @@ -24,9 +24,10 @@ class Session implements SessionContainer, TypeSafeGetter { /** @var SessionStore */ protected $store; + /** @param array $config */ public function __construct( SessionHandlerInterface $sessionHandler, - iterable $config = [], + array $config = [], string $id = null ) { $this->sessionHandler = $sessionHandler; @@ -61,7 +62,7 @@ public function __construct( while(!$success); $this->sessionHandler->open($sessionPath, $sessionName); - $this->store = $this->readSessionData() ?: null; + $this->store = $this->readSessionData(); if(is_null($this->store)) { $this->store = new SessionStore(__NAMESPACE__, $this); } @@ -94,7 +95,8 @@ public function getStore( public function get(string $key):mixed { return $this->store->get($key); } - public function set(string $key, $value):void { + + public function set(string $key, mixed $value):void { $this->store->set($key, $value); } @@ -136,8 +138,8 @@ protected function createNewId():string { return session_create_id(); } - protected function readSessionData() { - return unserialize($this->sessionHandler->read($this->id)); + protected function readSessionData():?SessionStore { + return unserialize($this->sessionHandler->read($this->id)) ?: null; } public function write():bool { diff --git a/src/SessionArrayWrapper.php b/src/SessionArrayWrapper.php index 7d808fc..a484668 100644 --- a/src/SessionArrayWrapper.php +++ b/src/SessionArrayWrapper.php @@ -2,17 +2,19 @@ namespace Gt\Session; class SessionArrayWrapper implements SessionContainer { - private $sourceArray; + /** @var array */ + private array $sourceArray; + /** @param array &$sourceArray */ public function __construct(array &$sourceArray) { $this->sourceArray = &$sourceArray; } - public function get(string $key) { + public function get(string $key):mixed { return $this->sourceArray[$key] ?? null; } - public function set(string $key, $value) { + public function set(string $key, mixed $value):void { $this->sourceArray[$key] = $value; } @@ -27,4 +29,4 @@ public function remove(string $key):void { unset($this->sourceArray[$key]); } -} \ No newline at end of file +} diff --git a/src/SessionContainer.php b/src/SessionContainer.php index 6b4e385..aab32f8 100644 --- a/src/SessionContainer.php +++ b/src/SessionContainer.php @@ -2,8 +2,8 @@ namespace Gt\Session; interface SessionContainer { - public function get(string $key); - public function set(string $key, $value); + public function get(string $key):mixed; + public function set(string $key, mixed $value):void; public function contains(string $key):bool; public function remove(string $key):void; -} \ No newline at end of file +} diff --git a/src/SessionStore.php b/src/SessionStore.php index a21c4d3..f9bc930 100644 --- a/src/SessionStore.php +++ b/src/SessionStore.php @@ -1,24 +1,19 @@ */ + protected array $stores; + /** @var array */ + protected array $data; + protected SessionStore $parentStore; public function __construct( string $name, @@ -36,7 +31,7 @@ public function setData(string $key, mixed $value):void { $this->data[$key] = $value; } - public function getData(string $key) { + public function getData(string $key):mixed { return $this->data[$key] ?? null; } @@ -76,7 +71,6 @@ public function getStore( $namespaceParts = explode(".", $namespace); $topLevelStoreName = array_shift($namespaceParts); - /** @var SessionStore $store */ $store = $this->stores[$topLevelStoreName] ?? null; if(is_null($store)) { if($createIfNotExists) { @@ -144,7 +138,7 @@ public function get(string $key):mixed { return $store->getData($key); } - public function set(string $key, $value):void { + public function set(string $key, mixed $value):void { $store = $this; $lastDotPosition = strrpos($key, "."); From 26cef1abafa092b2a67b1937aa859c5a1d272d1d Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Wed, 2 Mar 2022 14:17:55 +0000 Subject: [PATCH 3/3] fix: extra type safety changes --- .github/workflows/ci.yml | 7 +++--- src/FileHandler.php | 27 ++++++---------------- src/Handler.php | 35 +++++++---------------------- src/Session.php | 16 +++++--------- src/SessionSetup.php | 2 +- src/SessionStore.php | 48 ++++++++++++++++++---------------------- 6 files changed, 48 insertions(+), 87 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cb9d96f..7d75366 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,7 +15,7 @@ jobs: path: /tmp/composer-cache key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }} - - uses: php-actions/composer@v5 + - uses: php-actions/composer@v6 - name: Archive build run: mkdir /tmp/github-actions/ && tar -cvf /tmp/github-actions/build.tar ./ @@ -40,7 +40,7 @@ jobs: run: tar -xvf /tmp/github-actions/build.tar ./ - name: PHP Unit tests - uses: php-actions/phpunit@v2 + uses: php-actions/phpunit@v3 with: php_version: 8.0 php_extensions: xdebug @@ -61,6 +61,7 @@ jobs: run: tar -xvf /tmp/github-actions/build.tar ./ - name: PHP Static Analysis - uses: php-actions/phpstan@v2 + uses: php-actions/phpstan@v3 with: path: src/ + level: 7 diff --git a/src/FileHandler.php b/src/FileHandler.php index 76d2fd7..0e602a7 100644 --- a/src/FileHandler.php +++ b/src/FileHandler.php @@ -42,10 +42,7 @@ public function close():bool { return true; } - /** - * @link http://php.net/manual/en/sessionhandlerinterface.read.php - * @param string $session_id - */ + /** @link http://php.net/manual/en/sessionhandlerinterface.read.php */ public function read(string $session_id):string { if(isset($this->cache[$session_id])) { return $this->cache[$session_id]; @@ -57,15 +54,11 @@ public function read(string $session_id):string { return ""; } - $this->cache[$session_id] = file_get_contents($filePath); + $this->cache[$session_id] = file_get_contents($filePath) ?: ""; return $this->cache[$session_id]; } - /** - * @link http://php.net/manual/en/sessionhandlerinterface.write.php - * @param string $session_id - * @param string $session_data - */ + /** @link http://php.net/manual/en/sessionhandlerinterface.write.php */ public function write(string $session_id, string $session_data):bool { if($session_data === self::EMPTY_PHP_ARRAY) { return true; @@ -75,10 +68,7 @@ public function write(string $session_id, string $session_data):bool { return $bytesWritten !== false; } - /** - * @link http://php.net/manual/en/sessionhandlerinterface.destroy.php - * @param string $id - */ + /** @link http://php.net/manual/en/sessionhandlerinterface.destroy.php */ public function destroy(string $id = ""):bool { $filePath = $this->getFilePath($id); @@ -89,13 +79,10 @@ public function destroy(string $id = ""):bool { return true; } - /** - * @link http://php.net/manual/en/sessionhandlerinterface.gc.php - * @param int $maxlifetime - */ - public function gc($maxlifetime):int|false { + /** @link http://php.net/manual/en/sessionhandlerinterface.gc.php */ + public function gc(int $maxLifeTime):int|false { $now = time(); - $expired = $now - $maxlifetime; + $expired = $now - $maxLifeTime; $num = 0; foreach(new DirectoryIterator($this->path) as $fileInfo) { diff --git a/src/Handler.php b/src/Handler.php index a3f7a11..39891af 100644 --- a/src/Handler.php +++ b/src/Handler.php @@ -5,40 +5,21 @@ abstract class Handler implements SessionHandlerInterface { - /** - * @link http://php.net/manual/en/sessionhandlerinterface.close.php - */ + /** @link http://php.net/manual/en/sessionhandlerinterface.close.php */ abstract public function close():bool; - /** - * @link http://php.net/manual/en/sessionhandlerinterface.destroy.php - * @param string $id - */ + /** @link http://php.net/manual/en/sessionhandlerinterface.destroy.php */ abstract public function destroy(string $id = ""):bool; - /** - * @link http://php.net/manual/en/sessionhandlerinterface.gc.php - * @param int $max_lifetime - */ - abstract public function gc(int $max_lifetime):int|false; - - /** - * @link http://php.net/manual/en/sessionhandlerinterface.open.php - * @param string $save_path The path where to store/retrieve the session. - * @param string $name The session name. - */ + /** @link http://php.net/manual/en/sessionhandlerinterface.gc.php */ + abstract public function gc(int $maxLifeTime):int|false; + + /** @link http://php.net/manual/en/sessionhandlerinterface.open.php */ abstract public function open(string $save_path, string $name):bool; - /** - * @link http://php.net/manual/en/sessionhandlerinterface.read.php - * @param string $session_id - */ + /** @link http://php.net/manual/en/sessionhandlerinterface.read.php */ abstract public function read(string $session_id):string; - /** - * @link http://php.net/manual/en/sessionhandlerinterface.write.php - * @param string $session_id - * @param string $session_data - */ + /** @link http://php.net/manual/en/sessionhandlerinterface.write.php */ abstract public function write(string $session_id, string $session_data):bool; } diff --git a/src/Session.php b/src/Session.php index cb7ff71..3abd1ea 100644 --- a/src/Session.php +++ b/src/Session.php @@ -1,7 +1,6 @@ $config */ public function __construct( @@ -72,7 +68,7 @@ public function kill():void { $this->sessionHandler->destroy($this->getId()); $params = session_get_cookie_params(); setcookie( - session_name(), + session_name() ?: "", "", -1, $params["path"], @@ -114,7 +110,7 @@ public function getId():string { session_id($this->createNewId()); } - return session_id(); + return session_id() ?: ""; } protected function getAbsolutePath(string $path):string { @@ -135,7 +131,7 @@ protected function getAbsolutePath(string $path):string { } protected function createNewId():string { - return session_create_id(); + return session_create_id() ?: ""; } protected function readSessionData():?SessionStore { diff --git a/src/SessionSetup.php b/src/SessionSetup.php index 065dbd5..4228390 100644 --- a/src/SessionSetup.php +++ b/src/SessionSetup.php @@ -1,4 +1,4 @@ - */ protected array $data; - protected SessionStore $parentStore; + protected ?SessionStore $parentStore; public function __construct( string $name, @@ -74,8 +74,7 @@ public function getStore( $store = $this->stores[$topLevelStoreName] ?? null; if(is_null($store)) { if($createIfNotExists) { - $store = $this->createStore($namespace); - return $store; + return $this->createStore($namespace); } else { return null; @@ -119,23 +118,9 @@ public function createStore(string $namespace):SessionStore { } public function get(string $key):mixed { - $store = $this; - $lastDotPosition = strrpos($key, "."); - - if ($lastDotPosition !== false) { - $namespace = $this->getNamespaceFromKey($key); - $store = $this->getStore($namespace); - } - - if (is_null($store)) { - return null; - } - - if ($lastDotPosition !== false) { - $key = substr($key, $lastDotPosition + 1); - } - - return $store->getData($key); + $store = $this->getStoreFromKey($key); + $key = $this->normaliseKey($key); + return $store?->getData($key); } public function set(string $key, mixed $value):void { @@ -160,6 +145,12 @@ public function set(string $key, mixed $value):void { } public function contains(string $key):bool { + $store = $this->getStoreFromKey($key); + $key = $this->normaliseKey($key); + return $store?->containsData($key) ?? false; + } + + private function getStoreFromKey(string $key):?SessionStore { $store = $this; $lastDotPosition = strrpos($key, "."); @@ -169,14 +160,19 @@ public function contains(string $key):bool { } if (is_null($store)) { - return false; + return null; } - if ($lastDotPosition !== false) { + return $store; + } + + private function normaliseKey(string $key):string { + $lastDotPosition = strrpos($key, "."); + if($lastDotPosition !== false) { $key = substr($key, $lastDotPosition + 1); } - return $store->containsData($key); + return $key; } public function remove(string $key = null):void { @@ -214,11 +210,11 @@ protected function getSession():Session { } protected function getNamespaceFromKey(string $key):?string { - $lastDotPostition = strrpos($key, "."); - if ($lastDotPostition === false) { + $lastDotPosition = strrpos($key, "."); + if ($lastDotPosition === false) { return null; } - return substr($key, 0, $lastDotPostition); + return substr($key, 0, $lastDotPosition); } }