From 02a7497714db64bc235435ecd7213f8d41a810ac Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 17 Jul 2021 08:47:41 -0700 Subject: [PATCH 1/4] Be more strict with types Make sure getMaxAge() always returns null or int. Also trigger deprecations if wrong types are passed to SetCookie --- composer.json | 3 ++- src/Cookie/SetCookie.php | 46 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 5f3a49e17..f92c6739f 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,8 @@ "ext-json": "*", "guzzlehttp/promises": "^1.4", "guzzlehttp/psr7": "^1.7 || ^2.0", - "psr/http-client": "^1.0" + "psr/http-client": "^1.0", + "symfony/deprecation-contracts": "^2.4" }, "provide": { "psr/http-client-implementation": "1.0" diff --git a/src/Cookie/SetCookie.php b/src/Cookie/SetCookie.php index 602370d6f..6b7ff68e7 100644 --- a/src/Cookie/SetCookie.php +++ b/src/Cookie/SetCookie.php @@ -128,6 +128,10 @@ public function getName() */ public function setName($name): void { + if (!is_string($name)) { + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); + } + $this->data['Name'] = $name; } @@ -148,6 +152,10 @@ public function getValue() */ public function setValue($value): void { + if (!is_string($value)) { + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); + } + $this->data['Value'] = $value; } @@ -168,6 +176,10 @@ public function getDomain() */ public function setDomain($domain): void { + if (!is_string($domain)) { + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); + } + $this->data['Domain'] = $domain; } @@ -188,6 +200,10 @@ public function getPath() */ public function setPath($path): void { + if (!is_string($path)) { + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); + } + $this->data['Path'] = $path; } @@ -198,7 +214,7 @@ public function setPath($path): void */ public function getMaxAge() { - return $this->data['Max-Age']; + return null === $this->data['Max-Age'] ? null : (int) $this->data['Max-Age']; } /** @@ -208,6 +224,10 @@ public function getMaxAge() */ public function setMaxAge($maxAge): void { + if (!is_int($maxAge)) { + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing an int to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); + } + $this->data['Max-Age'] = $maxAge; } @@ -228,6 +248,10 @@ public function getExpires() */ public function setExpires($timestamp): void { + if (!is_int($timestamp) && !is_string($timestamp)) { + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing an int or string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); + } + $this->data['Expires'] = \is_numeric($timestamp) ? (int) $timestamp : \strtotime($timestamp); @@ -250,6 +274,10 @@ public function getSecure() */ public function setSecure($secure): void { + if (!is_bool($secure)) { + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a boolean to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); + } + $this->data['Secure'] = $secure; } @@ -270,6 +298,10 @@ public function getDiscard() */ public function setDiscard($discard): void { + if (!is_bool($discard)) { + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a boolean to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); + } + $this->data['Discard'] = $discard; } @@ -290,6 +322,10 @@ public function getHttpOnly() */ public function setHttpOnly($httpOnly): void { + if (!is_bool($httpOnly)) { + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a boolean to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); + } + $this->data['HttpOnly'] = $httpOnly; } @@ -310,6 +346,10 @@ public function setHttpOnly($httpOnly): void */ public function matchesPath(string $requestPath): bool { + if (!is_string($requestPath)) { + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); + } + $cookiePath = $this->getPath(); // Match on exact matches or when path is the default empty "/" @@ -338,6 +378,10 @@ public function matchesPath(string $requestPath): bool */ public function matchesDomain(string $domain): bool { + if (!is_string($domain)) { + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); + } + $cookieDomain = $this->getDomain(); if (null === $cookieDomain) { return true; From b6c70a714bbe3f64f79eac3149bd0574a1565301 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 17 Jul 2021 08:53:42 -0700 Subject: [PATCH 2/4] fix --- src/Cookie/SetCookie.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/Cookie/SetCookie.php b/src/Cookie/SetCookie.php index 6b7ff68e7..e3bafef9d 100644 --- a/src/Cookie/SetCookie.php +++ b/src/Cookie/SetCookie.php @@ -346,10 +346,6 @@ public function setHttpOnly($httpOnly): void */ public function matchesPath(string $requestPath): bool { - if (!is_string($requestPath)) { - trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); - } - $cookiePath = $this->getPath(); // Match on exact matches or when path is the default empty "/" @@ -378,10 +374,6 @@ public function matchesPath(string $requestPath): bool */ public function matchesDomain(string $domain): bool { - if (!is_string($domain)) { - trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); - } - $cookieDomain = $this->getDomain(); if (null === $cookieDomain) { return true; From 3850175843fc38239598404135cfd115b512de53 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 17 Jul 2021 08:59:51 -0700 Subject: [PATCH 3/4] Update baseline --- phpstan-baseline.neon | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 8ada6052b..a3bd3376e 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -110,3 +110,7 @@ parameters: count: 2 path: src/Middleware.php + - + message: "#^Result of && is always false\\.$#" + count: 1 + path: src/Cookie/SetCookie.php From 38a127fa1dc5f32e2ea00be48df300e6f33772a2 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 17 Jul 2021 15:22:16 -0700 Subject: [PATCH 4/4] Require version 2.2 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index f92c6739f..7fdb2a2c5 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,7 @@ "guzzlehttp/promises": "^1.4", "guzzlehttp/psr7": "^1.7 || ^2.0", "psr/http-client": "^1.0", - "symfony/deprecation-contracts": "^2.4" + "symfony/deprecation-contracts": "^2.2" }, "provide": { "psr/http-client-implementation": "1.0"