Skip to content

Commit

Permalink
Be more strict with types (#2914)
Browse files Browse the repository at this point in the history
* Be more strict with types

Make sure getMaxAge() always returns null or int. Also trigger deprecations if wrong types are passed to SetCookie

* fix

* Update baseline

* Require version 2.2
  • Loading branch information
Nyholm committed Jul 17, 2021
1 parent a2b8dd1 commit 1482295
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -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.2"
},
"provide": {
"psr/http-client-implementation": "1.0"
Expand Down
4 changes: 4 additions & 0 deletions phpstan-baseline.neon
Expand Up @@ -110,3 +110,7 @@ parameters:
count: 2
path: src/Middleware.php

-
message: "#^Result of && is always false\\.$#"
count: 1
path: src/Cookie/SetCookie.php
38 changes: 37 additions & 1 deletion src/Cookie/SetCookie.php
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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'];
}

/**
Expand All @@ -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;
}

Expand All @@ -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);
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down

0 comments on commit 1482295

Please sign in to comment.