Skip to content

Commit ec96ea5

Browse files
authored
Merge pull request #47 from fadrian06/require-php80
Require PHP 8.0 for compatibility with firebase/php-jwt
2 parents 05d31d3 + f18aa3d commit ec96ea5

6 files changed

Lines changed: 35 additions & 25 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"minimum-stability": "stable",
3333
"prefer-stable": true,
3434
"require": {
35+
"php": ">=8.0",
3536
"leafs/date": "*",
3637
"leafs/password": "*",
3738
"leafs/session": "*",

rector.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
use Rector\Config\RectorConfig;
66

77
return RectorConfig::configure()
8-
->withDowngradeSets(false, false, false, false, false, true)
8+
->withDowngradeSets(php80: true)
99
->withFluentCallNewLine()
10-
->withImportNames(true, true, true, true)
10+
->withImportNames(
11+
importNames: true,
12+
importDocBlockNames: true,
13+
importShortClasses: true,
14+
removeUnusedImports: true,
15+
)
1116
->withPaths([
1217
__DIR__ . '/src',
1318
__DIR__ . '/tests',

src/Auth.php

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

33
namespace Leaf;
44

5+
use League\OAuth2\Client\Provider\AbstractProvider;
56
use Exception;
67
use Firebase\JWT\JWT;
78
use Firebase\JWT\Key;
@@ -43,7 +44,7 @@ class Auth
4344
*/
4445
protected $errorsArray = [];
4546

46-
/** @var array<string, \League\OAuth2\Client\Provider\AbstractProvider> Configured oauth clients */
47+
/** @var array<string, AbstractProvider> Configured oauth clients */
4748
protected $oauthClients = [];
4849

4950
public function __construct()
@@ -219,7 +220,7 @@ public function withGoogle(
219220
* ---
220221
* Register a generic OAuth client to use with Leaf Auth, should be a league/oauth2-client compatible client.
221222
* @param string $clientName The name of the client to register
222-
* @param \League\OAuth2\Client\Provider\AbstractProvider $client An instance of a league/oauth2-client compatible client
223+
* @param AbstractProvider $client An instance of a league/oauth2-client compatible client
223224
* @return static
224225
*/
225226
public function withProvider(string $clientName, $client)
@@ -231,7 +232,7 @@ public function withProvider(string $clientName, $client)
231232
/**
232233
* Return an oauth client
233234
* @param string $clientName The name of the client to return
234-
* @return \League\OAuth2\Client\Provider\AbstractProvider|null
235+
* @return AbstractProvider|null
235236
*/
236237
public function client(string $clientName)
237238
{
@@ -587,7 +588,7 @@ public function find($id)
587588
*
588589
* @param array<string, mixed> $userData The user details to save
589590
* @return User|false|never
590-
* @throws \Exception If database connection is not established
591+
* @throws Exception If database connection is not established
591592
*/
592593
public function createUserFor($userData)
593594
{
@@ -772,7 +773,7 @@ public function tokens()
772773
* @param string $middleware The middleware to register
773774
* @param callable $callback The callback to run if middleware fails
774775
* @return void|never
775-
* @throws \Exception If not used with leafs/leaf installed
776+
* @throws Exception If not used with leafs/leaf installed
776777
*/
777778
public function middleware(string $middleware, callable $callback)
778779
{
@@ -976,7 +977,7 @@ protected function getFromSession($value)
976977

977978
/**
978979
* @return void|never
979-
* @throws \Exception If sessions are not enabled
980+
* @throws Exception If sessions are not enabled
980981
*/
981982
protected function sessionCheck()
982983
{

src/Auth/Model.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,7 @@ class Model
3535
protected array $dataToSave = [];
3636

3737
/**
38-
* @param array{
39-
* db: \Leaf\Db,
40-
* user: User,
41-
* table: string,
42-
* } $data
38+
* @param array{db: Db, user: User, table: string} $data
4339
*/
4440
public function __construct($data)
4541
{

src/Auth/User.php

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

33
namespace Leaf\Auth;
44

5+
use Exception;
6+
use Throwable;
7+
use ReturnTypeWillChange;
8+
use Leaf\Db;
59
use Firebase\JWT\JWT;
610
use Leaf\Date;
711
use Leaf\Helpers\Password;
@@ -22,7 +26,7 @@ class User
2226

2327
/**
2428
* Internal instance of Leaf database
25-
* @var \Leaf\Db
29+
* @var Db
2630
*/
2731
protected $db;
2832

@@ -82,7 +86,7 @@ public function __construct($data, $session = true)
8286
$sessionLifetime = strtotime($sessionLifetime);
8387

8488
if (!$sessionLifetime) {
85-
throw new \Exception('Invalid session lifetime');
89+
throw new Exception('Invalid session lifetime');
8690
}
8791
} else {
8892
$sessionLifetime = time() + $sessionLifetime;
@@ -165,8 +169,8 @@ public function update(array $userData): bool
165169
$this->errorsArray = array_merge($this->errorsArray, $this->db->errors());
166170
return false;
167171
}
168-
} catch (\Throwable $th) {
169-
throw new \Exception($th->getMessage());
172+
} catch (Throwable $th) {
173+
throw new Exception($th->getMessage());
170174
}
171175

172176
if (Config::get('session')) {
@@ -226,8 +230,8 @@ public function updatePassword(string $oldPassword, string $newPassword): bool
226230
$this->errorsArray = array_merge($this->errorsArray, $this->db->errors());
227231
return false;
228232
}
229-
} catch (\Throwable $th) {
230-
throw new \Exception($th->getMessage());
233+
} catch (Throwable $th) {
234+
throw new Exception($th->getMessage());
231235
}
232236

233237
$this->data[$passwordKey] = $newPassword;
@@ -268,8 +272,8 @@ public function resetPassword(string $newPassword): bool
268272
$this->errorsArray = array_merge($this->errorsArray, $this->db->errors());
269273
return false;
270274
}
271-
} catch (\Throwable $th) {
272-
throw new \Exception($th->getMessage());
275+
} catch (Throwable $th) {
276+
throw new Exception($th->getMessage());
273277
}
274278

275279
$this->data[$passwordKey] = $newPassword;
@@ -390,7 +394,7 @@ public function verifyEmail(): bool
390394
->execute();
391395

392396
return true;
393-
} catch (\Throwable $th) {
397+
} catch (Throwable $th) {
394398
return false;
395399
}
396400
}
@@ -425,7 +429,7 @@ public function get()
425429

426430
/**
427431
* Set user db instance
428-
* @param \Leaf\Db $db
432+
* @param Db $db
429433
* @return User
430434
*/
431435
public function setDb($db)
@@ -444,6 +448,7 @@ public function errors()
444448
return $this->errorsArray;
445449
}
446450

451+
#[ReturnTypeWillChange]
447452
public function __toString()
448453
{
449454
return json_encode($this->get()) ?: '';
@@ -501,14 +506,14 @@ public function __unset($name)
501506
*
502507
* @param mixed $method The table to relate to
503508
* @param mixed $args
504-
* @throws \Exception
509+
* @throws Exception
505510
*
506511
* @return Model
507512
*/
508513
public function __call($method, $args)
509514
{
510515
if (!class_exists('Leaf\App')) {
511-
throw new \Exception('Relations are only available in Leaf apps.');
516+
throw new Exception('Relations are only available in Leaf apps.');
512517
}
513518

514519
return (new Model([

tests/Pest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Leaf\Auth;
44
use Leaf\Db;
5+
use Leaf\Helpers\Password;
56

67
dataset('test-user', [[[
78
'username' => 'test-user',
@@ -44,6 +45,7 @@ function authInstance(): Auth
4445
{
4546
$auth = new Auth();
4647
$auth->dbConnection(dbInstance()->connection());
48+
$auth->config('token.secret', Password::hash(uniqid()));
4749

4850
return $auth;
4951
}

0 commit comments

Comments
 (0)