From cbb02cf70d892aab0a2327a0410b99c662dc1164 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=A1=D0=BF=D0=B8?= =?UTF-8?q?=D1=80=D0=BA=D0=BE=D0=B2?= Date: Tue, 16 Sep 2025 12:01:22 +0400 Subject: [PATCH 1/3] Fix notice "Object of class DateTimeImmutable could not be converted to int" in `CookieCollection::has` --- framework/web/CookieCollection.php | 34 ++-- tests/framework/web/CookieCollectionTest.php | 189 +++++++++++++++++++ 2 files changed, 210 insertions(+), 13 deletions(-) create mode 100644 tests/framework/web/CookieCollectionTest.php diff --git a/framework/web/CookieCollection.php b/framework/web/CookieCollection.php index 8b7db87c5ec..bb703d805ef 100644 --- a/framework/web/CookieCollection.php +++ b/framework/web/CookieCollection.php @@ -116,19 +116,27 @@ public function getValue($name, $defaultValue = null) */ public function has($name) { - return isset($this->_cookies[$name]) && $this->_cookies[$name]->value !== '' - && ($this->_cookies[$name]->expire === null - || $this->_cookies[$name]->expire === 0 - || ( - (is_string($this->_cookies[$name]->expire) && strtotime($this->_cookies[$name]->expire) >= time()) - || ( - interface_exists('\\DateTimeInterface') - && $this->_cookies[$name]->expire instanceof \DateTimeInterface - && $this->_cookies[$name]->expire->getTimestamp() >= time() - ) - || $this->_cookies[$name]->expire >= time() - ) - ); + if (!isset($this->_cookies[$name]) || $this->_cookies[$name]->value === '') { + return false; + } + + $expire = $this->_cookies[$name]->expire; + + if ($expire === null || $expire === 0) { + return true; + } + + $currentTime = time(); + + if (is_numeric($expire)) { + return (int) $expire >= $currentTime; + } + + if (is_string($expire)){ + return strtotime($expire) >= $currentTime; + } + + return $expire->getTimestamp() >= $currentTime; } /** diff --git a/tests/framework/web/CookieCollectionTest.php b/tests/framework/web/CookieCollectionTest.php new file mode 100644 index 00000000000..272d98a220e --- /dev/null +++ b/tests/framework/web/CookieCollectionTest.php @@ -0,0 +1,189 @@ +has($name); + + $this->assertEquals($expectedResult, $result); + } + + public static function provideHasData(): array + { + $pastTime = time() - 100; + $futureTime = time() + 100; + + return [ + [ + 'test', + [], + false, + ], + [ + 'test', + [ + 'abc' => new Cookie([ + 'value' => 'abc', + ]), + ], + false, + ], + [ + 'test', + [ + 'test' => new Cookie(), + ], + false, + ], + [ + 'test', + [ + 'test' => new Cookie([ + 'value' => 'test', + 'expire' => null, + ]), + ], + true, + ], + [ + 'test', + [ + 'test' => new Cookie([ + 'value' => 'test', + ]), + ], + true, + ], + [ + 'test', + [ + 'test' => new Cookie([ + 'value' => 'test', + ]), + ], + true, + ], + [ + 'test', + [ + 'test' => new Cookie([ + 'value' => 'test', + 'expire' => (string) $futureTime, + ]), + ], + true, + ], + [ + 'test', + [ + 'test' => new Cookie([ + 'value' => 'test', + 'expire' => (string) $pastTime, + ]), + ], + false, + ], + [ + 'test', + [ + 'test' => new Cookie([ + 'value' => 'test', + 'expire' => $futureTime, + ]), + ], + true, + ], + [ + 'test', + [ + 'test' => new Cookie([ + 'value' => 'test', + 'expire' => $pastTime, + ]), + ], + false, + ], + [ + 'test', + [ + 'test' => new Cookie([ + 'value' => 'test', + 'expire' => date('Y-m-d H:i:s', $futureTime), + ]), + ], + true, + ], + [ + 'test', + [ + 'test' => new Cookie([ + 'value' => 'test', + 'expire' => date('Y-m-d', $pastTime), + ]), + ], + false, + ], + [ + 'test', + [ + 'test' => new Cookie([ + 'value' => 'test', + 'expire' => (new DateTimeImmutable())->setTimestamp($futureTime), + ]), + ], + true, + ], + [ + 'test', + [ + 'test' => new Cookie([ + 'value' => 'test', + 'expire' => (new DateTimeImmutable())->setTimestamp($pastTime), + ]), + ], + false, + ], + [ + 'test', + [ + 'test' => new Cookie([ + 'value' => 'test', + 'expire' => (new DateTime())->setTimestamp($futureTime), + ]), + ], + true, + ], + [ + 'test', + [ + 'test' => new Cookie([ + 'value' => 'test', + 'expire' => (new DateTime())->setTimestamp($pastTime), + ]), + ], + false, + ], + ]; + } +} From 3156b310a612feb5d5e5e2690d7bd28896db6926 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=A1=D0=BF=D0=B8?= =?UTF-8?q?=D1=80=D0=BA=D0=BE=D0=B2?= Date: Tue, 16 Sep 2025 12:09:01 +0400 Subject: [PATCH 2/3] changelog --- framework/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index fea8357ca98..77aa51a82f8 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -31,6 +31,7 @@ Yii Framework 2 Change Log - Bug #19506: Fix `@property` annotations in `yii\console\widgets\Table`, `yii\di\Container` and `yii\web\Session` (max-s-lab) - Enh #20525: Add `@template` annotations for all actions (max-s-lab) - Bug #20524: Fix PHPStan/Psalm annotations in `Yii::createObject` (max-s-lab) +- Bug #20530: Fix notice "Object of class DateTimeImmutable could not be converted to int" in `CookieCollection::has` (max-s-lab) 2.0.53 June 27, 2025 From d10cecf7be0e521a0c49b987a09121a35a3bc912 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=A1=D0=BF=D0=B8?= =?UTF-8?q?=D1=80=D0=BA=D0=BE=D0=B2?= Date: Tue, 16 Sep 2025 12:09:28 +0400 Subject: [PATCH 3/3] fix codestyle --- framework/web/CookieCollection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/web/CookieCollection.php b/framework/web/CookieCollection.php index bb703d805ef..c476c694c85 100644 --- a/framework/web/CookieCollection.php +++ b/framework/web/CookieCollection.php @@ -132,7 +132,7 @@ public function has($name) return (int) $expire >= $currentTime; } - if (is_string($expire)){ + if (is_string($expire)) { return strtotime($expire) >= $currentTime; }