From 5f0291b1d43f51430788f5b4580456fa36486cea Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sun, 17 Oct 2021 14:54:23 +0100 Subject: [PATCH 1/2] Stricter treatment of types in `SetCookie` --- src/Cookie/SetCookie.php | 48 +++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/src/Cookie/SetCookie.php b/src/Cookie/SetCookie.php index e3bafef9d..7c04034d2 100644 --- a/src/Cookie/SetCookie.php +++ b/src/Cookie/SetCookie.php @@ -92,7 +92,7 @@ public function __construct(array $data = []) public function __toString() { - $str = $this->data['Name'] . '=' . $this->data['Value'] . '; '; + $str = $this->data['Name'] . '=' . ($this->data['Value'] ?? '') . '; '; foreach ($this->data as $k => $v) { if ($k !== 'Name' && $k !== 'Value' && $v !== null && $v !== false) { if ($k === 'Expires') { @@ -132,7 +132,7 @@ public function setName($name): void 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; + $this->data['Name'] = (string) $name; } /** @@ -156,7 +156,7 @@ public function setValue($value): void 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; + $this->data['Value'] = (string) $value; } /** @@ -172,15 +172,15 @@ public function getDomain() /** * Set the domain of the cookie. * - * @param string $domain + * @param string|null $domain */ 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__); + if (!is_string($domain) && null !== $domain) { + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string or null to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); } - $this->data['Domain'] = $domain; + $this->data['Domain'] = null === $domain ? null : (string) $domain; } /** @@ -204,7 +204,7 @@ public function setPath($path): void 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; + $this->data['Path'] = (string) $path; } /** @@ -220,15 +220,15 @@ public function getMaxAge() /** * Set the max-age of the cookie. * - * @param int $maxAge Max age of the cookie in seconds + * @param int|null $maxAge Max age of the cookie in seconds */ 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__); + if (!is_int($maxAge) && null !== $maxAge) { + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing an int or null to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); } - $this->data['Max-Age'] = $maxAge; + $this->data['Max-Age'] = $maxAge === null ? null : (int) $maxAge; } /** @@ -244,23 +244,21 @@ public function getExpires() /** * Set the unix timestamp for which the cookie will expire. * - * @param int|string $timestamp Unix timestamp or any English textual datetime description. + * @param int|string|null $timestamp Unix timestamp or any English textual datetime description. */ 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__); + if (!is_int($timestamp) && !is_string($timestamp) && null !== $timestamp) { + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing an int, string or null 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); + $this->data['Expires'] = null === $timestamp ? null : (\is_numeric($timestamp) ? (int) $timestamp : \strtotime((string) $timestamp)); } /** * Get whether or not this is a secure cookie. * - * @return bool|null + * @return bool */ public function getSecure() { @@ -275,10 +273,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__); + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); } - $this->data['Secure'] = $secure; + $this->data['Secure'] = (bool) $secure; } /** @@ -299,10 +297,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__); + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); } - $this->data['Discard'] = $discard; + $this->data['Discard'] = (bool) $discard; } /** @@ -323,10 +321,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__); + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); } - $this->data['HttpOnly'] = $httpOnly; + $this->data['HttpOnly'] = (bool) $httpOnly; } /** From 20a7a8cd56b8ad700fad96e635830365cf85ccbb Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sun, 17 Oct 2021 15:04:14 +0100 Subject: [PATCH 2/2] Upgraded static analyzers --- phpstan-baseline.neon | 67 +++++++++++++++++++------------- psalm-baseline.xml | 45 +++++++++++++++++---- vendor-bin/phpstan/composer.json | 2 +- vendor-bin/psalm/composer.json | 2 +- 4 files changed, 78 insertions(+), 38 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index fa80b8b52..8d935a7ba 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -6,12 +6,22 @@ parameters: path: src/Client.php - - message: "#^Property GuzzleHttp\\\\Handler\\\\CurlFactory\\:\\:\\$handles has unknown class CurlHandle as its type\\.$#" + message: "#^Result of && is always false\\.$#" + count: 3 + path: src/Cookie/SetCookie.php + + - + message: "#^Strict comparison using \\!\\=\\= between null and null will always evaluate to false\\.$#" + count: 3 + path: src/Cookie/SetCookie.php + + - + message: "#^Cannot access offset 'version' on array\\|false\\.$#" count: 1 path: src/Handler/CurlFactory.php - - message: "#^Parameter \\#1 \\$ch of function curl_setopt_array expects resource, CurlHandle\\|resource given\\.$#" + message: "#^Cannot call method getBody\\(\\) on Psr\\\\Http\\\\Message\\\\ResponseInterface\\|null\\.$#" count: 1 path: src/Handler/CurlFactory.php @@ -21,7 +31,12 @@ parameters: path: src/Handler/CurlFactory.php - - message: "#^Parameter \\#1 \\$ch of function curl_setopt expects resource, CurlHandle\\|resource given\\.$#" + message: "#^Parameter \\#1 \\$ch of function curl_error expects resource, CurlHandle\\|resource given\\.$#" + count: 1 + path: src/Handler/CurlFactory.php + + - + message: "#^Parameter \\#1 \\$ch of function curl_getinfo expects resource, CurlHandle\\|resource given\\.$#" count: 4 path: src/Handler/CurlFactory.php @@ -31,52 +46,57 @@ parameters: path: src/Handler/CurlFactory.php - - message: "#^Parameter \\#1 \\$ch of function curl_getinfo expects resource, CurlHandle\\|resource given\\.$#" + message: "#^Parameter \\#1 \\$ch of function curl_setopt expects resource, CurlHandle\\|resource given\\.$#" count: 4 path: src/Handler/CurlFactory.php - - message: "#^Parameter \\#1 \\$ch of function curl_error expects resource, CurlHandle\\|resource given\\.$#" + message: "#^Parameter \\#1 \\$ch of function curl_setopt_array expects resource, CurlHandle\\|resource given\\.$#" count: 1 path: src/Handler/CurlFactory.php - - message: "#^Cannot access offset 'version' on array\\|false\\.$#" + message: "#^Parameter \\#1 \\$str1 of function strcasecmp expects string, int\\|string given\\.$#" count: 1 path: src/Handler/CurlFactory.php - - message: "#^Parameter \\#1 \\$str1 of function strcasecmp expects string, int\\|string given\\.$#" + message: "#^Property GuzzleHttp\\\\Handler\\\\CurlFactory\\:\\:\\$handles has unknown class CurlHandle as its type\\.$#" count: 1 path: src/Handler/CurlFactory.php - - message: "#^Parameter \\#1 \\$ch of function curl_exec expects resource, CurlHandle\\|resource given\\.$#" + message: "#^Parameter \\#1 \\$ch of function curl_errno expects resource, CurlHandle\\|resource given\\.$#" count: 1 path: src/Handler/CurlHandler.php - - message: "#^Parameter \\#1 \\$ch of function curl_errno expects resource, CurlHandle\\|resource given\\.$#" + message: "#^Parameter \\#1 \\$ch of function curl_exec expects resource, CurlHandle\\|resource given\\.$#" count: 1 path: src/Handler/CurlHandler.php - - message: "#^Property GuzzleHttp\\\\Handler\\\\CurlMultiHandler\\:\\:\\$active has unknown class CurlMultiHandle as its type\\.$#" + message: "#^Parameter \\#1 \\$mh of function curl_multi_add_handle expects resource, CurlMultiHandle\\|resource given\\.$#" + count: 2 + path: src/Handler/CurlMultiHandler.php + + - + message: "#^Parameter \\#1 \\$mh of function curl_multi_close expects resource, CurlMultiHandle\\|resource given\\.$#" count: 1 path: src/Handler/CurlMultiHandler.php - - message: "#^Return typehint of method GuzzleHttp\\\\Handler\\\\CurlMultiHandler\\:\\:__get\\(\\) has invalid type CurlMultiHandle\\.$#" + message: "#^Parameter \\#1 \\$mh of function curl_multi_exec expects resource, CurlMultiHandle\\|resource given\\.$#" count: 1 path: src/Handler/CurlMultiHandler.php - - message: "#^Parameter \\#1 \\$mh of function curl_multi_close expects resource, CurlMultiHandle\\|resource given\\.$#" + message: "#^Parameter \\#1 \\$mh of function curl_multi_info_read expects resource, CurlMultiHandle\\|resource given\\.$#" count: 1 path: src/Handler/CurlMultiHandler.php - - message: "#^Parameter \\#1 \\$mh of function curl_multi_add_handle expects resource, CurlMultiHandle\\|resource given\\.$#" + message: "#^Parameter \\#1 \\$mh of function curl_multi_remove_handle expects resource, CurlMultiHandle\\|resource given\\.$#" count: 2 path: src/Handler/CurlMultiHandler.php @@ -86,17 +106,12 @@ parameters: path: src/Handler/CurlMultiHandler.php - - message: "#^Parameter \\#1 \\$mh of function curl_multi_exec expects resource, CurlMultiHandle\\|resource given\\.$#" + message: "#^Property GuzzleHttp\\\\Handler\\\\CurlMultiHandler\\:\\:\\$active has unknown class CurlMultiHandle as its type\\.$#" count: 1 path: src/Handler/CurlMultiHandler.php - - message: "#^Parameter \\#1 \\$mh of function curl_multi_remove_handle expects resource, CurlMultiHandle\\|resource given\\.$#" - count: 2 - path: src/Handler/CurlMultiHandler.php - - - - message: "#^Parameter \\#1 \\$mh of function curl_multi_info_read expects resource, CurlMultiHandle\\|resource given\\.$#" + message: "#^Return typehint of method GuzzleHttp\\\\Handler\\\\CurlMultiHandler\\:\\:__get\\(\\) has invalid type CurlMultiHandle\\.$#" count: 1 path: src/Handler/CurlMultiHandler.php @@ -105,17 +120,13 @@ parameters: count: 1 path: src/Handler/EasyHandle.php - - - message: "#^Result of && is always false\\.$#" - count: 2 - path: src/Middleware.php - - message: "#^Result of && is always false\\.$#" count: 1 - path: src/Cookie/SetCookie.php + path: src/HandlerStack.php - message: "#^Result of && is always false\\.$#" - count: 1 - path: src/HandlerStack.php + count: 2 + path: src/Middleware.php + diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 3404799ca..5b674bb00 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -13,16 +13,45 @@ $result + + + (bool) $discard + (bool) $httpOnly + (bool) $secure + (int) $maxAge + (string) $domain + (string) $name + (string) $path + (string) $timestamp + (string) $value + + + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__) + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__) + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__) + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string or null to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__) + 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__) + 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__) + 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__) + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing an int or null to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__) + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing an int, string or null to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__) + + $uri - - $timeoutRequiresNoSignal + $timeoutRequiresNoSignal + + $options['connect_timeout'] < 1 + + + $timeoutRequiresNoSignal + $easy->handle $easy->handle @@ -41,9 +70,6 @@ $sslKey - - $timeoutRequiresNoSignal - private $handles = []; resource[]|\CurlHandle[] @@ -68,6 +94,9 @@ $this->_mh $this->_mh + + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing an integer to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__) + resource|\CurlMultiHandle resource|\CurlMultiHandle|null @@ -77,9 +106,6 @@ - - (string) $startLine[2] - resource|\CurlHandle @@ -88,6 +114,9 @@ $uri + + empty($options) + diff --git a/vendor-bin/phpstan/composer.json b/vendor-bin/phpstan/composer.json index bfbc7273b..b09c65ddf 100644 --- a/vendor-bin/phpstan/composer.json +++ b/vendor-bin/phpstan/composer.json @@ -1,7 +1,7 @@ { "require": { "php": "^7.2.5 || ^8.0", - "phpstan/phpstan": "0.12.81", + "phpstan/phpstan": "0.12.99", "phpstan/phpstan-deprecation-rules": "0.12.6" }, "config": { diff --git a/vendor-bin/psalm/composer.json b/vendor-bin/psalm/composer.json index 535a0797d..7794e6bbc 100644 --- a/vendor-bin/psalm/composer.json +++ b/vendor-bin/psalm/composer.json @@ -1,7 +1,7 @@ { "require": { "php": "^7.2.5 || ^8.0", - "psalm/phar": "4.6.2" + "psalm/phar": "4.10.0" }, "config": { "preferred-install": "dist"