Skip to content

Commit 8efcf6d

Browse files
committed
added PHP 8 typehints
1 parent 78178b1 commit 8efcf6d

File tree

8 files changed

+19
-43
lines changed

8 files changed

+19
-43
lines changed

src/Bridges/SecurityHttp/SessionStorage.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,8 @@ private function setupExpiration(): void
101101

102102
/**
103103
* Changes namespace; allows more users to share a session.
104-
* @return static
105104
*/
106-
public function setNamespace(string $namespace)
105+
public function setNamespace(string $namespace): static
107106
{
108107
if ($this->namespace !== $namespace) {
109108
$this->namespace = $namespace;

src/Security/Authorizator.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,8 @@ interface Authorizator
3131

3232
/**
3333
* Performs a role-based authorization.
34-
* @param string|null $role
35-
* @param string|null $resource
36-
* @param string|null $privilege
3734
*/
38-
function isAllowed($role, $resource, $privilege): bool;
35+
function isAllowed(?string $role, ?string $resource, ?string $privilege): bool;
3936
}
4037

4138

src/Security/IIdentity.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ interface IIdentity
1818
{
1919
/**
2020
* Returns the ID of user.
21-
* @return mixed
2221
*/
23-
function getId();
22+
function getId(): string|int;
2423

2524
/**
2625
* Returns a list of roles that the user is a member of.

src/Security/Identity.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,9 @@ public function __construct($id, $roles = null, ?iterable $data = null)
4343

4444
/**
4545
* Sets the ID of user.
46-
* @param string|int $id
47-
* @return static
4846
*/
49-
public function setId($id)
47+
public function setId(string|int $id): static
5048
{
51-
if (!is_string($id) && !is_int($id)) {
52-
throw new Nette\InvalidArgumentException('Identity identifier must be string|int, but type "' . gettype($id) . '" given.');
53-
}
5449

5550
$this->id = is_numeric($id) && !is_float($tmp = $id * 1) ? $tmp : $id;
5651
return $this;
@@ -59,19 +54,17 @@ public function setId($id)
5954

6055
/**
6156
* Returns the ID of user.
62-
* @return mixed
6357
*/
64-
public function getId()
58+
public function getId(): string|int
6559
{
6660
return $this->id;
6761
}
6862

6963

7064
/**
7165
* Sets a list of roles that the user is a member of.
72-
* @return static
7366
*/
74-
public function setRoles(array $roles)
67+
public function setRoles(array $roles): static
7568
{
7669
$this->roles = $roles;
7770
return $this;
@@ -112,9 +105,8 @@ public function __set(string $key, $value): void
112105

113106
/**
114107
* Returns user data value.
115-
* @return mixed
116108
*/
117-
public function &__get(string $key)
109+
public function &__get(string $key): mixed
118110
{
119111
if ($this->parentIsSet($key)) {
120112
return $this->parentGet($key);

src/Security/Passwords.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Passwords
2727
* Chooses which secure algorithm is used for hashing and how to configure it.
2828
* @see https://php.net/manual/en/password.constants.php
2929
*/
30-
public function __construct($algo = PASSWORD_DEFAULT, array $options = [])
30+
public function __construct(string $algo = PASSWORD_DEFAULT, array $options = [])
3131
{
3232
$this->algo = $algo;
3333
$this->options = $options;

src/Security/Permission.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ public function deny(
424424
* @param string|string[]|null $privileges
425425
* @return static
426426
*/
427-
public function removeAllow($roles = self::All, $resources = self::All, $privileges = self::All)
427+
public function removeAllow($roles = self::All, $resources = self::All, $privileges = self::All): static
428428
{
429429
$this->setRule(false, self::Allow, $roles, $resources, $privileges);
430430
return $this;
@@ -439,7 +439,7 @@ public function removeAllow($roles = self::All, $resources = self::All, $privile
439439
* @param string|string[]|null $privileges
440440
* @return static
441441
*/
442-
public function removeDeny($roles = self::All, $resources = self::All, $privileges = self::All)
442+
public function removeDeny($roles = self::All, $resources = self::All, $privileges = self::All): static
443443
{
444444
$this->setRule(false, self::Deny, $roles, $resources, $privileges);
445445
return $this;

src/Security/User.php

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @property-read bool $loggedIn
2020
* @property-read IIdentity $identity
21-
* @property-read mixed $id
21+
* @property-read string|int $id
2222
* @property-read array $roles
2323
* @property-read int $logoutReason
2424
* @property IAuthenticator $authenticator
@@ -78,10 +78,7 @@ public function __construct(
7878
}
7979

8080

81-
/**
82-
* @return UserStorage|IUserStorage
83-
*/
84-
final public function getStorage()
81+
final public function getStorage(): UserStorage|IUserStorage
8582
{
8683
return $this->storage;
8784
}
@@ -96,7 +93,7 @@ final public function getStorage()
9693
* @throws AuthenticationException if authentication was not successful
9794
*/
9895
public function login(
99-
$user,
96+
string|IIdentity $user,
10097
#[\SensitiveParameter]
10198
?string $password = null
10299
): void
@@ -202,9 +199,8 @@ private function getStoredData(): void
202199

203200
/**
204201
* Returns current user ID, if any.
205-
* @return mixed
206202
*/
207-
public function getId()
203+
public function getId(): string|int|null
208204
{
209205
$identity = $this->getIdentity();
210206
return $identity ? $identity->getId() : null;
@@ -219,9 +215,8 @@ final public function refreshStorage(): void
219215

220216
/**
221217
* Sets authentication handler.
222-
* @return static
223218
*/
224-
public function setAuthenticator(IAuthenticator $handler)
219+
public function setAuthenticator(IAuthenticator $handler): static
225220
{
226221
$this->authenticator = $handler;
227222
return $this;
@@ -264,15 +259,9 @@ final public function hasAuthenticator(): bool
264259

265260
/**
266261
* Enables log out after inactivity (like '20 minutes').
267-
* @param string|null $expire
268-
* @param int|bool $clearIdentity
269-
* @return static
270262
*/
271-
public function setExpiration($expire, $clearIdentity = null)
263+
public function setExpiration(?string $expire, bool|int|null $clearIdentity = null)
272264
{
273-
if ($expire !== null && !is_string($expire)) {
274-
trigger_error("Expiration should be a string like '20 minutes' etc.", E_USER_DEPRECATED);
275-
}
276265

277266
if (func_num_args() > 2) {
278267
$clearIdentity = $clearIdentity || func_get_arg(2);
@@ -346,9 +335,8 @@ public function isAllowed($resource = Authorizator::All, $privilege = Authorizat
346335

347336
/**
348337
* Sets authorization handler.
349-
* @return static
350338
*/
351-
public function setAuthorizator(Authorizator $handler)
339+
public function setAuthorizator(Authorizator $handler): static
352340
{
353341
$this->authorizator = $handler;
354342
return $this;

tests/Security/MockUserStorage.legacy.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ class MockUserStorage implements Nette\Security\IUserStorage
99
private $identity;
1010

1111

12-
public function setAuthenticated(bool $state)
12+
public function setAuthenticated(bool $state): self
1313
{
1414
$this->auth = $state;
15+
return $this;
1516
}
1617

1718

0 commit comments

Comments
 (0)