Skip to content

Commit dc5cc9f

Browse files
committed
added PHP 8 typehints
1 parent 573922e commit dc5cc9f

File tree

8 files changed

+47
-81
lines changed

8 files changed

+47
-81
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
@@ -27,11 +27,8 @@ interface Authorizator
2727

2828
/**
2929
* Performs a role-based authorization.
30-
* @param string|null $role
31-
* @param string|null $resource
32-
* @param string|null $privilege
3330
*/
34-
function isAllowed($role, $resource, $privilege): bool;
31+
function isAllowed(?string $role, ?string $resource, ?string $privilege): bool;
3532
}
3633

3734

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: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,10 @@ class Permission implements Authorizator
5252
/**
5353
* Adds a Role to the list. The most recently added parent
5454
* takes precedence over parents that were previously added.
55-
* @param string|array $parents
5655
* @throws Nette\InvalidArgumentException
5756
* @throws Nette\InvalidStateException
58-
* @return static
5957
*/
60-
public function addRole(string $role, $parents = null)
58+
public function addRole(string $role, string|array|null $parents = null): static
6159
{
6260
$this->checkRole($role, false);
6361
if (isset($this->roles[$role])) {
@@ -161,9 +159,8 @@ public function roleInheritsFrom(string $role, string $inherit, bool $onlyParent
161159
* Removes the Role from the list.
162160
*
163161
* @throws Nette\InvalidStateException
164-
* @return static
165162
*/
166-
public function removeRole(string $role)
163+
public function removeRole(string $role): static
167164
{
168165
$this->checkRole($role);
169166

@@ -199,10 +196,8 @@ public function removeRole(string $role)
199196

200197
/**
201198
* Removes all Roles from the list.
202-
*
203-
* @return static
204199
*/
205-
public function removeAllRoles()
200+
public function removeAllRoles(): static
206201
{
207202
$this->roles = [];
208203

@@ -228,9 +223,8 @@ public function removeAllRoles()
228223
*
229224
* @throws Nette\InvalidArgumentException
230225
* @throws Nette\InvalidStateException
231-
* @return static
232226
*/
233-
public function addResource(string $resource, ?string $parent = null)
227+
public function addResource(string $resource, ?string $parent = null): static
234228
{
235229
$this->checkResource($resource, false);
236230

@@ -324,9 +318,8 @@ public function resourceInheritsFrom(string $resource, string $inherit, bool $on
324318
* Removes a Resource and all of its children.
325319
*
326320
* @throws Nette\InvalidStateException
327-
* @return static
328321
*/
329-
public function removeResource(string $resource)
322+
public function removeResource(string $resource): static
330323
{
331324
$this->checkResource($resource);
332325

@@ -356,9 +349,8 @@ public function removeResource(string $resource)
356349

357350
/**
358351
* Removes all Resources.
359-
* @return static
360352
*/
361-
public function removeAllResources()
353+
public function removeAllResources(): static
362354
{
363355
foreach ($this->resources as $resource => $foo) {
364356
foreach ($this->rules['byResource'] as $resourceCurrent => $rules) {
@@ -383,14 +375,14 @@ public function removeAllResources()
383375
* @param string|string[]|null $roles
384376
* @param string|string[]|null $resources
385377
* @param string|string[]|null $privileges
386-
* @return static
387378
*/
388379
public function allow(
389380
$roles = self::ALL,
390381
$resources = self::ALL,
391382
$privileges = self::ALL,
392383
?callable $assertion = null,
393-
) {
384+
): static
385+
{
394386
$this->setRule(true, self::ALLOW, $roles, $resources, $privileges, $assertion);
395387
return $this;
396388
}
@@ -403,14 +395,14 @@ public function allow(
403395
* @param string|string[]|null $roles
404396
* @param string|string[]|null $resources
405397
* @param string|string[]|null $privileges
406-
* @return static
407398
*/
408399
public function deny(
409400
$roles = self::ALL,
410401
$resources = self::ALL,
411402
$privileges = self::ALL,
412403
?callable $assertion = null,
413-
) {
404+
): static
405+
{
414406
$this->setRule(true, self::DENY, $roles, $resources, $privileges, $assertion);
415407
return $this;
416408
}
@@ -422,9 +414,8 @@ public function deny(
422414
* @param string|string[]|null $roles
423415
* @param string|string[]|null $resources
424416
* @param string|string[]|null $privileges
425-
* @return static
426417
*/
427-
public function removeAllow($roles = self::ALL, $resources = self::ALL, $privileges = self::ALL)
418+
public function removeAllow($roles = self::ALL, $resources = self::ALL, $privileges = self::ALL): static
428419
{
429420
$this->setRule(false, self::ALLOW, $roles, $resources, $privileges);
430421
return $this;
@@ -437,9 +428,8 @@ public function removeAllow($roles = self::ALL, $resources = self::ALL, $privile
437428
* @param string|string[]|null $roles
438429
* @param string|string[]|null $resources
439430
* @param string|string[]|null $privileges
440-
* @return static
441431
*/
442-
public function removeDeny($roles = self::ALL, $resources = self::ALL, $privileges = self::ALL)
432+
public function removeDeny($roles = self::ALL, $resources = self::ALL, $privileges = self::ALL): static
443433
{
444434
$this->setRule(false, self::DENY, $roles, $resources, $privileges);
445435
return $this;
@@ -452,9 +442,15 @@ public function removeDeny($roles = self::ALL, $resources = self::ALL, $privileg
452442
* @param string|string[]|null $resources
453443
* @param string|string[]|null $privileges
454444
* @throws Nette\InvalidStateException
455-
* @return static
456445
*/
457-
protected function setRule(bool $toAdd, bool $type, $roles, $resources, $privileges, ?callable $assertion = null)
446+
protected function setRule(
447+
bool $toAdd,
448+
bool $type,
449+
$roles,
450+
$resources,
451+
$privileges,
452+
?callable $assertion = null,
453+
): static
458454
{
459455
// ensure that all specified Roles exist; normalize input to array of Roles or null
460456
if ($roles === self::ALL) {
@@ -564,12 +560,13 @@ protected function setRule(bool $toAdd, bool $type, $roles, $resources, $privile
564560
* and its respective parents are checked similarly before the lower-priority parents of
565561
* the Role are checked.
566562
*
567-
* @param string|Role|null $role
568-
* @param string|Nette\Security\Resource|null $resource
569-
* @param string|null $privilege
570563
* @throws Nette\InvalidStateException
571564
*/
572-
public function isAllowed($role = self::ALL, $resource = self::ALL, $privilege = self::ALL): bool
565+
public function isAllowed(
566+
string|Role|null $role = self::ALL,
567+
string|Nette\Security\Resource|null $resource = self::ALL,
568+
?string $privilege = self::ALL,
569+
): bool
573570
{
574571
$this->queriedRole = $role;
575572
if ($role !== self::ALL) {
@@ -626,19 +623,17 @@ public function isAllowed($role = self::ALL, $resource = self::ALL, $privilege =
626623

627624
/**
628625
* Returns real currently queried Role. Use by assertion.
629-
* @return mixed
630626
*/
631-
public function getQueriedRole()
627+
public function getQueriedRole(): mixed
632628
{
633629
return $this->queriedRole;
634630
}
635631

636632

637633
/**
638634
* Returns real currently queried Resource. Use by assertion.
639-
* @return mixed
640635
*/
641-
public function getQueriedResource()
636+
public function getQueriedResource(): mixed
642637
{
643638
return $this->queriedResource;
644639
}
@@ -653,7 +648,7 @@ public function getQueriedResource()
653648
* @param bool $all (true) or one?
654649
* @return mixed null if no applicable rule is found, otherwise returns ALLOW or DENY
655650
*/
656-
private function searchRolePrivileges(bool $all, $role, $resource, $privilege)
651+
private function searchRolePrivileges(bool $all, $role, $resource, $privilege): mixed
657652
{
658653
$dfs = [
659654
'visited' => [],
@@ -698,12 +693,9 @@ private function searchRolePrivileges(bool $all, $role, $resource, $privilege)
698693

699694
/**
700695
* Returns the rule type associated with the specified Resource, Role, and privilege.
701-
* @param string|null $resource
702-
* @param string|null $role
703-
* @param string|null $privilege
704696
* @return bool|null null if a rule does not exist or assertion fails, otherwise returns ALLOW or DENY
705697
*/
706-
private function getRuleType($resource, $role, $privilege): ?bool
698+
private function getRuleType(?string $resource, ?string $role, ?string $privilege): ?bool
707699
{
708700
if (!$rules = $this->getRules($resource, $role)) {
709701
return null;
@@ -740,10 +732,8 @@ private function getRuleType($resource, $role, $privilege): ?bool
740732
/**
741733
* Returns the rules associated with a Resource and a Role, or null if no such rules exist.
742734
* If the $create parameter is true, then a rule set is first created and then returned to the caller.
743-
* @param string|null $resource
744-
* @param string|null $role
745735
*/
746-
private function &getRules($resource, $role, bool $create = false): ?array
736+
private function &getRules(?string $resource, ?string $role, bool $create = false): ?array
747737
{
748738
$null = null;
749739
if ($resource === self::ALL) {

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
@@ -75,10 +75,7 @@ public function __construct(
7575
}
7676

7777

78-
/**
79-
* @return UserStorage|IUserStorage
80-
*/
81-
final public function getStorage()
78+
final public function getStorage(): UserStorage|IUserStorage
8279
{
8380
return $this->storage;
8481
}
@@ -92,7 +89,7 @@ final public function getStorage()
9289
* @param string|IIdentity $user name or Identity
9390
* @throws AuthenticationException if authentication was not successful
9491
*/
95-
public function login($user, ?string $password = null): void
92+
public function login(string|IIdentity $user, ?string $password = null): void
9693
{
9794
$this->logout(true);
9895
if ($user instanceof IIdentity) {
@@ -195,9 +192,8 @@ private function getStoredData(): void
195192

196193
/**
197194
* Returns current user ID, if any.
198-
* @return mixed
199195
*/
200-
public function getId()
196+
public function getId(): string|int|null
201197
{
202198
$identity = $this->getIdentity();
203199
return $identity ? $identity->getId() : null;
@@ -212,9 +208,8 @@ final public function refreshStorage(): void
212208

213209
/**
214210
* Sets authentication handler.
215-
* @return static
216211
*/
217-
public function setAuthenticator(IAuthenticator $handler)
212+
public function setAuthenticator(IAuthenticator $handler): static
218213
{
219214
$this->authenticator = $handler;
220215
return $this;
@@ -257,15 +252,9 @@ final public function hasAuthenticator(): bool
257252

258253
/**
259254
* Enables log out after inactivity (like '20 minutes').
260-
* @param string|null $expire
261-
* @param int|bool $clearIdentity
262-
* @return static
263255
*/
264-
public function setExpiration($expire, $clearIdentity = null)
256+
public function setExpiration(?string $expire, bool|int|null $clearIdentity = null)
265257
{
266-
if ($expire !== null && !is_string($expire)) {
267-
trigger_error("Expiration should be a string like '20 minutes' etc.", E_USER_DEPRECATED);
268-
}
269258

270259
if (func_num_args() > 2) {
271260
$clearIdentity = $clearIdentity || func_get_arg(2);
@@ -339,9 +328,8 @@ public function isAllowed($resource = Authorizator::ALL, $privilege = Authorizat
339328

340329
/**
341330
* Sets authorization handler.
342-
* @return static
343331
*/
344-
public function setAuthorizator(Authorizator $handler)
332+
public function setAuthorizator(Authorizator $handler): static
345333
{
346334
$this->authorizator = $handler;
347335
return $this;

0 commit comments

Comments
 (0)