Skip to content

Commit 3d4bb3c

Browse files
committed
refactor: ♻️ General refactor
1 parent 09ab582 commit 3d4bb3c

File tree

15 files changed

+60
-99
lines changed

15 files changed

+60
-99
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"php": ">=8.0",
2929
"ext-json": "*",
3030
"delight-im/cookie": "^3",
31-
"illuminate/support": "^8|^9|^10",
31+
"illuminate/support": ">=8",
3232
"maicol07/flarum-api-client": "^1",
3333
"mistic100/php-hooks": "^0"
3434
},

example/delete.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
<?php /** @noinspection DuplicatedCode */
22

3-
use Dotenv\Dotenv;
4-
53
// Note: Since this is called from the example folder, the vendor folder is located in the previous tree level
64
require_once __DIR__ . '/../vendor/autoload.php';
7-
8-
// Load .env
9-
$env = Dotenv::createImmutable(__DIR__);
10-
$env->load();
11-
125
require_once __DIR__ . '/flarum.php';
136
/** @var $flarum <-- Fix PHPStorm hints */
147

example/flarum.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
// Create the Flarum object with the required configuration. The parameters are explained in the class file (src/Flarum.php)
33
use Maicol07\SSO\Flarum;
44

5+
/** @noinspection DevelopmentDependenciesUsageInspection */
6+
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
7+
$dotenv->load();
8+
9+
/** @noinspection LaravelFunctionsInspection */
510
$flarum = new Flarum([
611
'url' => env('FLARUM_HOST', 'https://discuss.flarum.org'),
712
'root_domain' => env('ROOT_DOMAIN', 'flarum.org'),

example/index.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
<?php /** @noinspection ForgottenDebugOutputInspection */
22

3-
use Dotenv\Dotenv;
43
use Illuminate\Support\Arr;
54
use Maicol07\SSO\Addons\Groups;
65

76
// Note: Since this is called from the example folder, the vendor folder is located in the previous tree level
87
require_once __DIR__ . '/../vendor/autoload.php';
98

10-
// Load .env
11-
$env = Dotenv::createImmutable(__DIR__);
12-
$env->load();
13-
149
// Dummy users
1510
$users = [
1611
'user' => [

example/logout.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
<?php /** @noinspection DuplicatedCode */
22

3-
use Dotenv\Dotenv;
4-
53
// Note: Since this is called from the example folder, the vendor folder is located in the previous tree level
64
require_once __DIR__ . '/../vendor/autoload.php';
7-
8-
// Load .env
9-
$env = Dotenv::createImmutable(__DIR__);
10-
$env->load();
11-
125
require_once __DIR__ . '/flarum.php';
136
/** @var $flarum <-- Fix PHPStorm hints */
147

@@ -48,4 +41,4 @@
4841

4942
<?php require_once 'footer.php' ?>
5043
</body>
51-
</html>
44+
</html>

example/update.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
<?php /** @noinspection ForgottenDebugOutputInspection */
22

3-
use Dotenv\Dotenv;
43
use Illuminate\Support\Arr;
54
use Maicol07\SSO\Addons\Groups;
65

76
// Note: Since this is called from the example folder, the vendor folder is located in the previous tree level
87
require_once __DIR__ . '/../vendor/autoload.php';
98

10-
// Load .env
11-
$env = Dotenv::createImmutable(__DIR__);
12-
$env->load();
13-
149
if (Arr::exists($_POST, 'username')) {
1510
require_once __DIR__ . '/flarum.php';
1611
/** @var $flarum <-- Fix PHPStorm hints */

src/Addons/Core.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
*/
1212
class Core
1313
{
14-
/** @var Hooks */
15-
protected $hooks;
16-
1714
/** @var array Actions list */
1815
protected $actions = [];
1916

@@ -23,20 +20,13 @@ class Core
2320
/** @var array Required addons that needs to be loaded before this one */
2421
protected $required = [];
2522

26-
/** @var Flarum */
27-
protected $flarum;
28-
29-
public function __construct(Hooks $hooks, Flarum $flarum)
23+
public function __construct(protected \Hooks\Hooks $hooks, protected \Maicol07\SSO\Flarum $flarum)
3024
{
31-
$this->flarum = $flarum;
32-
$this->hooks = $hooks;
3325
$this->load();
3426
}
3527

3628
/**
3729
* Load Addons hooks. If the addons require other addons loaded before it, then it will raise an exception
38-
*
39-
* @return $this
4030
*/
4131
public function load(): Core
4232
{
@@ -47,7 +37,8 @@ public function load(): Core
4737
$required[] = $addon;
4838
}
4939
}
50-
if (!empty($required)) {
40+
41+
if ($required !== []) {
5142
throw new MissingRequiredAddonException('Following required addons not loaded: ' . implode(', ', $required) . '. You need to load it/them to use this addon');
5243
}
5344

@@ -66,16 +57,14 @@ private function manageHooks(string $op): void
6657
$type = in_array($method, $this->actions, true) ? 'action' : 'filter';
6758
$methods = is_array($method) ? $method : [$method];
6859

69-
foreach ($methods as $m) {
70-
$this->hooks->{"{$op}_$type"}($name, [$this, $m]);
60+
foreach ($methods as $method) {
61+
$this->hooks->{"{$op}_$type"}($name, [$this, $method]);
7162
}
7263
}
7364
}
7465

7566
/**
7667
* Unload Addons hooks
77-
*
78-
* @return $this
7968
*/
8069
public function unload(): Core
8170
{

src/Addons/Groups.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Groups extends Core
2121
public function setGroups(): void
2222
{
2323
$user = $this->flarum->user();
24-
if (!empty($user->id)) {
24+
if ($user->id !== null && $user->id !== 0) {
2525
$groups = [];
2626

2727
/** Search flarum groups - @noinspection NullPointerExceptionInspection */
@@ -32,16 +32,14 @@ public function setGroups(): void
3232
);
3333

3434
foreach ($user->relationships->groups as $group) {
35-
if (empty($group) or !is_string($group)) {
35+
if (empty($group) || !is_string($group)) {
3636
continue;
3737
}
3838

3939
// Find ID of the group
40-
$id = array_key_first(Arr::where($flarum_groups, function ($name) use ($group) {
41-
return $name === $group;
42-
}));
40+
$id = array_key_first(Arr::where($flarum_groups, static fn($name): bool => $name === $group));
4341
// If it doesn't exists, create it
44-
if (empty($id)) {
42+
if ($id === 0 || $id === '' || $id === null) {
4543
$id = $this->createGroup($group);
4644
}
4745

@@ -64,10 +62,8 @@ public function setGroups(): void
6462
/**
6563
* Add a group to Flarum
6664
*
67-
* @param string $group
6865
*
6966
* @return mixed
70-
*
7167
* @noinspection MissingReturnTypeInspection
7268
*/
7369
public function createGroup(string $group)

src/Flarum.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
*/
1818
class Flarum
1919
{
20-
use Cookies, Addons;
21-
20+
use Cookies;
21+
use Addons;
2222
/* @var Client Api client */
23+
/**
24+
* @var \Maicol07\Flarum\Api\Client
25+
*/
2326
public $api;
2427

2528
/* @var bool Should the login be remembered (this equals to 5 years remember from last usage)? If false, token will be remembered only for 1 hour */
@@ -40,8 +43,7 @@ class Flarum
4043
/** @var string */
4144
public $cookies_prefix;
4245

43-
/** @var User|null */
44-
private $user;
46+
private ?\Maicol07\SSO\User $user = null;
4547

4648
/**
4749
* Flarum constructor
@@ -96,24 +98,24 @@ public function __construct(array $config)
9698
/**
9799
* Logs out the current user from Flarum. Generally, you should use this method when an user successfully logged out from
98100
* your SSO system (or main website)
99-
*
100-
* @return bool
101101
*/
102102
public function logout(): bool
103103
{
104104
$this->action_hook('before_logout');
105105

106-
$deleted = $this->deleteSessionTokenCookie() and $this->deleteRememberTokenCookie();
106+
if ($deleted = $this->deleteSessionTokenCookie()) {
107+
$this->deleteRememberTokenCookie();
108+
}
107109
$created = $this->setLogoutCookie();
108110

109111
$this->hooks->do_action('after_logout', $deleted, $created);
110112

111-
return ($deleted and $created);
113+
return ($deleted && $created);
112114
}
113115

114116
public function user(string $username = null): User
115117
{
116-
if ($this->user === null) {
118+
if (!$this->user instanceof \Maicol07\SSO\User) {
117119
$this->user = new User($username, $this);
118120
}
119121

@@ -136,7 +138,6 @@ public function user(string $username = null): User
136138
*
137139
* There could be more if you have other extensions that adds them to Flarum API
138140
*
139-
* @return Collection
140141
*
141142
* @noinspection MissingParameterTypeDeclarationInspection
142143
*/
@@ -147,7 +148,7 @@ public function getUsersList($filters = null): Collection
147148

148149
while ($offset !== null) {
149150
$response = $this->api->users()->offset($offset)->request();
150-
if ($response instanceof Item and empty($response->type)) {
151+
if ($response instanceof Item && $response->type === '') {
151152
$offset = null;
152153
continue;
153154
}
@@ -158,7 +159,7 @@ public function getUsersList($filters = null): Collection
158159

159160
// Filters
160161
$filtered = collect();
161-
if (!empty($filters)) {
162+
if ($filters !== '' && $filters !== []) {
162163
$grouped = true;
163164
if (is_string($filters)) {
164165
$filters = [$filters];
@@ -167,11 +168,13 @@ public function getUsersList($filters = null): Collection
167168

168169
foreach ($filters as $filter) {
169170
$plucked = $collection->pluck($filter);
170-
if (!empty($grouped)) {
171+
if ($grouped) {
171172
$plucked = [$filter => $plucked];
172173
}
174+
173175
$filtered = $filtered->mergeRecursive($plucked);
174176
}
177+
175178
$collection = $filtered;
176179
}
177180

@@ -193,6 +196,7 @@ public function isSessionRemembered(bool $remember = null)
193196
$this->remember = $remember;
194197
return;
195198
}
199+
196200
return $this->remember;
197201
}
198202

src/Traits/Addons.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ trait Addons
1717
* Load an addon to the plugin
1818
*
1919
* @param string $addon Class name to add as addon
20-
* @return int
2120
*/
2221
public function loadAddon(string $addon): int
2322
{
@@ -43,8 +42,6 @@ public function unloadAddon(string $addon): self
4342
/**
4443
* Set addon properties
4544
*
46-
* @param string $addon
47-
* @param array $attributes
4845
* @return $this
4946
*/
5047
public function setAddonProperties(string $addon, array $attributes): self
@@ -53,14 +50,12 @@ public function setAddonProperties(string $addon, array $attributes): self
5350
foreach ($attributes as $key => $value) {
5451
$hook->$key = $value;
5552
}
53+
5654
return $this;
5755
}
5856

5957
/**
6058
* Check if addon is loaded
61-
*
62-
* @param string $addon
63-
* @return bool
6459
*/
6560
public function isAddonLoaded(string $addon): bool
6661
{
@@ -69,9 +64,6 @@ public function isAddonLoaded(string $addon): bool
6964

7065
/**
7166
* A simple proxy to Hook do_action function
72-
*
73-
* @param string $tag
74-
* @return int|null
7567
*/
7668
public function action_hook(string $tag): ?int
7769
{
@@ -81,14 +73,14 @@ public function action_hook(string $tag): ?int
8173
if (!$this->hooks->has_action($tag)) {
8274
return -1;
8375
}
76+
8477
$this->hooks->do_action($tag, $args);
8578
return null;
8679
}
8780

8881
/**
8982
* A simple proxy to Hook apply_filters function
9083
*
91-
* @param string $tag
9284
* @param $value
9385
*
9486
* @return mixed
@@ -101,6 +93,7 @@ public function filter_hook(string $tag, $value)
10193
if (!$this->hooks->has_filter($tag)) {
10294
return -1;
10395
}
96+
10497
return $this->hooks->apply_filters($tag, $value);
10598
}
10699

0 commit comments

Comments
 (0)