Skip to content

Commit 711445a

Browse files
authored
Merge branch 'v4.x' into chore/add-phpstan-for-static-analysis
2 parents 1fc0c86 + 78a0374 commit 711445a

File tree

13 files changed

+295
-73
lines changed

13 files changed

+295
-73
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@
3636
"leafs/password": "*",
3737
"leafs/session": "*",
3838
"leafs/db": "*",
39-
"leafs/form": "*",
4039
"leafs/http": "*",
41-
"firebase/php-jwt": "^6.10"
40+
"firebase/php-jwt": "^7.0"
4241
},
4342
"config": {
4443
"allow-plugins": {
@@ -55,6 +54,7 @@
5554
"leafs/leaf": "^4.4",
5655
"league/oauth2-google": "^4.0",
5756
"leafs/billing": "^0.2.0"
57+
"rector/rector": "^2.2"
5858
},
5959
"scripts": {
6060
"alchemy": "./vendor/bin/alchemy setup",

rector.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
7+
return RectorConfig::configure()
8+
->withDowngradeSets(false, false, false, false, false, true)
9+
->withFluentCallNewLine()
10+
->withImportNames(true, true, true, true)
11+
->withPaths([
12+
__DIR__ . '/src',
13+
__DIR__ . '/tests',
14+
])
15+
;

src/Auth.php

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
namespace Leaf;
44

5+
use Exception;
56
use Firebase\JWT\JWT;
67
use Firebase\JWT\Key;
78
use Leaf\Auth\Config;
89
use Leaf\Auth\User;
10+
use Leaf\Exception\General;
911
use Leaf\Helpers\Password;
1012
use Leaf\Http\Session;
13+
use League\OAuth2\Client\Provider\Google;
14+
use PDO;
15+
use Throwable;
1116

1217
/**
1318
* Leaf Simple Auth
@@ -53,31 +58,31 @@ public function __construct()
5358
});
5459

5560
$this->middleware('is', function ($role) {
56-
\Leaf\Exception\General::error(
61+
General::error(
5762
'404',
5863
'<p>The page you are looking for could not be found.</p>',
5964
403
6065
);
6166
});
6267

6368
$this->middleware('isNot', function () {
64-
\Leaf\Exception\General::error(
69+
General::error(
6570
'404',
6671
'<p>The page you are looking for could not be found.</p>',
6772
403
6873
);
6974
});
7075

7176
$this->middleware('can', function () {
72-
\Leaf\Exception\General::error(
77+
General::error(
7378
'404',
7479
'<p>The page you are looking for could not be found.</p>',
7580
403
7681
);
7782
});
7883

7984
$this->middleware('cannot', function () {
80-
\Leaf\Exception\General::error(
85+
General::error(
8186
'404',
8287
'<p>The page you are looking for could not be found.</p>',
8388
403
@@ -146,10 +151,10 @@ public function autoConnect(array $pdoOptions = [])
146151
/**
147152
* Pass in db connection instance directly
148153
*
149-
* @param \PDO $connection A connection instance of your db
150-
* @return $this
154+
* @param PDO $connection A connection instance of your db
155+
* @return $this;
151156
*/
152-
public function dbConnection(\PDO $connection)
157+
public function dbConnection(PDO $connection)
153158
{
154159
$this->db = new Db();
155160
$this->db->connection($connection);
@@ -182,7 +187,7 @@ public function withGoogle(
182187
$options['redirectUri'] = _env('APP_URL') . '/auth/google/callback';
183188
}
184189

185-
$this->withProvider($clientName, new \League\OAuth2\Client\Provider\Google(array_merge([
190+
$this->withProvider($clientName, new Google(array_merge([
186191
'clientId' => $clientId,
187192
'clientSecret' => $clientSecret,
188193
'redirectUri' => $options['redirectUri'],
@@ -292,8 +297,8 @@ public function login(array $credentials): bool
292297
$this->errorsArray['auth'] = Config::get('messages.loginParamsError');
293298
return false;
294299
}
295-
} catch (\Throwable $th) {
296-
throw new \Exception($th->getMessage());
300+
} catch (Throwable $th) {
301+
throw new Exception($th->getMessage());
297302
}
298303

299304
if ($passwordKey !== false) {
@@ -346,7 +351,7 @@ public function register(array $userData): bool
346351
$userData['email'] = strtolower($userData['email']);
347352
}
348353

349-
if (isset($credentials[Config::get('id.key')])) {
354+
if (isset($userData[Config::get('id.key')])) {
350355
$userData[Config::get('id.key')] = is_callable($userData[Config::get('id.key')])
351356
? call_user_func($userData[Config::get('id.key')])
352357
: $userData[Config::get('id.key')];
@@ -359,8 +364,8 @@ public function register(array $userData): bool
359364
$this->errorsArray = array_merge($this->errorsArray, $this->db->errors());
360365
return false;
361366
}
362-
} catch (\Throwable $th) {
363-
throw new \Exception($th->getMessage());
367+
} catch (Throwable $th) {
368+
throw new Exception($th->getMessage());
364369
}
365370

366371
$user = $this->db->select($table)->where($userData)->first();
@@ -429,8 +434,8 @@ public function update(array $userData): bool
429434
$this->errorsArray = array_merge($this->errorsArray, $this->db->errors());
430435
return false;
431436
}
432-
} catch (\Throwable $th) {
433-
throw new \Exception($th->getMessage());
437+
} catch (Throwable $th) {
438+
throw new Exception($th->getMessage());
434439
}
435440

436441
if (Config::get('session')) {
@@ -492,8 +497,8 @@ public function updatePassword(string $oldPassword, string $newPassword): bool
492497
$this->errorsArray = array_merge($this->errorsArray, $this->db->errors());
493498
return false;
494499
}
495-
} catch (\Throwable $th) {
496-
throw new \Exception($th->getMessage());
500+
} catch (Throwable $th) {
501+
throw new Exception($th->getMessage());
497502
}
498503

499504
$this->user->{$passwordKey} = $newPassword;
@@ -590,7 +595,7 @@ public function createUserFor($userData)
590595
$userData['email'] = strtolower($userData['email']);
591596
}
592597

593-
if (isset($credentials[Config::get('id.key')])) {
598+
if (isset($userData[Config::get('id.key')])) {
594599
$userData[Config::get('id.key')] = is_callable($userData[Config::get('id.key')])
595600
? call_user_func($userData[Config::get('id.key')])
596601
: $userData[Config::get('id.key')];
@@ -603,8 +608,8 @@ public function createUserFor($userData)
603608
$this->errorsArray = array_merge($this->errorsArray, $this->db->errors());
604609
return false;
605610
}
606-
} catch (\Throwable $th) {
607-
throw new \Exception($th->getMessage());
611+
} catch (Throwable $th) {
612+
throw new Exception($th->getMessage());
608613
}
609614

610615
$user = $this->db->select($table)->where($userData)->first();
@@ -702,8 +707,8 @@ public function user()
702707
$this->errorsArray = $this->db->errors();
703708
return null;
704709
}
705-
} catch (\Throwable $th) {
706-
throw new \Exception($th->getMessage());
710+
} catch (Throwable $th) {
711+
throw new Exception($th->getMessage());
707712
}
708713

709714
return $this->user = (new User(
@@ -753,8 +758,8 @@ public function tokens()
753758
*/
754759
public function middleware(string $middleware, callable $callback)
755760
{
756-
if (!class_exists(\Leaf\App::class)) {
757-
throw new \Exception('This feature is only available for Leaf apps');
761+
if (!class_exists(App::class)) {
762+
throw new Exception('This feature is only available for Leaf apps');
758763
}
759764

760765
if ($middleware === 'auth.required') {
@@ -867,7 +872,7 @@ public function parseToken()
867872
$bearerToken,
868873
new Key(Config::get('token.secret'), 'HS256')
869874
);
870-
} catch (\Throwable $th) {
875+
} catch (Throwable $th) {
871876
$this->errorsArray['token'] = $th->getMessage();
872877
return null;
873878
}
@@ -909,7 +914,7 @@ public function verifyToken(string $token, ?string $purpose = null)
909914
}
910915

911916
return $user;
912-
} catch (\Throwable $th) {
917+
} catch (Throwable $th) {
913918
$this->errorsArray['token'] = $th->getMessage();
914919
return null;
915920
}
@@ -928,13 +933,13 @@ public function db()
928933
protected function checkDbConnection(): void
929934
{
930935
if (!$this->db && function_exists('db')) {
931-
if (db()->connection() instanceof \PDO || db()->autoConnect()) {
936+
if (db()->connection() instanceof PDO || db()->autoConnect()) {
932937
$this->db = db();
933938
}
934939
}
935940

936941
if (!$this->db) {
937-
throw new \Exception('You need to connect to your database first');
942+
throw new Exception('You need to connect to your database first');
938943
}
939944
}
940945

@@ -958,7 +963,7 @@ protected function getFromSession($value)
958963
protected function sessionCheck()
959964
{
960965
if (!Config::get('session')) {
961-
throw new \Exception('Turn on sessions to use this feature.');
966+
throw new Exception('Turn on sessions to use this feature.');
962967
}
963968
}
964969

src/Auth/Model.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Leaf\Auth;
44

5+
use Leaf\Date;
6+
use Leaf\Db;
57
use PDOStatement;
68

79
/**
@@ -27,7 +29,7 @@ class Model
2729
/**
2830
* DB connection
2931
*/
30-
protected \Leaf\Db $db;
32+
protected Db $db;
3133

3234
/** @var array<string, mixed> User data to save */
3335
protected array $dataToSave = [];
@@ -58,7 +60,7 @@ public function create(array $data): ?PDOStatement
5860
$data['user_id'] = $this->user->id();
5961

6062
if (Config::get('timestamps')) {
61-
$now = (new \Leaf\Date())->tick()->format(Config::get('timestamps.format'));
63+
$now = (new Date())->tick()->format(Config::get('timestamps.format'));
6264
$data['created_at'] = $now;
6365
$data['updated_at'] = $now;
6466
}
@@ -71,11 +73,11 @@ public function create(array $data): ?PDOStatement
7173
*
7274
* @param array<string, mixed> $data Data to be updated
7375
*
74-
* @return \Leaf\Db
76+
* @return Db
7577
*/
76-
public function update(array $data): \Leaf\Db
78+
public function update(array $data): Db
7779
{
78-
$data['updated_at'] = (new \Leaf\Date())->tick()->format(Config::get('timestamps.format'));
80+
$data['updated_at'] = (new Date())->tick()->format(Config::get('timestamps.format'));
7981

8082
return $this->db->update($this->table)
8183
->params($data)
@@ -85,9 +87,9 @@ public function update(array $data): \Leaf\Db
8587
/**
8688
* Delete a model resource
8789
*
88-
* @return \Leaf\Db
90+
* @return Db
8991
*/
90-
public function delete(): \Leaf\Db
92+
public function delete(): Db
9193
{
9294
return $this->db->delete($this->table)
9395
->where('user_id', $this->user->id());
@@ -98,9 +100,9 @@ public function delete(): \Leaf\Db
98100
*
99101
* @param string $columns Columns to search by separated by commas or *
100102
*
101-
* @return \Leaf\Db
103+
* @return Db
102104
*/
103-
public function table($columns = '*'): \Leaf\Db
105+
public function table($columns = '*'): Db
104106
{
105107
return $this->db->select($this->table, $columns)
106108
->where('user_id', $this->user->id());

0 commit comments

Comments
 (0)