Skip to content

Commit 4d6c268

Browse files
committed
Update PHP minimum version to 8.0 and dependencies to latest compatible version
1 parent 19c6b2d commit 4d6c268

File tree

12 files changed

+93
-102
lines changed

12 files changed

+93
-102
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
fail-fast: false
1414
matrix:
1515
operating-system: [ubuntu-latest, windows-latest, macOS-latest]
16-
php-versions: ['7.4', '8.0', '8.1', '8.2', '8.3']
16+
php-versions: ['8.0', '8.1', '8.2', '8.3', '8.4']
1717
name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }}
1818
steps:
1919
- name: Checkout
@@ -28,7 +28,7 @@ jobs:
2828
run: php -v
2929
- name: Cache Composer packages
3030
id: composer-cache
31-
uses: actions/cache@v3
31+
uses: actions/cache@v4
3232
with:
3333
path: vendor
3434
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}

.scrutinizer.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ build:
1111
command: phpcs-run
1212
use_website_config: false
1313
environment:
14-
php: 7.4.0
14+
php: 8.0
1515
filter:
1616
excluded_paths:
1717
- 'tests/*'

README.MD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
### Requirements
16-
- **PHP >= 7.4**, **PHP 8**
16+
- **PHP >= 8.0**
1717

1818
### Installation
1919
#### Using composer (recommended)

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@
1919
},
2020

2121
"require": {
22-
"php": "^7.4 || ^8",
22+
"php": "^8",
2323
"platine-php/http": "^1.0",
2424
"platine-php/request-handler": "^1.0"
2525
},
2626

2727
"require-dev": {
2828
"phpmd/phpmd": "@stable",
29-
"phpunit/phpunit": "^9.5",
30-
"platine-php/dev": "^1.0",
29+
"phpunit/phpunit": "^9.6",
30+
"platine-php/dev": "^2.0",
3131
"squizlabs/php_codesniffer": "3.*",
32-
"phpstan/phpstan": "^1.8"
32+
"phpstan/phpstan": "^2.0"
3333
},
3434

3535
"autoload": {

src/Cookie.php

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@
4747

4848
namespace Platine\Cookie;
4949

50+
use DateTimeInterface;
51+
use InvalidArgumentException;
52+
53+
/**
54+
* @class Cookie
55+
* @package Platine\Cookie
56+
*/
5057
class Cookie implements CookieInterface
5158
{
5259
/**
@@ -112,7 +119,7 @@ class Cookie implements CookieInterface
112119
*
113120
* @param string $name
114121
* @param string $value
115-
* @param int|string|\DateTimeInterface|null $expire
122+
* @param int|string|DateTimeInterface|null $expire
116123
* @param string |null $domain
117124
* @param string |null $path
118125
* @param bool|null $secure
@@ -122,7 +129,7 @@ class Cookie implements CookieInterface
122129
public function __construct(
123130
string $name,
124131
string $value = '',
125-
$expire = null,
132+
int|string|DateTimeInterface|null $expire = null,
126133
?string $domain = null,
127134
?string $path = '/',
128135
?bool $secure = true,
@@ -216,7 +223,7 @@ public function expire(): self
216223
/**
217224
* {@inheritdoc}
218225
*/
219-
public function withExpires($expire = null): self
226+
public function withExpires(int|string|DateTimeInterface|null $expire = null): self
220227
{
221228
if ($expire === $this->expires) {
222229
return $this;
@@ -306,7 +313,7 @@ public function withSecure(bool $secure = true): self
306313
*/
307314
public function isHttpOnly(): bool
308315
{
309-
return $this->httpOnly ? $this->httpOnly : false;
316+
return $this->httpOnly ?? false;
310317
}
311318

312319
/**
@@ -364,7 +371,7 @@ public function __toString(): string
364371
{
365372
$cookie = $this->name . '=' . rawurlencode($this->value);
366373

367-
if (!$this->isSession()) {
374+
if ($this->isSession() === false) {
368375
$cookie .= '; Expires=' . gmdate('D, d-M-Y H:i:s T', $this->expires);
369376
$cookie .= '; Max-Age=' . $this->getMaxAge();
370377
}
@@ -399,14 +406,14 @@ public function __toString(): string
399406
private function setName(string $name): self
400407
{
401408
if (empty($name)) {
402-
throw new \InvalidArgumentException(sprintf(
409+
throw new InvalidArgumentException(sprintf(
403410
'The cookie name [%s] could not be empty',
404411
$name
405412
));
406413
}
407414

408415
if (!preg_match('/^[a-zA-Z0-9!#$%&\' *+\-.^_`|~]+$/', $name)) {
409-
throw new \InvalidArgumentException(sprintf(
416+
throw new InvalidArgumentException(sprintf(
410417
'The cookie name [%s] contains invalid characters; must contain any US-ASCII'
411418
. ' characters, except control and separator characters, spaces, or tabs.',
412419
$name
@@ -431,35 +438,22 @@ private function setValue(string $value): self
431438

432439
/**
433440
* Set the cookie expires
434-
* @param mixed $expire
441+
* @param int|string|DateTimeInterface|null $expire
435442
*/
436-
private function setExpires($expire): self
443+
private function setExpires(int|string|DateTimeInterface|null $expire): self
437444
{
438-
if (
439-
$expire !== null
440-
&& !is_int($expire)
441-
&& !is_string($expire)
442-
&& !$expire instanceof \DateTimeInterface
443-
) {
444-
throw new \InvalidArgumentException(sprintf(
445-
'The cookie expire time is not valid; must be null, or string,'
446-
. ' or integer, or DateTimeInterface instance; received [%s]',
447-
is_object($expire) ? get_class($expire) : gettype($expire)
448-
));
449-
}
450-
451-
if (empty($expire)) {
445+
if ($expire === null || (is_string($expire) && empty($expire))) {
452446
$expire = 0;
453447
}
454448

455-
if ($expire instanceof \DateTimeInterface) {
449+
if ($expire instanceof DateTimeInterface) {
456450
$expire = $expire->format('U');
457451
} elseif (!is_numeric($expire)) {
458452
$strExpire = $expire;
459453
$expire = strtotime($strExpire);
460454

461455
if ($expire === false) {
462-
throw new \InvalidArgumentException(sprintf(
456+
throw new InvalidArgumentException(sprintf(
463457
'The string representation of the cookie expire time [%s] is not valid',
464458
$strExpire
465459
));
@@ -521,19 +515,23 @@ private function setHttpOnly(?bool $httpOnly): self
521515
*/
522516
private function setSameSite(?string $sameSite): self
523517
{
524-
$sameSite = empty($sameSite) ? null : ucfirst(strtolower($sameSite));
518+
$sameSiteValue = empty($sameSite) ? null : ucfirst(strtolower($sameSite));
525519

526-
$sameSiteList = [self::SAME_SITE_NONE, self::SAME_SITE_LAX, self::SAME_SITE_STRICT];
520+
$sameSiteList = [
521+
self::SAME_SITE_NONE,
522+
self::SAME_SITE_LAX,
523+
self::SAME_SITE_STRICT,
524+
];
527525

528-
if ($sameSite !== null && !in_array($sameSite, $sameSiteList, true)) {
529-
throw new \InvalidArgumentException(sprintf(
526+
if ($sameSiteValue !== null && !in_array($sameSiteValue, $sameSiteList, true)) {
527+
throw new InvalidArgumentException(sprintf(
530528
'The same site attribute `%s` is not valid; must be one of (%s).',
531-
$sameSite,
529+
$sameSiteValue,
532530
implode(', ', $sameSiteList)
533531
));
534532
}
535533

536-
$this->sameSite = $sameSite;
534+
$this->sameSite = $sameSiteValue;
537535

538536
return $this;
539537
}

src/CookieInterface.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@
4747

4848
namespace Platine\Cookie;
4949

50+
use DateTimeInterface;
51+
5052
/**
53+
* @class CookieInterface
54+
* @package Platine\Cookie
55+
*
5156
* Value object representing a cookie.
5257
*
5358
* Instances of this interface are considered immutable; all methods that might
@@ -141,16 +146,16 @@ public function expire(): self;
141146
* which will expire as soon as the browser is closed.
142147
*
143148
*
144-
* @param int|string|\DateTimeInterface|null $expire
149+
* @param int|string|DateTimeInterface|null $expire
145150
*
146151
* @return CookieInterface
147152
*/
148-
public function withExpires($expire = null): self;
153+
public function withExpires(int|string|DateTimeInterface|null $expire = null): self;
149154

150155
/**
151156
* Return the domain of the cookie
152157
*
153-
* @return string
158+
* @return string|null
154159
*/
155160
public function getDomain(): ?string;
156161

@@ -169,7 +174,7 @@ public function withDomain(?string $domain): self;
169174
/**
170175
* Return the path of the cookie
171176
*
172-
* @return string
177+
* @return string|null
173178
*/
174179
public function getPath(): ?string;
175180

@@ -228,7 +233,7 @@ public function withHttpOnly(bool $httpOnly = true): self;
228233
/**
229234
* Return the path of the cookie
230235
*
231-
* @return string
236+
* @return string|null
232237
*/
233238
public function getSameSite(): ?string;
234239

src/CookieManager.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,28 @@
4747

4848
namespace Platine\Cookie;
4949

50-
use InvalidArgumentException;
5150
use Platine\Http\ResponseInterface;
5251

52+
/**
53+
* @class CookieManager
54+
* @package Platine\Cookie
55+
*/
5356
class CookieManager implements CookieManagerInterface
5457
{
5558
/**
56-
* The list of CookieInterface
57-
* @var array<CookieInterface>
59+
* The cookies
60+
* @var CookieInterface[]
5861
*/
5962
protected array $cookies = [];
6063

6164
/**
6265
* Create new instance
63-
* @param array<CookieInterface> $cookies the default cookies to store
66+
* @param CookieInterface[] $cookies the default cookies to store
6467
*/
6568
public function __construct(array $cookies = [])
6669
{
6770
foreach ($cookies as $cookie) {
68-
if (!$cookie instanceof CookieInterface) {
69-
throw new InvalidArgumentException(sprintf(
70-
'Each cookie must implement interface [%s]',
71-
CookieInterface::class
72-
));
73-
}
74-
75-
$this->cookies[$cookie->getName()] = $cookie;
71+
$this->add($cookie);
7672
}
7773
}
7874

@@ -89,7 +85,7 @@ public function add(CookieInterface $cookie): void
8985
*/
9086
public function get(string $name): ?CookieInterface
9187
{
92-
return isset($this->cookies[$name]) ? $this->cookies[$name] : null;
88+
return $this->cookies[$name] ?? null;
9389
}
9490

9591
/**
@@ -105,7 +101,9 @@ public function all(): array
105101
*/
106102
public function getValue(string $name): ?string
107103
{
108-
return isset($this->cookies[$name]) ? $this->cookies[$name]->getValue() : null;
104+
return isset($this->cookies[$name])
105+
? $this->cookies[$name]->getValue()
106+
: null;
109107
}
110108

111109
/**

src/CookieManagerInterface.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949

5050
use Platine\Http\ResponseInterface;
5151

52+
/**
53+
* @class CookieManagerInterface
54+
* @package Platine\Cookie
55+
*/
5256
interface CookieManagerInterface
5357
{
5458
/**
@@ -68,15 +72,16 @@ public function get(string $name): ?CookieInterface;
6872
/**
6973
* Return all the cookies
7074
*
71-
* @return array<CookieInterface> the list of CookieInterface
75+
* @return CookieInterface[] the list of CookieInterface
7276
*/
7377
public function all(): array;
7478

7579
/**
7680
* Return the value of the named cookie.
7781
*
7882
* @param string $name the name of the cookie
79-
* @return string|null the value of the named cookie or `null` if the cookie does not exist.
83+
* @return string|null the value of the named cookie or `null`
84+
* if the cookie does not exist.
8085
*/
8186
public function getValue(string $name): ?string;
8287

@@ -106,12 +111,14 @@ public function remove(string $name): ?CookieInterface;
106111
public function clear(): void;
107112

108113
/**
109-
* Set all cookie to the response and return a clone instance of the response with the cookies set.
114+
* Set all cookie to the response and return a clone instance
115+
* of the response with the cookies set.
110116
*
111117
* This method must be called before emitting the response.
112118
*
113119
* @param ResponseInterface $response
114-
* @param bool $removeResponseCookies whether to remove previously set cookies from the response
120+
* @param bool $removeResponseCookies whether to remove
121+
* previously set cookies from the response
115122
* @return ResponseInterface
116123
*/
117124
public function send(

0 commit comments

Comments
 (0)