Skip to content

Commit b002626

Browse files
committed
added PHP 8 typehints
1 parent 5db80a3 commit b002626

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
@@ -32,11 +32,8 @@ interface Authorizator
3232

3333
/**
3434
* Performs a role-based authorization.
35-
* @param string|null $role
36-
* @param string|null $resource
37-
* @param string|null $privilege
3835
*/
39-
function isAllowed($role, $resource, $privilege): bool;
36+
function isAllowed(?string $role, ?string $resource, ?string $privilege): bool;
4037
}
4138

4239

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
@@ -79,10 +79,7 @@ public function __construct(
7979
}
8080

8181

82-
/**
83-
* @return UserStorage|IUserStorage
84-
*/
85-
final public function getStorage()
82+
final public function getStorage(): UserStorage|IUserStorage
8683
{
8784
return $this->storage;
8885
}
@@ -96,7 +93,7 @@ final public function getStorage()
9693
* @param string|IIdentity $user name or Identity
9794
* @throws AuthenticationException if authentication was not successful
9895
*/
99-
public function login($user, ?string $password = null): void
96+
public function login(string|IIdentity $user, ?string $password = null): void
10097
{
10198
$this->logout(true);
10299
if ($user instanceof IIdentity) {
@@ -199,9 +196,8 @@ private function getStoredData(): void
199196

200197
/**
201198
* Returns current user ID, if any.
202-
* @return mixed
203199
*/
204-
public function getId()
200+
public function getId(): string|int|null
205201
{
206202
$identity = $this->getIdentity();
207203
return $identity ? $identity->getId() : null;
@@ -216,9 +212,8 @@ final public function refreshStorage(): void
216212

217213
/**
218214
* Sets authentication handler.
219-
* @return static
220215
*/
221-
public function setAuthenticator(IAuthenticator $handler)
216+
public function setAuthenticator(IAuthenticator $handler): static
222217
{
223218
$this->authenticator = $handler;
224219
return $this;
@@ -261,15 +256,9 @@ final public function hasAuthenticator(): bool
261256

262257
/**
263258
* Enables log out after inactivity (like '20 minutes').
264-
* @param string|null $expire
265-
* @param int|bool $clearIdentity
266-
* @return static
267259
*/
268-
public function setExpiration($expire, $clearIdentity = null)
260+
public function setExpiration(?string $expire, bool|int|null $clearIdentity = null)
269261
{
270-
if ($expire !== null && !is_string($expire)) {
271-
trigger_error("Expiration should be a string like '20 minutes' etc.", E_USER_DEPRECATED);
272-
}
273262

274263
if (func_num_args() > 2) {
275264
$clearIdentity = $clearIdentity || func_get_arg(2);
@@ -343,9 +332,8 @@ public function isAllowed($resource = Authorizator::All, $privilege = Authorizat
343332

344333
/**
345334
* Sets authorization handler.
346-
* @return static
347335
*/
348-
public function setAuthorizator(Authorizator $handler)
336+
public function setAuthorizator(Authorizator $handler): static
349337
{
350338
$this->authorizator = $handler;
351339
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)