Skip to content

Commit

Permalink
Improve function signatures and return type annotations
Browse files Browse the repository at this point in the history
PHP 8.4 introduces the deprecation of implicitly nullable parameters
(https://github.com/php/php-src/blob/php-8.4.0RC1/UPGRADING#L497).
This PR updates the codebase to be compatible with PHP 8.4, and also
improves the function signatures and return type annotations.
  • Loading branch information
selfsimilar committed Sep 27, 2024
1 parent 82cad73 commit 585a40a
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/Controllers/ImpersonateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct()
* @return RedirectResponse
* @throws \Exception
*/
public function take(Request $request, $id, $guardName = null)
public function take(Request $request, int $id, ?string $guardName = null): RedirectResponse
{
$guardName = $guardName ?? $this->manager->getDefaultSessionGuard();

Expand Down Expand Up @@ -64,7 +64,7 @@ public function take(Request $request, $id, $guardName = null)
/**
* @return RedirectResponse
*/
public function leave()
public function leave(): RedirectResponse
{
if (!$this->manager->isImpersonating()) {
abort(403);
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class InvalidUserProvider extends \Exception
{
public function __construct(string $guard, $message = "", $code = 0, Throwable $previous = null)
public function __construct(string $guard, $message = "", $code = 0, ?Throwable $previous = null)
{
parent::__construct(sprintf('Invalid user provider for guard %s', $guard), $code, $previous);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Guard/SessionGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SessionGuard extends BaseSessionGuard
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @return void
*/
public function quietLogin(Authenticatable $user)
public function quietLogin(Authenticatable $user): void
{
$this->updateSession($user->getAuthIdentifier());

Expand All @@ -27,7 +27,7 @@ public function quietLogin(Authenticatable $user)
* @param void
* @return void
*/
public function quietLogout()
public function quietLogout(): void
{
$this->clearUserDataFromStorage();

Expand Down
2 changes: 1 addition & 1 deletion src/Impersonate.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Impersonate extends Facade
*
* @return string
*/
protected static function getFacadeAccessor()
protected static function getFacadeAccessor(): string
{
return ImpersonateManager::class;
}
Expand Down
16 changes: 8 additions & 8 deletions src/ImpersonateServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ImpersonateServiceProvider extends \Illuminate\Support\ServiceProvider
*
* @return void
*/
public function register()
public function register(): void
{
$this->mergeConfig();

Expand All @@ -50,7 +50,7 @@ public function register()
*
* @return void
*/
public function boot()
public function boot(): void
{
$this->publishConfig();

Expand All @@ -69,7 +69,7 @@ public function boot()
* @param void
* @return void
*/
protected function registerBladeDirectives()
protected function registerBladeDirectives(): void
{
$this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
$bladeCompiler->directive('impersonating', function ($guard = null) {
Expand Down Expand Up @@ -107,7 +107,7 @@ protected function registerBladeDirectives()
* @param void
* @return void
*/
protected function registerRoutesMacro()
protected function registerRoutesMacro(): void
{
$router = $this->app['router'];

Expand All @@ -123,7 +123,7 @@ protected function registerRoutesMacro()
* @param void
* @return void
*/
protected function registerAuthDriver()
protected function registerAuthDriver(): void
{
/** @var AuthManager $auth */
$auth = $this->app['auth'];
Expand Down Expand Up @@ -155,7 +155,7 @@ protected function registerAuthDriver()
* @param void
* @return void
*/
public function registerMiddleware()
public function registerMiddleware(): void
{
$this->app['router']->aliasMiddleware('impersonate.protect', ProtectFromImpersonation::class);
}
Expand All @@ -166,7 +166,7 @@ public function registerMiddleware()
* @param void
* @return void
*/
protected function mergeConfig()
protected function mergeConfig(): void
{
$configPath = __DIR__ . '/../config/' . $this->configName . '.php';

Expand All @@ -179,7 +179,7 @@ protected function mergeConfig()
* @param void
* @return void
*/
protected function publishConfig()
protected function publishConfig(): void
{
$configPath = __DIR__ . '/../config/' . $this->configName . '.php';

Expand Down
3 changes: 2 additions & 1 deletion src/Middleware/ProtectFromImpersonation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Lab404\Impersonate\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Lab404\Impersonate\Services\ImpersonateManager;

Expand All @@ -15,7 +16,7 @@ class ProtectFromImpersonation
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
public function handle(Request $request, Closure $next): mixed
{
$impersonate_manager = app()->make(ImpersonateManager::class);

Expand Down
10 changes: 5 additions & 5 deletions src/Models/Impersonate.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait Impersonate
* @param void
* @return bool
*/
public function canImpersonate()
public function canImpersonate(): bool
{
return true;
}
Expand All @@ -24,7 +24,7 @@ public function canImpersonate()
* @param void
* @return bool
*/
public function canBeImpersonated()
public function canBeImpersonated(): bool
{
return true;
}
Expand All @@ -36,7 +36,7 @@ public function canBeImpersonated()
* @param string|null $guardName
* @return bool
*/
public function impersonate(Model $user, $guardName = null)
public function impersonate(Model $user, ?string $guardName = null): bool
{
return app(ImpersonateManager::class)->take($this, $user, $guardName);
}
Expand All @@ -47,7 +47,7 @@ public function impersonate(Model $user, $guardName = null)
* @param void
* @return bool
*/
public function isImpersonated()
public function isImpersonated(): bool
{
return app(ImpersonateManager::class)->isImpersonating();
}
Expand All @@ -58,7 +58,7 @@ public function isImpersonated()
* @param void
* @return bool
*/
public function leaveImpersonation()
public function leaveImpersonation(): bool
{
if ($this->isImpersonated()) {
return app(ImpersonateManager::class)->leave();
Expand Down
20 changes: 11 additions & 9 deletions src/Services/ImpersonateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace Lab404\Impersonate\Services;

use Exception;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Application;
use Illuminate\Support\Collection;
use Lab404\Impersonate\Events\LeaveImpersonation;
use Lab404\Impersonate\Events\TakeImpersonation;
use Lab404\Impersonate\Exceptions\InvalidUserProvider;
Expand All @@ -31,7 +33,7 @@ public function __construct(Application $app)
* @throws InvalidUserProvider
* @throws ModelNotFoundException
*/
public function findUserById($id, $guardName = null)
public function findUserById(int $id, ?string $guardName = null): Authenticatable
{
if (empty($guardName)) {
$guardName = $this->app['config']->get('auth.default.guard', 'web');
Expand Down Expand Up @@ -70,15 +72,15 @@ public function isImpersonating(): bool
/**
* @return int|null
*/
public function getImpersonatorId()
public function getImpersonatorId(): ?int
{
return session($this->getSessionKey(), null);
}

/**
* @return \Illuminate\Contracts\Auth\Authenticatable
*/
public function getImpersonator()
public function getImpersonator(): Authenticatable
{
$id = session($this->getSessionKey(), null);

Expand All @@ -88,15 +90,15 @@ public function getImpersonator()
/**
* @return string|null
*/
public function getImpersonatorGuardName()
public function getImpersonatorGuardName(): ?string
{
return session($this->getSessionGuard(), null);
}

/**
* @return string|null
*/
public function getImpersonatorGuardUsingName()
public function getImpersonatorGuardUsingName(): ?string
{
return session($this->getSessionGuardUsing(), null);
}
Expand All @@ -107,7 +109,7 @@ public function getImpersonatorGuardUsingName()
* @param string|null $guardName
* @return bool
*/
public function take($from, $to, $guardName = null)
public function take(Authenticatable $from, Authenticatable $to, ?string $guardName = null): bool
{
$this->saveAuthCookieInSession();

Expand Down Expand Up @@ -153,7 +155,7 @@ public function leave(): bool
return true;
}

public function clear()
public function clear(): void
{
session()->forget($this->getSessionKey());
session()->forget($this->getSessionGuard());
Expand Down Expand Up @@ -205,7 +207,7 @@ public function getLeaveRedirectTo(): string
/**
* @return array|null
*/
public function getCurrentAuthGuardName()
public function getCurrentAuthGuardName(): ?array
{
$guards = array_keys(config('auth.guards'));

Expand Down Expand Up @@ -249,7 +251,7 @@ protected function extractAuthCookieFromSession(): void
* @param string $search
* @return \Illuminate\Support\Collection
*/
protected function findByKeyInArray(array $values, string $search)
protected function findByKeyInArray(array $values, string $search): Collection
{
return collect($values ?? session()->all())
->filter(function ($val, $key) use ($search) {
Expand Down
6 changes: 3 additions & 3 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @param null $guard
* @return bool
*/
function can_impersonate(string $guard = null): bool
function can_impersonate(?string $guard = null): bool
{
$guard = $guard ?? app('impersonate')->getCurrentAuthGuardName();

Expand All @@ -28,7 +28,7 @@ function can_impersonate(string $guard = null): bool
* @param string|null $guard
* @return bool
*/
function can_be_impersonated(Authenticatable $user, string $guard = null): bool
function can_be_impersonated(Authenticatable $user, ?string $guard = null): bool
{
$guard = $guard ?? app('impersonate')->getCurrentAuthGuardName();
return app('auth')->guard($guard)->check()
Expand All @@ -45,7 +45,7 @@ function can_be_impersonated(Authenticatable $user, string $guard = null): bool
* @param string|null $guard
* @return bool
*/
function is_impersonating(string $guard = null): bool
function is_impersonating(?string $guard = null): bool
{
$guard = $guard ?? app('impersonate')->getCurrentAuthGuardName();

Expand Down

0 comments on commit 585a40a

Please sign in to comment.