diff --git a/composer.json b/composer.json index 13676ea..5bd6dea 100644 --- a/composer.json +++ b/composer.json @@ -32,6 +32,7 @@ "minimum-stability": "stable", "prefer-stable": true, "require": { + "php": ">=8.0", "leafs/date": "*", "leafs/password": "*", "leafs/session": "*", diff --git a/rector.php b/rector.php index d58d7a0..d96bf3c 100644 --- a/rector.php +++ b/rector.php @@ -5,9 +5,14 @@ use Rector\Config\RectorConfig; return RectorConfig::configure() - ->withDowngradeSets(false, false, false, false, false, true) + ->withDowngradeSets(php80: true) ->withFluentCallNewLine() - ->withImportNames(true, true, true, true) + ->withImportNames( + importNames: true, + importDocBlockNames: true, + importShortClasses: true, + removeUnusedImports: true, + ) ->withPaths([ __DIR__ . '/src', __DIR__ . '/tests', diff --git a/src/Auth.php b/src/Auth.php index 2d4b3ee..e4d46b9 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -2,6 +2,7 @@ namespace Leaf; +use League\OAuth2\Client\Provider\AbstractProvider; use Exception; use Firebase\JWT\JWT; use Firebase\JWT\Key; @@ -43,7 +44,7 @@ class Auth */ protected $errorsArray = []; - /** @var array Configured oauth clients */ + /** @var array Configured oauth clients */ protected $oauthClients = []; public function __construct() @@ -201,7 +202,7 @@ public function withGoogle( * --- * Register a generic OAuth client to use with Leaf Auth, should be a league/oauth2-client compatible client. * @param string $clientName The name of the client to register - * @param \League\OAuth2\Client\Provider\AbstractProvider $client An instance of a league/oauth2-client compatible client + * @param AbstractProvider $client An instance of a league/oauth2-client compatible client * @return static */ public function withProvider(string $clientName, $client) @@ -213,7 +214,7 @@ public function withProvider(string $clientName, $client) /** * Return an oauth client * @param string $clientName The name of the client to return - * @return \League\OAuth2\Client\Provider\AbstractProvider|null + * @return AbstractProvider|null */ public function client(string $clientName) { @@ -569,7 +570,7 @@ public function find($id) * * @param array $userData The user details to save * @return User|false|never - * @throws \Exception If database connection is not established + * @throws Exception If database connection is not established */ public function createUserFor($userData) { @@ -754,7 +755,7 @@ public function tokens() * @param string $middleware The middleware to register * @param callable $callback The callback to run if middleware fails * @return void|never - * @throws \Exception If not used with leafs/leaf installed + * @throws Exception If not used with leafs/leaf installed */ public function middleware(string $middleware, callable $callback) { @@ -958,7 +959,7 @@ protected function getFromSession($value) /** * @return void|never - * @throws \Exception If sessions are not enabled + * @throws Exception If sessions are not enabled */ protected function sessionCheck() { diff --git a/src/Auth/Model.php b/src/Auth/Model.php index 17b920d..c5cc61d 100644 --- a/src/Auth/Model.php +++ b/src/Auth/Model.php @@ -35,11 +35,7 @@ class Model protected array $dataToSave = []; /** - * @param array{ - * db: \Leaf\Db, - * user: User, - * table: string, - * } $data + * @param array{db: Db, user: User, table: string} $data */ public function __construct($data) { diff --git a/src/Auth/User.php b/src/Auth/User.php index df6f3ae..f8a31de 100644 --- a/src/Auth/User.php +++ b/src/Auth/User.php @@ -2,6 +2,10 @@ namespace Leaf\Auth; +use Exception; +use Throwable; +use ReturnTypeWillChange; +use Leaf\Db; use Firebase\JWT\JWT; use Leaf\Date; use Leaf\Helpers\Password; @@ -22,7 +26,7 @@ class User /** * Internal instance of Leaf database - * @var \Leaf\Db + * @var Db */ protected $db; @@ -82,7 +86,7 @@ public function __construct($data, $session = true) $sessionLifetime = strtotime($sessionLifetime); if (!$sessionLifetime) { - throw new \Exception('Invalid session lifetime'); + throw new Exception('Invalid session lifetime'); } } else { $sessionLifetime = time() + $sessionLifetime; @@ -165,8 +169,8 @@ public function update(array $userData): bool $this->errorsArray = array_merge($this->errorsArray, $this->db->errors()); return false; } - } catch (\Throwable $th) { - throw new \Exception($th->getMessage()); + } catch (Throwable $th) { + throw new Exception($th->getMessage()); } if (Config::get('session')) { @@ -226,8 +230,8 @@ public function updatePassword(string $oldPassword, string $newPassword): bool $this->errorsArray = array_merge($this->errorsArray, $this->db->errors()); return false; } - } catch (\Throwable $th) { - throw new \Exception($th->getMessage()); + } catch (Throwable $th) { + throw new Exception($th->getMessage()); } $this->data[$passwordKey] = $newPassword; @@ -268,8 +272,8 @@ public function resetPassword(string $newPassword): bool $this->errorsArray = array_merge($this->errorsArray, $this->db->errors()); return false; } - } catch (\Throwable $th) { - throw new \Exception($th->getMessage()); + } catch (Throwable $th) { + throw new Exception($th->getMessage()); } $this->data[$passwordKey] = $newPassword; @@ -390,7 +394,7 @@ public function verifyEmail(): bool ->execute(); return true; - } catch (\Throwable $th) { + } catch (Throwable $th) { return false; } } @@ -425,7 +429,7 @@ public function get() /** * Set user db instance - * @param \Leaf\Db $db + * @param Db $db * @return User */ public function setDb($db) @@ -444,6 +448,7 @@ public function errors() return $this->errorsArray; } + #[ReturnTypeWillChange] public function __toString() { return json_encode($this->get()) ?: ''; @@ -501,14 +506,14 @@ public function __unset($name) * * @param mixed $method The table to relate to * @param mixed $args - * @throws \Exception + * @throws Exception * * @return Model */ public function __call($method, $args) { if (!class_exists('Leaf\App')) { - throw new \Exception('Relations are only available in Leaf apps.'); + throw new Exception('Relations are only available in Leaf apps.'); } return (new Model([ diff --git a/tests/Pest.php b/tests/Pest.php index 7656d56..13cc97a 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -2,6 +2,7 @@ use Leaf\Auth; use Leaf\Db; +use Leaf\Helpers\Password; dataset('test-user', [[[ 'username' => 'test-user', @@ -38,6 +39,7 @@ function authInstance(): Auth { $auth = new Auth(); $auth->dbConnection(dbInstance()->connection()); + $auth->config('token.secret', Password::hash(uniqid())); return $auth; }