Skip to content

Commit ace0cfd

Browse files
authored
type hints (#343)
1 parent 60e83fd commit ace0cfd

File tree

14 files changed

+340
-276
lines changed

14 files changed

+340
-276
lines changed

resources/lib/UnityConfig.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
class UnityConfig
66
{
7-
public static function getConfig($def_config_loc, $deploy_loc)
8-
{
7+
public static function getConfig(
8+
string $def_config_loc,
9+
string $deploy_loc,
10+
): array {
911
$CONFIG = parse_ini_file(
1012
$def_config_loc . "/config.ini.default",
1113
true,
@@ -22,7 +24,7 @@ public static function getConfig($def_config_loc, $deploy_loc)
2224
return $CONFIG;
2325
}
2426

25-
private static function pullConfig($CONFIG, $loc)
27+
private static function pullConfig(array $CONFIG, string $loc): array
2628
{
2729
$file_loc = $loc . "/config/config.ini";
2830
if (file_exists($file_loc)) {

resources/lib/UnityGithub.php

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

55
class UnityGithub
66
{
7-
public function getSshPublicKeys($username)
7+
public function getSshPublicKeys(string $username): array
88
{
99
$url = "https://api.github.com/users/$username/keys";
1010
$headers = ["User-Agent: Unity Cluster User Portal"];

resources/lib/UnityGroup.php

Lines changed: 76 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
namespace UnityWebPortal\lib;
4+
use PHPOpenLDAPer\LDAPEntry;
45

56
use Exception;
67

@@ -9,26 +10,24 @@
910
*/
1011
class UnityGroup
1112
{
12-
public const PI_PREFIX = "pi_";
13-
14-
public $gid;
15-
private $entry;
16-
17-
private $LDAP;
18-
private $SQL;
19-
private $MAILER;
20-
private $WEBHOOK;
21-
private $REDIS;
22-
23-
/**
24-
* Constructor for the object
25-
*
26-
* @param string $gid PI UID in the format <PI_PREFIX><OWNER_UID>
27-
* @param LDAP $LDAP LDAP Connection
28-
* @param SQL $SQL SQL Connection
29-
*/
30-
public function __construct($gid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK)
31-
{
13+
public const string PI_PREFIX = "pi_";
14+
15+
public string $gid;
16+
private LDAPEntry $entry;
17+
private UnityLDAP $LDAP;
18+
private UnitySQL $SQL;
19+
private UnityMailer $MAILER;
20+
private UnityWebhook $WEBHOOK;
21+
private UnityRedis $REDIS;
22+
23+
public function __construct(
24+
string $gid,
25+
UnityLDAP $LDAP,
26+
UnitySQL $SQL,
27+
UnityMailer $MAILER,
28+
UnityRedis $REDIS,
29+
UnityWebhook $WEBHOOK,
30+
) {
3231
$gid = trim($gid);
3332
$this->gid = $gid;
3433
$this->entry = $LDAP->getPIGroupEntry($gid);
@@ -40,7 +39,7 @@ public function __construct($gid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK)
4039
$this->WEBHOOK = $WEBHOOK;
4140
}
4241

43-
public function equals($other_group)
42+
public function equals(UnityGroup $other_group): bool
4443
{
4544
if (!is_a($other_group, self::class)) {
4645
throw new Exception(
@@ -53,29 +52,27 @@ public function equals($other_group)
5352
return $this->gid == $other_group->gid;
5453
}
5554

56-
public function __toString()
55+
public function __toString(): string
5756
{
5857
return $this->gid;
5958
}
6059

6160
/**
6261
* Checks if the current PI is an approved and existent group
63-
*
64-
* @return bool true if yes, false if no
6562
*/
66-
public function exists()
63+
public function exists(): bool
6764
{
6865
return $this->entry->exists();
6966
}
7067

7168
public function requestGroup(
72-
$firstname,
73-
$lastname,
74-
$email,
75-
$org,
76-
$send_mail_to_admins,
77-
$send_mail = true,
78-
) {
69+
string $firstname,
70+
string $lastname,
71+
string $email,
72+
string $org,
73+
bool $send_mail_to_admins,
74+
bool $send_mail = true,
75+
): void {
7976
if ($this->exists()) {
8077
return;
8178
}
@@ -117,8 +114,10 @@ public function requestGroup(
117114
/**
118115
* This method will create the group (this is what is executed when an admin approved the group)
119116
*/
120-
public function approveGroup($operator = null, $send_mail = true)
121-
{
117+
public function approveGroup(
118+
?UnityUser $operator = null,
119+
bool $send_mail = true,
120+
): void {
122121
$uid = $this->getOwner()->uid;
123122
$request = $this->SQL->getRequest($uid, UnitySQL::REQUEST_BECOME_PI);
124123
if ($this->exists()) {
@@ -152,8 +151,10 @@ public function approveGroup($operator = null, $send_mail = true)
152151
/**
153152
* This method is executed when an admin denys the PI group request
154153
*/
155-
public function denyGroup($operator = null, $send_mail = true)
156-
{
154+
public function denyGroup(
155+
?UnityUser $operator = null,
156+
bool $send_mail = true,
157+
): void {
157158
$request = $this->SQL->getRequest(
158159
$this->getOwner()->uid,
159160
UnitySQL::REQUEST_BECOME_PI,
@@ -176,7 +177,7 @@ public function denyGroup($operator = null, $send_mail = true)
176177
}
177178
}
178179

179-
public function cancelGroupRequest($send_mail = true)
180+
public function cancelGroupRequest(bool $send_mail = true): void
180181
{
181182
if (!$this->SQL->requestExists($this->getOwner()->uid)) {
182183
return;
@@ -189,8 +190,10 @@ public function cancelGroupRequest($send_mail = true)
189190
}
190191
}
191192

192-
public function cancelGroupJoinRequest($user, $send_mail = true)
193-
{
193+
public function cancelGroupJoinRequest(
194+
UnityUser $user,
195+
bool $send_mail = true,
196+
): void {
194197
if (!$this->requestExists($user)) {
195198
return;
196199
}
@@ -245,8 +248,10 @@ public function cancelGroupJoinRequest($user, $send_mail = true)
245248
* This method is executed when a user is approved to join the group
246249
* (either by admin or the group owner)
247250
*/
248-
public function approveUser($new_user, $send_mail = true)
249-
{
251+
public function approveUser(
252+
UnityUser $new_user,
253+
bool $send_mail = true,
254+
): void {
250255
$request = $this->SQL->getRequest($new_user->uid, $this->gid);
251256
if (!$new_user->exists()) {
252257
$new_user->init(
@@ -277,7 +282,7 @@ public function approveUser($new_user, $send_mail = true)
277282
}
278283
}
279284

280-
public function denyUser($new_user, $send_mail = true)
285+
public function denyUser(UnityUser $new_user, bool $send_mail = true): void
281286
{
282287
$request = $this->SQL->getRequest($new_user->uid, $this->gid);
283288
// remove request, this will fail silently if the request doesn't exist
@@ -300,8 +305,10 @@ public function denyUser($new_user, $send_mail = true)
300305
}
301306
}
302307

303-
public function removeUser($new_user, $send_mail = true)
304-
{
308+
public function removeUser(
309+
UnityUser $new_user,
310+
bool $send_mail = true,
311+
): void {
305312
if (!$this->memberExists($new_user)) {
306313
return;
307314
}
@@ -333,13 +340,13 @@ public function removeUser($new_user, $send_mail = true)
333340
}
334341

335342
public function newUserRequest(
336-
$new_user,
337-
$firstname,
338-
$lastname,
339-
$email,
340-
$org,
341-
$send_mail = true,
342-
) {
343+
UnityUser $new_user,
344+
string $firstname,
345+
string $lastname,
346+
string $email,
347+
string $org,
348+
bool $send_mail = true,
349+
): void {
343350
if ($this->memberExists($new_user)) {
344351
UnityHTTPD::errorLog(
345352
"warning",
@@ -377,7 +384,7 @@ public function newUserRequest(
377384
}
378385
}
379386

380-
public function getRequests()
387+
public function getRequests(): array
381388
{
382389
$requests = $this->SQL->getRequests($this->gid);
383390
$out = [];
@@ -402,7 +409,7 @@ public function getRequests()
402409
return $out;
403410
}
404411

405-
public function getGroupMembers($ignorecache = false)
412+
public function getGroupMembers(bool $ignorecache = false): array
406413
{
407414
$members = $this->getGroupMemberUIDs($ignorecache);
408415
$out = [];
@@ -420,7 +427,7 @@ public function getGroupMembers($ignorecache = false)
420427
return $out;
421428
}
422429

423-
public function getGroupMemberUIDs($ignorecache = false)
430+
public function getGroupMemberUIDs(bool $ignorecache = false): array
424431
{
425432
if (!$ignorecache) {
426433
$cached_val = $this->REDIS->getCache($this->gid, "members");
@@ -440,7 +447,7 @@ public function getGroupMemberUIDs($ignorecache = false)
440447
return $members;
441448
}
442449

443-
public function requestExists($user)
450+
public function requestExists(UnityUser $user): bool
444451
{
445452
$requesters = $this->getRequests();
446453
if (count($requesters) > 0) {
@@ -453,7 +460,7 @@ public function requestExists($user)
453460
return false;
454461
}
455462

456-
private function init()
463+
private function init(): void
457464
{
458465
$owner = $this->getOwner();
459466
\ensure(!$this->entry->exists());
@@ -467,29 +474,34 @@ private function init()
467474
// we need to update the cache here with the memberuid
468475
}
469476

470-
private function addUserToGroup($new_user)
477+
private function addUserToGroup(UnityUser $new_user): void
471478
{
472479
$this->entry->appendAttribute("memberuid", $new_user->uid);
473480
$this->entry->write();
474481
$this->REDIS->appendCacheArray($this->gid, "members", $new_user->uid);
475482
$this->REDIS->appendCacheArray($new_user->uid, "groups", $this->gid);
476483
}
477484

478-
private function removeUserFromGroup($old_user)
485+
private function removeUserFromGroup(UnityUser $old_user): void
479486
{
480487
$this->entry->removeAttributeEntryByValue("memberuid", $old_user->uid);
481488
$this->entry->write();
482489
$this->REDIS->removeCacheArray($this->gid, "members", $old_user->uid);
483490
$this->REDIS->removeCacheArray($old_user->uid, "groups", $this->gid);
484491
}
485492

486-
public function memberExists($user)
493+
public function memberExists(UnityUser $user): bool
487494
{
488495
return in_array($user->uid, $this->getGroupMemberUIDs());
489496
}
490497

491-
private function addRequest($uid, $firstname, $lastname, $email, $org)
492-
{
498+
private function addRequest(
499+
string $uid,
500+
string $firstname,
501+
string $lastname,
502+
string $email,
503+
string $org,
504+
): void {
493505
$this->SQL->addRequest(
494506
$uid,
495507
$firstname,
@@ -500,7 +512,7 @@ private function addRequest($uid, $firstname, $lastname, $email, $org)
500512
);
501513
}
502514

503-
public function getOwner()
515+
public function getOwner(): UnityUser
504516
{
505517
return new UnityUser(
506518
self::GID2OwnerUID($this->gid),
@@ -512,12 +524,12 @@ public function getOwner()
512524
);
513525
}
514526

515-
public static function ownerUID2GID($uid)
527+
public static function ownerUID2GID(string $uid): string
516528
{
517529
return self::PI_PREFIX . $uid;
518530
}
519531

520-
public static function GID2OwnerUID($gid)
532+
public static function GID2OwnerUID(string $gid): string
521533
{
522534
if (substr($gid, 0, strlen(self::PI_PREFIX)) != self::PI_PREFIX) {
523535
throw new Exception(

0 commit comments

Comments
 (0)