Skip to content

Commit 22875a7

Browse files
committed
added PHP 8 typehints
1 parent 6ed5a50 commit 22875a7

File tree

7 files changed

+60
-110
lines changed

7 files changed

+60
-110
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/Identity.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,27 @@ 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-
}
54-
5549
$this->id = is_numeric($id) && !is_float($tmp = $id * 1) ? $tmp : $id;
5650
return $this;
5751
}
5852

5953

6054
/**
6155
* Returns the ID of user.
62-
* @return mixed
6356
*/
64-
public function getId()
57+
public function getId(): string|int
6558
{
6659
return $this->id;
6760
}
6861

6962

7063
/**
7164
* Sets a list of roles that the user is a member of.
72-
* @return static
7365
*/
74-
public function setRoles(array $roles)
66+
public function setRoles(array $roles): static
7567
{
7668
$this->roles = $roles;
7769
return $this;
@@ -112,9 +104,8 @@ public function __set(string $key, $value): void
112104

113105
/**
114106
* Returns user data value.
115-
* @return mixed
116107
*/
117-
public function &__get(string $key)
108+
public function &__get(string $key): mixed
118109
{
119110
if ($this->parentIsSet($key)) {
120111
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: 44 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,10 @@ class Permission implements Authorizator
4949
/**
5050
* Adds a Role to the list. The most recently added parent
5151
* takes precedence over parents that were previously added.
52-
* @param string|array $parents
5352
* @throws Nette\InvalidArgumentException
5453
* @throws Nette\InvalidStateException
55-
* @return static
5654
*/
57-
public function addRole(string $role, $parents = null)
55+
public function addRole(string $role, string|array $parents = null): static
5856
{
5957
$this->checkRole($role, exists: false);
6058
if (isset($this->roles[$role])) {
@@ -158,9 +156,8 @@ public function roleInheritsFrom(string $role, string $inherit, bool $onlyParent
158156
* Removes the Role from the list.
159157
*
160158
* @throws Nette\InvalidStateException
161-
* @return static
162159
*/
163-
public function removeRole(string $role)
160+
public function removeRole(string $role): static
164161
{
165162
$this->checkRole($role);
166163

@@ -196,10 +193,8 @@ public function removeRole(string $role)
196193

197194
/**
198195
* Removes all Roles from the list.
199-
*
200-
* @return static
201196
*/
202-
public function removeAllRoles()
197+
public function removeAllRoles(): static
203198
{
204199
$this->roles = [];
205200

@@ -225,9 +220,8 @@ public function removeAllRoles()
225220
*
226221
* @throws Nette\InvalidArgumentException
227222
* @throws Nette\InvalidStateException
228-
* @return static
229223
*/
230-
public function addResource(string $resource, ?string $parent = null)
224+
public function addResource(string $resource, ?string $parent = null): static
231225
{
232226
$this->checkResource($resource, exists: false);
233227

@@ -321,9 +315,8 @@ public function resourceInheritsFrom(string $resource, string $inherit, bool $on
321315
* Removes a Resource and all of its children.
322316
*
323317
* @throws Nette\InvalidStateException
324-
* @return static
325318
*/
326-
public function removeResource(string $resource)
319+
public function removeResource(string $resource): static
327320
{
328321
$this->checkResource($resource);
329322

@@ -353,9 +346,8 @@ public function removeResource(string $resource)
353346

354347
/**
355348
* Removes all Resources.
356-
* @return static
357349
*/
358-
public function removeAllResources()
350+
public function removeAllResources(): static
359351
{
360352
foreach ($this->resources as $resource => $foo) {
361353
foreach ($this->rules['byResource'] as $resourceCurrent => $rules) {
@@ -376,18 +368,14 @@ public function removeAllResources()
376368
/**
377369
* Allows one or more Roles access to [certain $privileges upon] the specified Resource(s).
378370
* If $assertion is provided, then it must return true in order for rule to apply.
379-
*
380-
* @param string|string[]|null $roles
381-
* @param string|string[]|null $resources
382-
* @param string|string[]|null $privileges
383-
* @return static
384371
*/
385372
public function allow(
386-
$roles = self::All,
387-
$resources = self::All,
388-
$privileges = self::All,
373+
string|array|null $roles = self::All,
374+
string|array|null $resources = self::All,
375+
string|array|null $privileges = self::All,
389376
?callable $assertion = null,
390-
) {
377+
): static
378+
{
391379
$this->setRule(true, self::Allow, $roles, $resources, $privileges, $assertion);
392380
return $this;
393381
}
@@ -396,32 +384,27 @@ public function allow(
396384
/**
397385
* Denies one or more Roles access to [certain $privileges upon] the specified Resource(s).
398386
* If $assertion is provided, then it must return true in order for rule to apply.
399-
*
400-
* @param string|string[]|null $roles
401-
* @param string|string[]|null $resources
402-
* @param string|string[]|null $privileges
403-
* @return static
404387
*/
405388
public function deny(
406-
$roles = self::All,
407-
$resources = self::All,
408-
$privileges = self::All,
389+
string|array|null $roles = self::All,
390+
string|array|null $resources = self::All,
391+
string|array|null $privileges = self::All,
409392
?callable $assertion = null,
410-
) {
393+
): static
394+
{
411395
$this->setRule(true, self::Deny, $roles, $resources, $privileges, $assertion);
412396
return $this;
413397
}
414398

415399

416400
/**
417401
* Removes "allow" permissions from the list in the context of the given Roles, Resources, and privileges.
418-
*
419-
* @param string|string[]|null $roles
420-
* @param string|string[]|null $resources
421-
* @param string|string[]|null $privileges
422-
* @return static
423402
*/
424-
public function removeAllow($roles = self::All, $resources = self::All, $privileges = self::All)
403+
public function removeAllow(
404+
string|array|null $roles = self::All,
405+
string|array|null $resources = self::All,
406+
string|array|null $privileges = self::All,
407+
): static
425408
{
426409
$this->setRule(false, self::Allow, $roles, $resources, $privileges);
427410
return $this;
@@ -430,13 +413,12 @@ public function removeAllow($roles = self::All, $resources = self::All, $privile
430413

431414
/**
432415
* Removes "deny" restrictions from the list in the context of the given Roles, Resources, and privileges.
433-
*
434-
* @param string|string[]|null $roles
435-
* @param string|string[]|null $resources
436-
* @param string|string[]|null $privileges
437-
* @return static
438416
*/
439-
public function removeDeny($roles = self::All, $resources = self::All, $privileges = self::All)
417+
public function removeDeny(
418+
string|array|null $roles = self::All,
419+
string|array|null $resources = self::All,
420+
string|array|null $privileges = self::All,
421+
): static
440422
{
441423
$this->setRule(false, self::Deny, $roles, $resources, $privileges);
442424
return $this;
@@ -445,13 +427,16 @@ public function removeDeny($roles = self::All, $resources = self::All, $privileg
445427

446428
/**
447429
* Performs operations on Access Control List rules.
448-
* @param string|string[]|null $roles
449-
* @param string|string[]|null $resources
450-
* @param string|string[]|null $privileges
451430
* @throws Nette\InvalidStateException
452-
* @return static
453431
*/
454-
protected function setRule(bool $toAdd, bool $type, $roles, $resources, $privileges, ?callable $assertion = null)
432+
protected function setRule(
433+
bool $toAdd,
434+
bool $type,
435+
string|array|null $roles,
436+
string|array|null $resources,
437+
string|array|null $privileges,
438+
?callable $assertion = null,
439+
): void
455440
{
456441
// ensure that all specified Roles exist; normalize input to array of Roles or null
457442
if ($roles === self::All) {
@@ -545,8 +530,6 @@ protected function setRule(bool $toAdd, bool $type, $roles, $resources, $privile
545530
}
546531
}
547532
}
548-
549-
return $this;
550533
}
551534

552535

@@ -561,12 +544,13 @@ protected function setRule(bool $toAdd, bool $type, $roles, $resources, $privile
561544
* and its respective parents are checked similarly before the lower-priority parents of
562545
* the Role are checked.
563546
*
564-
* @param string|Role|null $role
565-
* @param string|Nette\Security\Resource|null $resource
566-
* @param string|null $privilege
567547
* @throws Nette\InvalidStateException
568548
*/
569-
public function isAllowed($role = self::All, $resource = self::All, $privilege = self::All): bool
549+
public function isAllowed(
550+
string|Role|null $role = self::All,
551+
string|Resource|null $resource = self::All,
552+
string|null $privilege = self::All,
553+
): bool
570554
{
571555
$this->queriedRole = $role;
572556
if ($role !== self::All) {
@@ -623,19 +607,17 @@ public function isAllowed($role = self::All, $resource = self::All, $privilege =
623607

624608
/**
625609
* Returns real currently queried Role. Use by assertion.
626-
* @return mixed
627610
*/
628-
public function getQueriedRole()
611+
public function getQueriedRole(): string|Role|null
629612
{
630613
return $this->queriedRole;
631614
}
632615

633616

634617
/**
635618
* Returns real currently queried Resource. Use by assertion.
636-
* @return mixed
637619
*/
638-
public function getQueriedResource()
620+
public function getQueriedResource(): string|Resource|null
639621
{
640622
return $this->queriedResource;
641623
}
@@ -648,9 +630,8 @@ public function getQueriedResource()
648630
* Performs a depth-first search of the Role DAG, starting at $role, in order to find a rule
649631
* allowing/denying $role access to a/all $privilege upon $resource.
650632
* @param bool $all (true) or one?
651-
* @return mixed null if no applicable rule is found, otherwise returns ALLOW or DENY
652633
*/
653-
private function searchRolePrivileges(bool $all, $role, $resource, $privilege)
634+
private function searchRolePrivileges(bool $all, ?string $role, ?string $resource, ?string $privilege): ?bool
654635
{
655636
$dfs = [
656637
'visited' => [],
@@ -695,12 +676,8 @@ private function searchRolePrivileges(bool $all, $role, $resource, $privilege)
695676

696677
/**
697678
* Returns the rule type associated with the specified Resource, Role, and privilege.
698-
* @param string|null $resource
699-
* @param string|null $role
700-
* @param string|null $privilege
701-
* @return bool|null null if a rule does not exist or assertion fails, otherwise returns ALLOW or DENY
702679
*/
703-
private function getRuleType($resource, $role, $privilege): ?bool
680+
private function getRuleType(?string $resource, ?string $role, ?string $privilege): ?bool
704681
{
705682
if (!$rules = $this->getRules($resource, $role)) {
706683
return null;
@@ -737,10 +714,8 @@ private function getRuleType($resource, $role, $privilege): ?bool
737714
/**
738715
* Returns the rules associated with a Resource and a Role, or null if no such rules exist.
739716
* If the $create parameter is true, then a rule set is first created and then returned to the caller.
740-
* @param string|null $resource
741-
* @param string|null $role
742717
*/
743-
private function &getRules($resource, $role, bool $create = false): ?array
718+
private function &getRules(?string $resource, ?string $role, bool $create = false): ?array
744719
{
745720
$null = null;
746721
if ($resource === self::All) {

0 commit comments

Comments
 (0)