From 8a3ce78867287440fd5fd070b08a5bb81f465a64 Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Wed, 5 Nov 2025 11:14:10 -0400 Subject: [PATCH 1/2] composer require rector/rector --dev -W --- composer.json | 5 +++-- rector.php | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 rector.php diff --git a/composer.json b/composer.json index 2e048d9..e005eda 100644 --- a/composer.json +++ b/composer.json @@ -48,7 +48,8 @@ "require-dev": { "pestphp/pest": "^1.0 | ^2.0", "friendsofphp/php-cs-fixer": "^3.64", - "leafs/alchemy": "*" + "leafs/alchemy": "*", + "rector/rector": "^2.2" }, "scripts": { "alchemy": "./vendor/bin/alchemy setup", @@ -56,4 +57,4 @@ "lint": "./vendor/bin/alchemy setup --lint", "actions": "./vendor/bin/alchemy setup --actions" } -} \ No newline at end of file +} diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..d58d7a0 --- /dev/null +++ b/rector.php @@ -0,0 +1,15 @@ +withDowngradeSets(false, false, false, false, false, true) + ->withFluentCallNewLine() + ->withImportNames(true, true, true, true) + ->withPaths([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]) +; From 730e6ebcd42a041481a6b879d39717ae0934ea20 Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Wed, 5 Nov 2025 11:14:41 -0400 Subject: [PATCH 2/2] composer exec rector --- src/Auth.php | 57 ++++++++++++++++++---------------- src/Auth/Model.php | 20 ++++++------ src/Auth/User.php | 15 +++++---- src/Auth/UsesRoles.php | 4 ++- src/Auth/UsesSubscriptions.php | 6 ++-- src/functions.php | 11 ++++--- tests/Pest.php | 19 +++++++----- tests/login.test.php | 8 +++-- tests/register.test.php | 6 ++-- tests/session.test.php | 18 ++++++----- tests/user.test.php | 11 ++++--- 11 files changed, 102 insertions(+), 73 deletions(-) diff --git a/src/Auth.php b/src/Auth.php index 7806538..53d53ff 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -2,12 +2,17 @@ namespace Leaf; +use Exception; use Firebase\JWT\JWT; use Firebase\JWT\Key; use Leaf\Auth\Config; use Leaf\Auth\User; +use Leaf\Exception\General; use Leaf\Helpers\Password; use Leaf\Http\Session; +use League\OAuth2\Client\Provider\Google; +use PDO; +use Throwable; /** * Leaf Simple Auth @@ -55,7 +60,7 @@ public function __construct() }); $this->middleware('is', function ($role) { - \Leaf\Exception\General::error( + General::error( '404', '

The page you are looking for could not be found.

', 403 @@ -63,7 +68,7 @@ public function __construct() }); $this->middleware('isNot', function () { - \Leaf\Exception\General::error( + General::error( '404', '

The page you are looking for could not be found.

', 403 @@ -71,7 +76,7 @@ public function __construct() }); $this->middleware('can', function () { - \Leaf\Exception\General::error( + General::error( '404', '

The page you are looking for could not be found.

', 403 @@ -79,7 +84,7 @@ public function __construct() }); $this->middleware('cannot', function () { - \Leaf\Exception\General::error( + General::error( '404', '

The page you are looking for could not be found.

', 403 @@ -141,10 +146,10 @@ public function autoConnect(array $pdoOptions = []) /** * Pass in db connection instance directly * - * @param \PDO $connection A connection instance of your db + * @param PDO $connection A connection instance of your db * @return $this; */ - public function dbConnection(\PDO $connection) + public function dbConnection(PDO $connection) { $this->db = new Db(); $this->db->connection($connection); @@ -174,7 +179,7 @@ public function withGoogle( $options['redirectUri'] = _env('APP_URL') . '/auth/google/callback'; } - $this->withProvider($clientName, new \League\OAuth2\Client\Provider\Google(array_merge([ + $this->withProvider($clientName, new Google(array_merge([ 'clientId' => $clientId, 'clientSecret' => $clientSecret, 'redirectUri' => $options['redirectUri'], @@ -283,8 +288,8 @@ public function login(array $credentials): bool $this->errorsArray['auth'] = Config::get('messages.loginParamsError'); return false; } - } catch (\Throwable $th) { - throw new \Exception($th->getMessage()); + } catch (Throwable $th) { + throw new Exception($th->getMessage()); } if ($passwordKey !== false) { @@ -350,8 +355,8 @@ public function register(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()); } $user = $this->db->select($table)->where($userData)->first(); @@ -420,8 +425,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')) { @@ -483,8 +488,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->user->{$passwordKey} = $newPassword; @@ -592,8 +597,8 @@ public function createUserFor($userData) $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()); } $user = $this->db->select($table)->where($userData)->first(); @@ -690,8 +695,8 @@ public function user() $this->errorsArray = $this->db->errors(); return null; } - } catch (\Throwable $th) { - throw new \Exception($th->getMessage()); + } catch (Throwable $th) { + throw new Exception($th->getMessage()); } return $this->user = (new User( @@ -736,8 +741,8 @@ public function tokens() */ public function middleware(string $middleware, callable $callback) { - if (!class_exists(\Leaf\App::class)) { - throw new \Exception('This feature is only available for Leaf apps'); + if (!class_exists(App::class)) { + throw new Exception('This feature is only available for Leaf apps'); } if ($middleware === 'auth.required') { @@ -833,7 +838,7 @@ public function parseToken() $bearerToken, new Key(Config::get('token.secret'), 'HS256') ); - } catch (\Throwable $th) { + } catch (Throwable $th) { $this->errorsArray['token'] = $th->getMessage(); return null; } @@ -875,7 +880,7 @@ public function verifyToken(string $token, ?string $purpose = null) } return $user; - } catch (\Throwable $th) { + } catch (Throwable $th) { $this->errorsArray['token'] = $th->getMessage(); return null; } @@ -894,13 +899,13 @@ public function db() protected function checkDbConnection(): void { if (!$this->db && function_exists('db')) { - if (db()->connection() instanceof \PDO || db()->autoConnect()) { + if (db()->connection() instanceof PDO || db()->autoConnect()) { $this->db = db(); } } if (!$this->db) { - throw new \Exception('You need to connect to your database first'); + throw new Exception('You need to connect to your database first'); } } @@ -916,7 +921,7 @@ protected function getFromSession($value) protected function sessionCheck() { if (!Config::get('session')) { - throw new \Exception('Turn on sessions to use this feature.'); + throw new Exception('Turn on sessions to use this feature.'); } } diff --git a/src/Auth/Model.php b/src/Auth/Model.php index f14ecb0..5e3ab5d 100644 --- a/src/Auth/Model.php +++ b/src/Auth/Model.php @@ -2,6 +2,8 @@ namespace Leaf\Auth; +use Leaf\Date; +use Leaf\Db; use PDOStatement; /** @@ -27,7 +29,7 @@ class Model /** * DB connection */ - protected \Leaf\Db $db; + protected Db $db; /** * User data to save @@ -53,7 +55,7 @@ public function create(array $data): ?PDOStatement $data['user_id'] = $this->user->id(); if (Config::get('timestamps')) { - $now = (new \Leaf\Date())->tick()->format(Config::get('timestamps.format')); + $now = (new Date())->tick()->format(Config::get('timestamps.format')); $data['created_at'] = $now; $data['updated_at'] = $now; } @@ -66,11 +68,11 @@ public function create(array $data): ?PDOStatement * * @param array $data Data to be updated * - * @return \Leaf\Db + * @return Db */ - public function update(array $data): \Leaf\Db + public function update(array $data): Db { - $data['updated_at'] = (new \Leaf\Date())->tick()->format(Config::get('timestamps.format')); + $data['updated_at'] = (new Date())->tick()->format(Config::get('timestamps.format')); return $this->db->update($this->table) ->params($data) @@ -80,9 +82,9 @@ public function update(array $data): \Leaf\Db /** * Delete a model resource * - * @return \Leaf\Db + * @return Db */ - public function delete(): \Leaf\Db + public function delete(): Db { return $this->db->delete($this->table) ->where('user_id', $this->user->id()); @@ -93,9 +95,9 @@ public function delete(): \Leaf\Db * * @param string $columns Columns to search by separated by commas or * * - * @return \Leaf\Db + * @return Db */ - public function table($columns = '*'): \Leaf\Db + public function table($columns = '*'): Db { return $this->db->select($this->table, $columns) ->where('user_id', $this->user->id()); diff --git a/src/Auth/User.php b/src/Auth/User.php index c397b18..44c40ee 100644 --- a/src/Auth/User.php +++ b/src/Auth/User.php @@ -2,8 +2,11 @@ namespace Leaf\Auth; +use Exception; use Firebase\JWT\JWT; +use Leaf\Db; use Leaf\Http\Session; +use Throwable; /** * Auth User @@ -20,7 +23,7 @@ class User /** * Internal instance of Leaf database - * @var \Leaf\DB + * @var Db */ protected $db; @@ -69,7 +72,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; @@ -209,7 +212,7 @@ public function verifyEmail(): bool ->execute(); return true; - } catch (\Throwable $th) { + } catch (Throwable $th) { return false; } } @@ -243,7 +246,7 @@ public function get() /** * Set user db instance - * @param \Leaf\Db $db + * @param Db $db * @return User */ public function setDb($db) @@ -293,14 +296,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/src/Auth/UsesRoles.php b/src/Auth/UsesRoles.php index ba82196..25370ba 100644 --- a/src/Auth/UsesRoles.php +++ b/src/Auth/UsesRoles.php @@ -2,6 +2,8 @@ namespace Leaf\Auth; +use Throwable; + /** * Functionality for user permissions * ---- @@ -46,7 +48,7 @@ public function assign($role): bool ]) ->where(Config::get('id.key'), $this->data[Config::get('id.key')]) ->execute(); - } catch (\Throwable $th) { + } catch (Throwable $th) { return false; } diff --git a/src/Auth/UsesSubscriptions.php b/src/Auth/UsesSubscriptions.php index 2dabad7..b3cc78a 100644 --- a/src/Auth/UsesSubscriptions.php +++ b/src/Auth/UsesSubscriptions.php @@ -2,6 +2,8 @@ namespace Leaf\Auth; +use Leaf\Billing\Subscription; + /** * Functionality for user subscriptions * ---- @@ -50,7 +52,7 @@ public function subscription(): ?array */ public function hasSubscription(): bool { - return $this->subscription() && $this->subscription['status'] !== \Leaf\Billing\Subscription::STATUS_CANCELLED; + return $this->subscription() && $this->subscription['status'] !== Subscription::STATUS_CANCELLED; } /** @@ -59,7 +61,7 @@ public function hasSubscription(): bool */ public function hasActiveSubscription(): bool { - return $this->subscription() && ($this->subscription['status'] === \Leaf\Billing\Subscription::STATUS_ACTIVE || $this->subscription['status'] === \Leaf\Billing\Subscription::STATUS_TRIAL); + return $this->subscription() && ($this->subscription['status'] === Subscription::STATUS_ACTIVE || $this->subscription['status'] === Subscription::STATUS_TRIAL); } /** diff --git a/src/functions.php b/src/functions.php index a88c508..fca0b34 100644 --- a/src/functions.php +++ b/src/functions.php @@ -1,5 +1,8 @@ 'test-user', 'email' => 'test-user@example.com', @@ -18,22 +21,22 @@ function getDatabaseConnection(): array ]; } -function dbInstance(): \Leaf\Db +function dbInstance(): Db { - $db = new \Leaf\Db(); + $db = new Db(); try { $db->connect(getDatabaseConnection()); - } catch (\Throwable $th) { + } catch (Throwable $th) { throw $th; } return $db; } -function authInstance(): \Leaf\Auth +function authInstance(): Auth { - $auth = new \Leaf\Auth(); + $auth = new Auth(); $auth->dbConnection(dbInstance()->connection()); return $auth; @@ -41,7 +44,7 @@ function authInstance(): \Leaf\Auth function deleteUser(string $username, $table = 'users') { - $db = new \Leaf\Db(); + $db = new Db(); $db->connect(getDatabaseConnection()); $db->delete($table)->where('username', $username)->execute(); @@ -64,7 +67,7 @@ function createTableForUsers($table = 'users'): void updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )") ->execute(); - } catch (\Throwable $th) { - throw new \Exception('Failed to create table for users: ' . $th->getMessage()); + } catch (Throwable $th) { + throw new Exception('Failed to create table for users: ' . $th->getMessage()); } } diff --git a/tests/login.test.php b/tests/login.test.php index 954826a..1b7a6a8 100644 --- a/tests/login.test.php +++ b/tests/login.test.php @@ -1,5 +1,7 @@ password_hash('password', PASSWORD_BCRYPT) ]) ->execute(); - } catch (\Throwable $th) { + } catch (Throwable $th) { throw $th; } }); @@ -33,7 +35,7 @@ $success = $auth->login($testUser); expect($success)->toBeTrue(); - expect($auth->user())->toBeInstanceOf(\Leaf\Auth\User::class); + expect($auth->user())->toBeInstanceOf(User::class); expect($auth->user()->username)->toBe($testUser['username']); }); @@ -100,7 +102,7 @@ $success = $auth->login($userData); expect($success)->toBeTrue(); - expect($auth->user())->toBeInstanceOf(\Leaf\Auth\User::class); + expect($auth->user())->toBeInstanceOf(User::class); expect($auth->user()->username)->toBe($userData['username']); $auth->config('password.key', 'password'); diff --git a/tests/register.test.php b/tests/register.test.php index 630335f..d87cb91 100644 --- a/tests/register.test.php +++ b/tests/register.test.php @@ -1,5 +1,7 @@ delete('users')->execute(); @@ -26,7 +28,7 @@ } expect($success)->toBeTrue(); - expect($auth->user())->toBeInstanceOf(\Leaf\Auth\User::class); + expect($auth->user())->toBeInstanceOf(User::class); expect($auth->user()->username)->toBe($userData['username']); }); @@ -47,7 +49,7 @@ $loginSuccess = $auth->login($userData); expect($loginSuccess)->toBeTrue(); - expect($auth->user())->toBeInstanceOf(\Leaf\Auth\User::class); + expect($auth->user())->toBeInstanceOf(User::class); expect($auth->user()->username)->toBe($userData['username']); }); diff --git a/tests/session.test.php b/tests/session.test.php index 55a3c2c..9eefe62 100644 --- a/tests/session.test.php +++ b/tests/session.test.php @@ -1,5 +1,7 @@ delete('users')->execute(); @@ -30,7 +32,7 @@ $success = $auth->register($userData); expect($success)->toBeTrue(); - expect($auth->user())->toBeInstanceOf(\Leaf\Auth\User::class); + expect($auth->user())->toBeInstanceOf(User::class); expect($auth->user()->username)->toBe($userData['username']); expect(session_status())->toBe(PHP_SESSION_ACTIVE); @@ -51,7 +53,7 @@ $success = $auth->register($userData); expect($success)->toBeTrue(); - expect($auth->user())->toBeInstanceOf(\Leaf\Auth\User::class); + expect($auth->user())->toBeInstanceOf(User::class); expect(session_status())->toBe(PHP_SESSION_NONE); expect($_SESSION['auth']['user']['username'] ?? null)->toBeNull(); @@ -71,7 +73,7 @@ $success = $auth->login($userData); expect($success)->toBeTrue(); - expect($auth->user())->toBeInstanceOf(\Leaf\Auth\User::class); + expect($auth->user())->toBeInstanceOf(User::class); expect($auth->user()->username)->toBe($userData['username']); expect(session_status())->toBe(PHP_SESSION_ACTIVE); @@ -94,7 +96,7 @@ $success = $auth->login($userData); expect($success)->toBeTrue(); - expect($auth->user())->toBeInstanceOf(\Leaf\Auth\User::class); + expect($auth->user())->toBeInstanceOf(User::class); expect($auth->user()->username)->toBe($userData['username']); expect(session_status())->toBe(PHP_SESSION_ACTIVE); @@ -117,7 +119,7 @@ $success = $auth->login($userData); expect($success)->toBeTrue(); - expect($auth->user())->toBeInstanceOf(\Leaf\Auth\User::class); + expect($auth->user())->toBeInstanceOf(User::class); expect($auth->user()->username)->toBe($userData['username']); expect(session_status())->toBe(PHP_SESSION_ACTIVE); @@ -140,7 +142,7 @@ $success = $auth->login($userData); expect($success)->toBeTrue(); - expect($auth->user())->toBeInstanceOf(\Leaf\Auth\User::class); + expect($auth->user())->toBeInstanceOf(User::class); expect($auth->user()->username)->toBe($userData['username']); expect(session_status())->toBe(PHP_SESSION_ACTIVE); @@ -172,7 +174,7 @@ $newSessionId = session_id(); expect($success)->toBeTrue(); - expect($auth->user())->toBeInstanceOf(\Leaf\Auth\User::class); + expect($auth->user())->toBeInstanceOf(User::class); expect($auth->user()->username)->toBe($userData['username']); expect(session_status())->toBe(PHP_SESSION_ACTIVE); @@ -195,7 +197,7 @@ $success = $auth->login($userData); expect($success)->toBeTrue(); - expect($auth->user())->toBeInstanceOf(\Leaf\Auth\User::class); + expect($auth->user())->toBeInstanceOf(User::class); expect($_SESSION['auth']['user']['username'] ?? null)->toBe($userData['username']); $auth->logout(); diff --git a/tests/user.test.php b/tests/user.test.php index 4086518..2283c83 100644 --- a/tests/user.test.php +++ b/tests/user.test.php @@ -1,5 +1,8 @@ delete('users')->execute(); @@ -13,7 +16,7 @@ 'password' => password_hash('password', PASSWORD_BCRYPT) ]) ->execute(); - } catch (\Throwable $th) { + } catch (Throwable $th) { throw $th; } }); @@ -39,7 +42,7 @@ } expect($success)->toBeTrue(); - expect($auth->user())->toBeInstanceOf(\Leaf\Auth\User::class); + expect($auth->user())->toBeInstanceOf(User::class); expect($auth->user()->username)->toBe($testUser['username']); }); @@ -60,11 +63,11 @@ } expect($success)->toBeTrue(); - expect($auth->user())->toBeInstanceOf(\Leaf\Auth\User::class); + expect($auth->user())->toBeInstanceOf(User::class); expect($auth->user()->username)->toBe($testUser['username']); $auth->logout(function ($auth) use ($testUser) { - expect($auth)->toBeInstanceOf(\Leaf\Auth::class); + expect($auth)->toBeInstanceOf(Auth::class); }); expect($auth->user())->toBeNull();