Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.0] Add typing to SetCookie #3214

Draft
wants to merge 1 commit into
base: 8.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -84,7 +84,8 @@
"guzzlehttp/promises": "^2.0.2",
"guzzlehttp/psr7": "^2.6.2",
"psr/http-client": "^1.0",
"symfony/deprecation-contracts": "^2.2 || ^3.0"
"symfony/deprecation-contracts": "^2.2 || ^3.0",
"symfony/polyfill-php80": "^1.25"
},
"provide": {
"psr/http-client-implementation": "1.0"
Expand Down
84 changes: 27 additions & 57 deletions src/Cookie/SetCookie.php
Expand Up @@ -132,7 +132,7 @@
}
}

public function __toString()
public function __toString(): string
{
$str = $this->data['Name'].'='.($this->data['Value'] ?? '').'; ';
foreach ($this->data as $k => $v) {
Expand All @@ -158,7 +158,7 @@
*
* @return string
*/
public function getName()
public function getName(): string
{
return $this->data['Name'];
}
Expand All @@ -168,13 +168,9 @@
*
* @param string $name Cookie name
*/
public function setName($name): void
public function setName(string $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'] = (string) $name;
$this->data['Name'] = $name;
}

/**
Expand All @@ -192,21 +188,17 @@
*
* @param string $value Cookie value
*/
public function setValue($value): void
public function setValue(string $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'] = (string) $value;
$this->data['Value'] = $value;
}

/**
* Get the domain.
*
* @return string|null
*/
public function getDomain()
public function getDomain(): ?string
{
return $this->data['Domain'];
}
Expand All @@ -216,21 +208,17 @@
*
* @param string|null $domain
*/
public function setDomain($domain): void
public function setDomain(?string $domain): void
{
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'] = null === $domain ? null : (string) $domain;
$this->data['Domain'] = $domain;
}

/**
* Get the path.
*
* @return string
*/
public function getPath()
public function getPath(): string
{
return $this->data['Path'];
}
Expand All @@ -240,12 +228,8 @@
*
* @param string $path Path of the cookie
*/
public function setPath($path): void
public function setPath(string $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'] = (string) $path;
}

Expand All @@ -254,7 +238,7 @@
*
* @return int|null
*/
public function getMaxAge()
public function getMaxAge(): ?int
{
return null === $this->data['Max-Age'] ? null : (int) $this->data['Max-Age'];
}
Expand All @@ -264,13 +248,9 @@
*
* @param int|null $maxAge Max age of the cookie in seconds
*/
public function setMaxAge($maxAge): void
public function setMaxAge(?int $maxAge): void
{
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 === null ? null : (int) $maxAge;
$this->data['Max-Age'] = $maxAge;
}

/**
Expand All @@ -290,8 +270,10 @@
*/
public function setExpires($timestamp): void
{
if (!is_int($timestamp) && !is_string($timestamp) && null !== $timestamp) {

Check failure on line 273 in src/Cookie/SetCookie.php

View workflow job for this annotation

GitHub Actions / PHPStan

Ignored error pattern #^Result of && is always false\.$# in path /home/runner/work/guzzle/guzzle/src/Cookie/SetCookie.php is expected to occur 3 times, but occurred only 1 time.

Check failure on line 273 in src/Cookie/SetCookie.php

View workflow job for this annotation

GitHub Actions / PHPStan

Ignored error pattern #^Strict comparison using \!\=\= between null and null will always evaluate to false\.$# in path /home/runner/work/guzzle/guzzle/src/Cookie/SetCookie.php is expected to occur 3 times, but occurred only 1 time.

Check failure on line 273 in src/Cookie/SetCookie.php

View workflow job for this annotation

GitHub Actions / PHPStan

Ignored error pattern #^Result of && is always false\.$# in path /home/runner/work/guzzle/guzzle/src/Cookie/SetCookie.php is expected to occur 3 times, but occurred only 1 time.

Check failure on line 273 in src/Cookie/SetCookie.php

View workflow job for this annotation

GitHub Actions / PHPStan

Ignored error pattern #^Strict comparison using \!\=\= between null and null will always evaluate to false\.$# in path /home/runner/work/guzzle/guzzle/src/Cookie/SetCookie.php is expected to occur 3 times, but occurred only 1 time.
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__);
throw new \TypeError(
\sprintf('%s(): Argument #1 ($timestamp) must be of type int|string|null, %s given', __METHOD__, \get_debug_type($subset)),

Check failure on line 275 in src/Cookie/SetCookie.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedFunction

src/Cookie/SetCookie.php:275:114: UndefinedFunction: Function get_debug_type does not exist (see https://psalm.dev/021)

Check failure on line 275 in src/Cookie/SetCookie.php

View workflow job for this annotation

GitHub Actions / Psalm

PossiblyUndefinedVariable

src/Cookie/SetCookie.php:275:130: PossiblyUndefinedVariable: Variable $subset must be defined prior to use within an unknown function or method (see https://psalm.dev/018)

Check failure on line 275 in src/Cookie/SetCookie.php

View workflow job for this annotation

GitHub Actions / PHPStan

Undefined variable: $subset

Check failure on line 275 in src/Cookie/SetCookie.php

View workflow job for this annotation

GitHub Actions / PHPStan

Undefined variable: $subset

Check failure on line 275 in src/Cookie/SetCookie.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedFunction

src/Cookie/SetCookie.php:275:114: UndefinedFunction: Function get_debug_type does not exist (see https://psalm.dev/021)

Check failure on line 275 in src/Cookie/SetCookie.php

View workflow job for this annotation

GitHub Actions / Psalm

PossiblyUndefinedVariable

src/Cookie/SetCookie.php:275:130: PossiblyUndefinedVariable: Variable $subset must be defined prior to use within an unknown function or method (see https://psalm.dev/018)
);
}

$this->data['Expires'] = null === $timestamp ? null : (\is_numeric($timestamp) ? (int) $timestamp : \strtotime((string) $timestamp));
Expand All @@ -302,7 +284,7 @@
*
* @return bool
*/
public function getSecure()
public function getSecure(): bool
{
return $this->data['Secure'];
}
Expand All @@ -312,21 +294,17 @@
*
* @param bool $secure Set to true or false if secure
*/
public function setSecure($secure): void
public function setSecure(bool $secure): void
{
if (!is_bool($secure)) {
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'] = (bool) $secure;
$this->data['Secure'] = $secure;
}

/**
* Get whether or not this is a session cookie.
*
* @return bool|null
*/
public function getDiscard()
public function getDiscard(): ?bool
{
return $this->data['Discard'];
}
Expand All @@ -336,21 +314,17 @@
*
* @param bool $discard Set to true or false if this is a session cookie
*/
public function setDiscard($discard): void
public function setDiscard(bool $discard): void
{
if (!is_bool($discard)) {
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'] = (bool) $discard;
$this->data['Discard'] = $discard;
}

/**
* Get whether or not this is an HTTP only cookie.
*
* @return bool
*/
public function getHttpOnly()
public function getHttpOnly(): bool
{
return $this->data['HttpOnly'];
}
Expand All @@ -360,13 +334,9 @@
*
* @param bool $httpOnly Set to true or false if this is HTTP only
*/
public function setHttpOnly($httpOnly): void
public function setHttpOnly(bool $httpOnly): void
{
if (!is_bool($httpOnly)) {
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'] = (bool) $httpOnly;
$this->data['HttpOnly'] = $httpOnly;
}

/**
Expand Down Expand Up @@ -450,7 +420,7 @@
/**
* Check if the cookie is valid according to RFC 6265.
*
* @return bool|string Returns true if valid or an error message if invalid
* @return string|true Returns true if valid or an error message if invalid
*/
public function validate()
{
Expand Down