Skip to content

Commit fd489c7

Browse files
authored
replace assert with ensure (#315)
* replace assert with ensure * add convention to contributing.md * fix type
1 parent 9d79475 commit fd489c7

File tree

8 files changed

+35
-16
lines changed

8 files changed

+35
-16
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* Comments should be used sparingly.
99
* Empty lines should be used sparingly.
1010
* No code should call `die()` or `exit()`, instead `UnitySite::die()`. This will avoid the premature death of our automated testing processes.
11+
* Instead of `assert`, use `\ensure`. This will enforce conditions even in production.
1112

1213
This repository will automatically check PRs for linting compliance.
1314

resources/autoload.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
require_once __DIR__ . "/lib/exceptions/NoDieException.php";
2929
require_once __DIR__ . "/lib/exceptions/SSOException.php";
3030
require_once __DIR__ . "/lib/exceptions/ArrayKeyException.php";
31+
require_once __DIR__ . "/lib/exceptions/EnsureException.php";
3132

3233
require_once __DIR__ . "/config.php";
3334
require __DIR__ . "/init.php";

resources/lib/UnityGroup.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public function cancelGroupJoinRequest($user, $send_mail = true)
229229
// $users = $this->getGroupMembers();
230230

231231
// // now we delete the ldap entry
232-
// assert($this->entry->exists());
232+
// \ensure($this->entry->exists());
233233
// $this->entry->delete();
234234
// $this->REDIS->removeCacheArray("sorted_groups", "", $this->gid);
235235
// foreach ($users as $user) {
@@ -463,7 +463,7 @@ public function requestExists($user)
463463
private function init()
464464
{
465465
$owner = $this->getOwner();
466-
assert(!$this->entry->exists());
466+
\ensure(!$this->entry->exists());
467467
$nextGID = $this->LDAP->getNextPIGIDNumber();
468468
$this->entry->setAttribute("objectclass", UnityLDAP::POSIX_GROUP_CLASS);
469469
$this->entry->setAttribute("gidnumber", strval($nextGID));

resources/lib/UnityOrg.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct($gid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK)
3030

3131
public function init()
3232
{
33-
assert(!$this->entry->exists());
33+
\ensure(!$this->entry->exists());
3434
$nextGID = $this->LDAP->getNextOrgGIDNumber($this->SQL);
3535
$this->entry->setAttribute("objectclass", UnityLDAP::POSIX_GROUP_CLASS);
3636
$this->entry->setAttribute("gidnumber", strval($nextGID));

resources/lib/UnityUser.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ public function init($firstname, $lastname, $email, $org, $send_mail = true)
6161
{
6262
$ldapGroupEntry = $this->getGroupEntry();
6363
$id = $this->LDAP->getNextUIDGIDNumber($this->uid);
64-
assert(!$ldapGroupEntry->exists());
64+
\ensure(!$ldapGroupEntry->exists());
6565
$ldapGroupEntry->setAttribute("objectclass", UnityLDAP::POSIX_GROUP_CLASS);
6666
$ldapGroupEntry->setAttribute("gidnumber", strval($id));
6767
$ldapGroupEntry->write();
6868

69-
assert(!$this->entry->exists());
69+
\ensure(!$this->entry->exists());
7070
$this->entry->setAttribute("objectclass", UnityLDAP::POSIX_ACCOUNT_CLASS);
7171
$this->entry->setAttribute("uid", $this->uid);
7272
$this->entry->setAttribute("givenname", $firstname);
@@ -145,7 +145,7 @@ public function setOrg($org)
145145

146146
public function getOrg($ignorecache = false)
147147
{
148-
assert($this->exists());
148+
\ensure($this->exists());
149149
if (!$ignorecache) {
150150
$cached_val = $this->REDIS->getCache($this->uid, "org");
151151
if (!is_null($cached_val)) {
@@ -194,7 +194,7 @@ public function setFirstname($firstname, $operator = null)
194194
*/
195195
public function getFirstname($ignorecache = false)
196196
{
197-
assert($this->exists());
197+
\ensure($this->exists());
198198
if (!$ignorecache) {
199199
$cached_val = $this->REDIS->getCache($this->uid, "firstname");
200200
if (!is_null($cached_val)) {
@@ -243,7 +243,7 @@ public function setLastname($lastname, $operator = null)
243243
*/
244244
public function getLastname($ignorecache = false)
245245
{
246-
assert($this->exists());
246+
\ensure($this->exists());
247247
if (!$ignorecache) {
248248
$cached_val = $this->REDIS->getCache($this->uid, "lastname");
249249
if (!is_null($cached_val)) {
@@ -266,7 +266,7 @@ public function getLastname($ignorecache = false)
266266

267267
public function getFullname()
268268
{
269-
assert($this->exists());
269+
\ensure($this->exists());
270270
return $this->getFirstname() . " " . $this->getLastname();
271271
}
272272

@@ -298,7 +298,7 @@ public function setMail($email, $operator = null)
298298
*/
299299
public function getMail($ignorecache = false)
300300
{
301-
assert($this->exists());
301+
\ensure($this->exists());
302302
if (!$ignorecache) {
303303
$cached_val = $this->REDIS->getCache($this->uid, "mail");
304304
if (!is_null($cached_val)) {
@@ -328,7 +328,7 @@ public function setSSHKeys($keys, $operator = null, $send_mail = true)
328328
{
329329
$operator = is_null($operator) ? $this->uid : $operator->uid;
330330
$keys_filt = array_values(array_unique($keys));
331-
assert($this->entry->exists());
331+
\ensure($this->entry->exists());
332332
$this->entry->setAttribute("sshpublickey", $keys_filt);
333333
$this->entry->write();
334334

@@ -357,7 +357,7 @@ public function setSSHKeys($keys, $operator = null, $send_mail = true)
357357
*/
358358
public function getSSHKeys($ignorecache = false)
359359
{
360-
assert($this->exists());
360+
\ensure($this->exists());
361361
if (!$ignorecache) {
362362
$cached_val = $this->REDIS->getCache($this->uid, "sshkeys");
363363
if (!is_null($cached_val)) {
@@ -400,7 +400,7 @@ public function setLoginShell($shell, $operator = null, $send_mail = true)
400400
if (empty($shell)) {
401401
throw new Exception("login shell must not be empty!");
402402
}
403-
assert($this->entry->exists());
403+
\ensure($this->entry->exists());
404404
$this->entry->setAttribute("loginshell", $shell);
405405
$this->entry->write();
406406

@@ -431,7 +431,7 @@ public function setLoginShell($shell, $operator = null, $send_mail = true)
431431
*/
432432
public function getLoginShell($ignorecache = false)
433433
{
434-
assert($this->exists());
434+
\ensure($this->exists());
435435
if (!$ignorecache) {
436436
$cached_val = $this->REDIS->getCache($this->uid, "loginshell");
437437
if (!is_null($cached_val)) {
@@ -454,7 +454,7 @@ public function getLoginShell($ignorecache = false)
454454

455455
public function setHomeDir($home, $operator = null)
456456
{
457-
assert($this->entry->exists());
457+
\ensure($this->entry->exists());
458458
$this->entry->setAttribute("homedirectory", $home);
459459
$this->entry->write();
460460
$operator = is_null($operator) ? $this->uid : $operator->uid;
@@ -476,7 +476,7 @@ public function setHomeDir($home, $operator = null)
476476
*/
477477
public function getHomeDir($ignorecache = false)
478478
{
479-
assert($this->exists());
479+
\ensure($this->exists());
480480
if (!$ignorecache) {
481481
$cached_val = $this->REDIS->getCache($this->uid, "homedir");
482482
if (!is_null($cached_val)) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace UnityWebPortal\lib\exceptions;
4+
5+
class EnsureException extends \Exception
6+
{
7+
}

resources/lib/utils.php

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

33
use UnityWebPortal\lib\exceptions\ArrayKeyException;
4+
use UnityWebPortal\lib\exceptions\EnsureException;
45

56
function arrayGet($array, ...$keys)
67
{
@@ -19,3 +20,11 @@ function arrayGet($array, ...$keys)
1920
}
2021
return $cursor;
2122
}
23+
24+
// like assert() but not subject to zend.assertions config
25+
function ensure(bool $condition, ?string $message = null)
26+
{
27+
if (!$condition) {
28+
throw new EnsureException($message ?? "ensure condition is false");
29+
}
30+
}

test/phpunit-bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
require_once __DIR__ . "/../resources/lib/exceptions/NoDieException.php";
2020
require_once __DIR__ . "/../resources/lib/exceptions/SSOException.php";
2121
require_once __DIR__ . "/../resources/lib/exceptions/ArrayKeyException.php";
22+
require_once __DIR__ . "/../resources/lib/exceptions/EnsureException.php";
2223

2324
$_SERVER["HTTP_HOST"] = "phpunit"; // used for config override
2425
require_once __DIR__ . "/../resources/config.php";

0 commit comments

Comments
 (0)