From d14e7a27e800f8e10f7616229c24ef235b5d7158 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Mon, 21 Feb 2022 12:55:28 +0000 Subject: [PATCH 1/2] 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 9ae14755251f742ee6424691870a218ff235f79d Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Tue, 8 Mar 2022 15:35:59 +0000 Subject: [PATCH 2/2] loosen type (can be array access) --- composer.lock | 95 ++++++++++++++++++++++++++----------------------- src/Session.php | 4 +-- 2 files changed, 52 insertions(+), 47 deletions(-) diff --git a/composer.lock b/composer.lock index 1b8982d..77a3edd 100644 --- a/composer.lock +++ b/composer.lock @@ -59,29 +59,30 @@ "packages-dev": [ { "name": "doctrine/instantiator", - "version": "1.4.0", + "version": "1.4.1", "source": { "type": "git", "url": "https://github.com/doctrine/instantiator.git", - "reference": "d56bf6102915de5702778fe20f2de3b2fe570b5b" + "reference": "10dcfce151b967d20fde1b34ae6640712c3891bc" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/doctrine/instantiator/zipball/d56bf6102915de5702778fe20f2de3b2fe570b5b", - "reference": "d56bf6102915de5702778fe20f2de3b2fe570b5b", + "url": "https://api.github.com/repos/doctrine/instantiator/zipball/10dcfce151b967d20fde1b34ae6640712c3891bc", + "reference": "10dcfce151b967d20fde1b34ae6640712c3891bc", "shasum": "" }, "require": { "php": "^7.1 || ^8.0" }, "require-dev": { - "doctrine/coding-standard": "^8.0", + "doctrine/coding-standard": "^9", "ext-pdo": "*", "ext-phar": "*", - "phpbench/phpbench": "^0.13 || 1.0.0-alpha2", - "phpstan/phpstan": "^0.12", - "phpstan/phpstan-phpunit": "^0.12", - "phpunit/phpunit": "^7.0 || ^8.0 || ^9.0" + "phpbench/phpbench": "^0.16 || ^1", + "phpstan/phpstan": "^1.4", + "phpstan/phpstan-phpunit": "^1", + "phpunit/phpunit": "^7.5 || ^8.5 || ^9.5", + "vimeo/psalm": "^4.22" }, "type": "library", "autoload": { @@ -108,7 +109,7 @@ ], "support": { "issues": "https://github.com/doctrine/instantiator/issues", - "source": "https://github.com/doctrine/instantiator/tree/1.4.0" + "source": "https://github.com/doctrine/instantiator/tree/1.4.1" }, "funding": [ { @@ -124,29 +125,33 @@ "type": "tidelift" } ], - "time": "2020-11-10T18:47:58+00:00" + "time": "2022-03-03T08:28:38+00:00" }, { "name": "myclabs/deep-copy", - "version": "1.10.2", + "version": "1.11.0", "source": { "type": "git", "url": "https://github.com/myclabs/DeepCopy.git", - "reference": "776f831124e9c62e1a2c601ecc52e776d8bb7220" + "reference": "14daed4296fae74d9e3201d2c4925d1acb7aa614" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/776f831124e9c62e1a2c601ecc52e776d8bb7220", - "reference": "776f831124e9c62e1a2c601ecc52e776d8bb7220", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/14daed4296fae74d9e3201d2c4925d1acb7aa614", + "reference": "14daed4296fae74d9e3201d2c4925d1acb7aa614", "shasum": "" }, "require": { "php": "^7.1 || ^8.0" }, + "conflict": { + "doctrine/collections": "<1.6.8", + "doctrine/common": "<2.13.3 || >=3,<3.2.2" + }, "require-dev": { - "doctrine/collections": "^1.0", - "doctrine/common": "^2.6", - "phpunit/phpunit": "^7.1" + "doctrine/collections": "^1.6.8", + "doctrine/common": "^2.13.3 || ^3.2.2", + "phpunit/phpunit": "^7.5.20 || ^8.5.23 || ^9.5.13" }, "type": "library", "autoload": { @@ -171,7 +176,7 @@ ], "support": { "issues": "https://github.com/myclabs/DeepCopy/issues", - "source": "https://github.com/myclabs/DeepCopy/tree/1.10.2" + "source": "https://github.com/myclabs/DeepCopy/tree/1.11.0" }, "funding": [ { @@ -179,7 +184,7 @@ "type": "tidelift" } ], - "time": "2020-11-13T09:40:50+00:00" + "time": "2022-03-03T13:19:32+00:00" }, { "name": "nikic/php-parser", @@ -577,16 +582,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.4.6", + "version": "1.4.8", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "8a7761f1c520e0dad6e04d862fdc697445457cfe" + "reference": "2a6d6704b17c4db6190cc3104056c0aad740cb15" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/8a7761f1c520e0dad6e04d862fdc697445457cfe", - "reference": "8a7761f1c520e0dad6e04d862fdc697445457cfe", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/2a6d6704b17c4db6190cc3104056c0aad740cb15", + "reference": "2a6d6704b17c4db6190cc3104056c0aad740cb15", "shasum": "" }, "require": { @@ -617,7 +622,7 @@ "description": "PHPStan - PHP Static Analysis Tool", "support": { "issues": "https://github.com/phpstan/phpstan/issues", - "source": "https://github.com/phpstan/phpstan/tree/1.4.6" + "source": "https://github.com/phpstan/phpstan/tree/1.4.8" }, "funding": [ { @@ -637,20 +642,20 @@ "type": "tidelift" } ], - "time": "2022-02-06T12:56:13+00:00" + "time": "2022-03-04T13:03:56+00:00" }, { "name": "phpunit/php-code-coverage", - "version": "9.2.11", + "version": "9.2.15", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "665a1ac0a763c51afc30d6d130dac0813092b17f" + "reference": "2e9da11878c4202f97915c1cb4bb1ca318a63f5f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/665a1ac0a763c51afc30d6d130dac0813092b17f", - "reference": "665a1ac0a763c51afc30d6d130dac0813092b17f", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/2e9da11878c4202f97915c1cb4bb1ca318a63f5f", + "reference": "2e9da11878c4202f97915c1cb4bb1ca318a63f5f", "shasum": "" }, "require": { @@ -706,7 +711,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.11" + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.15" }, "funding": [ { @@ -714,7 +719,7 @@ "type": "github" } ], - "time": "2022-02-18T12:46:09+00:00" + "time": "2022-03-07T09:28:20+00:00" }, { "name": "phpunit/php-file-iterator", @@ -959,16 +964,16 @@ }, { "name": "phpunit/phpunit", - "version": "9.5.14", + "version": "9.5.18", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "1883687169c017d6ae37c58883ca3994cfc34189" + "reference": "1b5856028273bfd855e60a887278857d872ec67a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/1883687169c017d6ae37c58883ca3994cfc34189", - "reference": "1883687169c017d6ae37c58883ca3994cfc34189", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/1b5856028273bfd855e60a887278857d872ec67a", + "reference": "1b5856028273bfd855e60a887278857d872ec67a", "shasum": "" }, "require": { @@ -984,7 +989,7 @@ "phar-io/version": "^3.0.2", "php": ">=7.3", "phpspec/prophecy": "^1.12.1", - "phpunit/php-code-coverage": "^9.2.7", + "phpunit/php-code-coverage": "^9.2.13", "phpunit/php-file-iterator": "^3.0.5", "phpunit/php-invoker": "^3.1.1", "phpunit/php-text-template": "^2.0.3", @@ -1046,7 +1051,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.5.14" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.5.18" }, "funding": [ { @@ -1058,7 +1063,7 @@ "type": "github" } ], - "time": "2022-02-18T12:54:07+00:00" + "time": "2022-03-08T06:52:28+00:00" }, { "name": "sebastian/cli-parser", @@ -2026,7 +2031,7 @@ }, { "name": "symfony/polyfill-ctype", - "version": "v1.24.0", + "version": "v1.25.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", @@ -2058,12 +2063,12 @@ } }, "autoload": { - "psr-4": { - "Symfony\\Polyfill\\Ctype\\": "" - }, "files": [ "bootstrap.php" - ] + ], + "psr-4": { + "Symfony\\Polyfill\\Ctype\\": "" + } }, "notification-url": "https://packagist.org/downloads/", "license": [ @@ -2088,7 +2093,7 @@ "portable" ], "support": { - "source": "https://github.com/symfony/polyfill-ctype/tree/v1.24.0" + "source": "https://github.com/symfony/polyfill-ctype/tree/v1.25.0" }, "funding": [ { diff --git a/src/Session.php b/src/Session.php index 3abd1ea..7c68915 100644 --- a/src/Session.php +++ b/src/Session.php @@ -20,10 +20,10 @@ class Session implements SessionContainer, TypeSafeGetter { protected SessionHandlerInterface $sessionHandler; protected ?SessionStore $store; - /** @param array $config */ + /** @param \ArrayAccess|array $config */ public function __construct( SessionHandlerInterface $sessionHandler, - array $config = [], + array|\ArrayAccess $config = [], string $id = null ) { $this->sessionHandler = $sessionHandler;