Skip to content

Commit

Permalink
Add typing to SetCookie
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell committed Mar 31, 2024
1 parent 04ff56e commit 72a7530
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 58 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function __construct(array $data = [])
}
}

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 @@ public function toArray(): array
*
* @return string
*/
public function getName()
public function getName(): string
{
return $this->data['Name'];
}
Expand All @@ -168,13 +168,9 @@ public function getName()
*
* @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 @@ public function getValue()
*
* @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 @@ public function getDomain()
*
* @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 @@ public function getPath()
*
* @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 @@ public function setPath($path): void
*
* @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 @@ public function getMaxAge()
*
* @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 @@ -291,7 +271,9 @@ public function getExpires()
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 @@ public function setExpires($timestamp): void
*
* @return bool
*/
public function getSecure()
public function getSecure(): bool
{
return $this->data['Secure'];
}
Expand All @@ -312,21 +294,17 @@ public function getSecure()
*
* @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 @@ public function getDiscard()
*
* @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 @@ public function getHttpOnly()
*
* @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 @@ public function isExpired(): bool
/**
* 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

0 comments on commit 72a7530

Please sign in to comment.