Skip to content

Commit 28604cc

Browse files
authored
phpstan level 4 (#424)
1 parent 77fcc86 commit 28604cc

File tree

11 files changed

+53
-52
lines changed

11 files changed

+53
-52
lines changed

phpstan.neon

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
11
parameters:
2-
level: 3
2+
level: 4
33
paths:
44
- resources
55
- test
66
ignoreErrors:
7+
# $this, $data comes from UnityMailer
78
- messages:
89
- '#Variable \$this might not be defined.#'
910
- '#Variable \$data might not be defined.#'
1011
paths:
1112
- resources/mail/*
13+
# these functions are called with call_user_func
14+
- messages:
15+
- '#Method SSHKeyAddTest::addSshKeysPaste\(\) is unused.#'
16+
- '#Method SSHKeyAddTest::addSshKeysImport\(\) is unused.#'
17+
- '#Method SSHKeyAddTest::addSshKeysGenerate\(\) is unused.#'
18+
- '#Method SSHKeyAddTest::addSshKeysGithub\(\) is unused.#'
19+
paths:
20+
- test/functional/SSHKeyAddTest.php
21+
# assertTrue(true) makes phpunit shut up about tests doing no assertions
22+
- messages:
23+
- '#Call to method PHPUnit\\Framework\\Assert::assertTrue\(\) with true will always evaluate to true.#'
24+
paths:
25+
- test/functional/InvalidEPPNTest.php
26+
- test/functional/PageLoadTest.php
27+
# I cannot seem to make this error go away no matter how many functions I add @phpstan-impure to
28+
- messages:
29+
- '#Negated boolean expression is always false\.#'
30+
paths:
31+
- test/functional/PiRemoveUserTest.php
32+
- test/phpunit-bootstrap.php
33+
# $Subject is written by mail templates
34+
- messages:
35+
- '#Property UnityWebPortal\\lib\\UnityWebhook::\$Subject is never written, only read\.#'
36+
paths:
37+
- resources/lib/UnityWebhook.php

resources/lib/PosixGroup.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ public function getDN(): string
2525

2626
public function equals(PosixGroup $other_group): bool
2727
{
28-
if (!is_a($other_group, self::class)) {
29-
throw new Exception(
30-
"Unable to check equality because the parameter is not a " .
31-
self::class .
32-
" object",
33-
);
34-
}
3528
return $this->getDN() == $other_group->getDN();
3629
}
3730

resources/lib/UnityGroup.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,6 @@ public function newUserRequest(UnityUser $new_user, bool $send_mail = true): voi
250250
}
251251
if ($this->SQL->accDeletionRequestExists($new_user->uid)) {
252252
throw new Exception("user '$new_user' requested account deletion");
253-
return;
254253
}
255254
$this->addRequest($new_user->uid);
256255
if ($send_mail) {

resources/lib/UnityHTTPD.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,6 @@ public static function getPostData(string $key): mixed
245245
/* returns null if not found and not $throw_if_not_found */
246246
public static function getQueryParameter(string $key, bool $throw_if_not_found = true): mixed
247247
{
248-
if (!is_array($_GET)) {
249-
throw new RuntimeException('$_GET is not an array!');
250-
}
251248
if (!array_key_exists($key, $_GET)) {
252249
if ($throw_if_not_found) {
253250
self::badRequest("\$_GET has no array key '$key'");
@@ -263,13 +260,16 @@ public static function getUploadedFileContents(
263260
bool $do_delete_tmpfile_after_read = true,
264261
string $encoding = "UTF-8",
265262
): string {
266-
try {
267-
$tmpfile_path = $_FILES[$filename]["tmp_name"];
268-
} catch (ArrayKeyException $e) {
269-
self::badRequest("no such uploaded file", $e, [
270-
'$_FILES' => $_FILES,
271-
]);
263+
if (!array_key_exists($filename, $_FILES)) {
264+
self::badRequest("\$_FILES has no array key '$filename'", data: ['$_FILES' => $_FILES]);
265+
}
266+
if (!array_key_exists("tmp_name", $_FILES[$filename])) {
267+
self::badRequest(
268+
"\$_FILES[$filename] has no array key 'tmp_name'",
269+
data: ['$_FILES' => $_FILES],
270+
);
272271
}
272+
$tmpfile_path = $_FILES[$filename]["tmp_name"];
273273
$contents = file_get_contents($tmpfile_path);
274274
if ($contents === false) {
275275
throw new \Exception("Failed to read file: " . $tmpfile_path);

resources/lib/UnityMailer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function __construct()
7676
}
7777
}
7878

79-
public function sendMail(string $recipients, ?string $template = null, mixed $data = null)
79+
public function sendMail(string|array $recipients, ?string $template = null, mixed $data = null)
8080
{
8181
if (isset($template)) {
8282
$this->setFrom($this->MSG_SENDER_EMAIL, $this->MSG_SENDER_NAME);

resources/lib/UnityUser.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,6 @@ public function __construct(
3535

3636
public function equals(UnityUser $other_user): bool
3737
{
38-
if (!is_a($other_user, self::class)) {
39-
throw new Exception(
40-
"Unable to check equality because the parameter is not a " .
41-
self::class .
42-
" object",
43-
);
44-
}
45-
4638
return $this->uid == $other_user->uid;
4739
}
4840

@@ -411,18 +403,6 @@ public function hasRequestedAccountDeletion(): bool
411403
*/
412404
public function isInGroup(string $uid, UnityGroup $group): bool
413405
{
414-
if (gettype($group) == "string") {
415-
$group_checked = new UnityGroup(
416-
$group,
417-
$this->LDAP,
418-
$this->SQL,
419-
$this->MAILER,
420-
$this->WEBHOOK,
421-
);
422-
} else {
423-
$group_checked = $group;
424-
}
425-
426-
return in_array($uid, $group_checked->getMemberUIDs());
406+
return in_array($uid, $group->getMemberUIDs());
427407
}
428408
}

resources/lib/UnityWebhook.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ public function sendWebhook(?string $template = null, mixed $data = null): bool
4949
curl_setopt($ch, CURLOPT_POST, 1);
5050
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
5151
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
52-
curl_setopt($ch, CURLOPT_POSTFIELDS, \jsonEncode(["text" => $message]));
52+
curl_setopt(
53+
$ch,
54+
CURLOPT_POSTFIELDS,
55+
\jsonEncode(["subject" => $this->Subject, "text" => $message]),
56+
);
5357
$result = curl_exec($ch);
5458
curl_close($ch);
5559
return $result;

test/functional/PIBecomeApproveTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function testApprovePI()
3636
switchuser(...$user_to_qualify_args);
3737
$pi_group = $USER->getPIGroup();
3838
$this->assertTrue($USER->exists());
39-
$this->assertTrue(!$pi_group->exists());
39+
$this->assertFalse($pi_group->exists());
4040
try {
4141
$this->requestGroupCreation();
4242
$this->assertRequestedPIGroup(true);

test/functional/PiMemberApproveTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function testApproveMemberByPI()
7676
$this->assertTrue($USER->exists());
7777
$this->assertTrue($pi_group->exists());
7878
$this->assertGroupMembers($pi_group, [$pi_uid]);
79-
$this->assertTrue(!$pi_group->memberUIDExists($USER->uid));
79+
$this->assertFalse($pi_group->memberUIDExists($USER->uid));
8080
$this->assertRequestedMembership(false, $gid);
8181
try {
8282
$this->requestGroupMembership($pi_group->gid);
@@ -103,7 +103,7 @@ public function testApproveMemberByPI()
103103
$this->approveUserByPI($approve_uid);
104104
switchUser(...$user_to_approve_args);
105105

106-
$this->assertTrue(!$pi_group->requestExists($USER));
106+
$this->assertFalse($pi_group->requestExists($USER));
107107
$this->assertRequestedMembership(false, $gid);
108108
$this->assertTrue($pi_group->memberUIDExists($USER->uid));
109109
$this->assertTrue($USER->getFlag(UserFlag::QUALIFIED));
@@ -116,7 +116,7 @@ public function testApproveMemberByPI()
116116
// }
117117
// $this->assertTrue($third_request_failed);
118118
$this->assertRequestedMembership(false, $gid);
119-
$this->assertTrue(!$pi_group->requestExists($USER));
119+
$this->assertFalse($pi_group->requestExists($USER));
120120
} finally {
121121
switchUser(...$user_to_approve_args);
122122
ensureUserNotInPIGroup($pi_group);
@@ -136,7 +136,7 @@ public function testApproveMemberByAdmin()
136136
$this->assertTrue($USER->exists());
137137
$this->assertTrue($pi_group->exists());
138138
$this->assertGroupMembers($pi_group, [$pi_uid]);
139-
$this->assertTrue(!$pi_group->memberUIDExists($USER->uid));
139+
$this->assertFalse($pi_group->memberUIDExists($USER->uid));
140140
$this->assertRequestedMembership(false, $gid);
141141
try {
142142
$this->requestGroupMembership($pi_group->gid);
@@ -163,7 +163,7 @@ public function testApproveMemberByAdmin()
163163
$this->approveUserByAdmin($gid, $approve_uid);
164164
switchUser(...$user_to_approve_args);
165165

166-
$this->assertTrue(!$pi_group->requestExists($USER));
166+
$this->assertFalse($pi_group->requestExists($USER));
167167
$this->assertRequestedMembership(false, $gid);
168168
$this->assertTrue($pi_group->memberUIDExists($USER->uid));
169169
$this->assertTrue($USER->getFlag(UserFlag::QUALIFIED));
@@ -176,7 +176,7 @@ public function testApproveMemberByAdmin()
176176
// }
177177
// $this->assertTrue($third_request_failed);
178178
$this->assertRequestedMembership(false, $gid);
179-
$this->assertTrue(!$pi_group->requestExists($USER));
179+
$this->assertFalse($pi_group->requestExists($USER));
180180
} finally {
181181
switchUser(...$user_to_approve_args);
182182
ensureUserNotInPIGroup($pi_group);

test/functional/RegisterUserTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ public function testRegisterUserAndCreateOrg($user_to_register_args, $expected_u
2626
$user_entry = $LDAP->getUserEntry($USER->uid);
2727
$user_group_entry = $LDAP->getGroupEntry($USER->uid);
2828
$org_entry = $LDAP->getOrgGroupEntry($SSO["org"]);
29-
$this->assertTrue(!$user_entry->exists());
30-
$this->assertTrue(!$user_group_entry->exists());
31-
$this->assertTrue(!$org_entry->exists());
29+
$this->assertFalse($user_entry->exists());
30+
$this->assertFalse($user_group_entry->exists());
31+
$this->assertFalse($org_entry->exists());
3232
try {
3333
$this->register();
3434
$this->assertTrue($user_entry->exists());

0 commit comments

Comments
 (0)