Skip to content

Commit

Permalink
[1.x] Uses PHP Native Type Declarations 🐘 (#201)
Browse files Browse the repository at this point in the history
* Adds types to stubs

* Fixes import order

* Fixes $policies annotation

* Improves types on `Request::rules`

* Adds types to profile page related features (#234)
  • Loading branch information
nunomaduro authored Jan 3, 2023
1 parent 136e8c2 commit 5d9ff31
Show file tree
Hide file tree
Showing 48 changed files with 150 additions and 255 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\LoginRequest;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;

class AuthenticatedSessionController extends Controller
{
/**
* Handle an incoming authentication request.
*
* @param \App\Http\Requests\Auth\LoginRequest $request
* @return \Illuminate\Http\Response
*/
public function store(LoginRequest $request)
public function store(LoginRequest $request): Response
{
$request->authenticate();

Expand All @@ -26,11 +24,8 @@ public function store(LoginRequest $request)

/**
* Destroy an authenticated session.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function destroy(Request $request)
public function destroy(Request $request): Response
{
Auth::guard('web')->logout();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class EmailVerificationNotificationController extends Controller
{
/**
* Send a new email verification notification.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
*/
public function store(Request $request)
public function store(Request $request): JsonResponse|RedirectResponse
{
if ($request->user()->hasVerifiedEmail()) {
return redirect()->intended(RouteServiceProvider::HOME);
Expand Down
6 changes: 2 additions & 4 deletions stubs/api/app/Http/Controllers/Auth/NewPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Http\Controllers\Controller;
use Illuminate\Auth\Events\PasswordReset;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Password;
Expand All @@ -16,12 +17,9 @@ class NewPasswordController extends Controller
/**
* Handle an incoming new password request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*
* @throws \Illuminate\Validation\ValidationException
*/
public function store(Request $request)
public function store(Request $request): JsonResponse
{
$request->validate([
'token' => ['required'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Password;
use Illuminate\Validation\ValidationException;
Expand All @@ -12,12 +13,9 @@ class PasswordResetLinkController extends Controller
/**
* Handle an incoming password reset link request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*
* @throws \Illuminate\Validation\ValidationException
*/
public function store(Request $request)
public function store(Request $request): JsonResponse
{
$request->validate([
'email' => ['required', 'email'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\User;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules;
Expand All @@ -15,12 +16,9 @@ class RegisteredUserController extends Controller
/**
* Handle an incoming registration request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*
* @throws \Illuminate\Validation\ValidationException
*/
public function store(Request $request)
public function store(Request $request): Response
{
$request->validate([
'name' => ['required', 'string', 'max:255'],
Expand Down
6 changes: 2 additions & 4 deletions stubs/api/app/Http/Controllers/Auth/VerifyEmailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
use App\Providers\RouteServiceProvider;
use Illuminate\Auth\Events\Verified;
use Illuminate\Foundation\Auth\EmailVerificationRequest;
use Illuminate\Http\RedirectResponse;

class VerifyEmailController extends Controller
{
/**
* Mark the authenticated user's email address as verified.
*
* @param \Illuminate\Foundation\Auth\EmailVerificationRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function __invoke(EmailVerificationRequest $request)
public function __invoke(EmailVerificationRequest $request): RedirectResponse
{
if ($request->user()->hasVerifiedEmail()) {
return redirect()->intended(
Expand Down
9 changes: 4 additions & 5 deletions stubs/api/app/Http/Middleware/EnsureEmailIsVerified.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@

use Closure;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class EnsureEmailIsVerified
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $redirectToRoute
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|null
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle($request, Closure $next, $redirectToRoute = null)
public function handle(Request $request, Closure $next, string $redirectToRoute = null): Response
{
if (! $request->user() ||
($request->user() instanceof MustVerifyEmail &&
Expand Down
20 changes: 6 additions & 14 deletions stubs/api/app/Http/Requests/Auth/LoginRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,18 @@ class LoginRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
* @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
*/
public function rules()
public function rules(): array
{
return [
'email' => ['required', 'string', 'email'],
Expand All @@ -37,11 +35,9 @@ public function rules()
/**
* Attempt to authenticate the request's credentials.
*
* @return void
*
* @throws \Illuminate\Validation\ValidationException
*/
public function authenticate()
public function authenticate(): void
{
$this->ensureIsNotRateLimited();

Expand All @@ -59,11 +55,9 @@ public function authenticate()
/**
* Ensure the login request is not rate limited.
*
* @return void
*
* @throws \Illuminate\Validation\ValidationException
*/
public function ensureIsNotRateLimited()
public function ensureIsNotRateLimited(): void
{
if (! RateLimiter::tooManyAttempts($this->throttleKey(), 5)) {
return;
Expand All @@ -83,10 +77,8 @@ public function ensureIsNotRateLimited()

/**
* Get the rate limiting throttle key for the request.
*
* @return string
*/
public function throttleKey()
public function throttleKey(): string
{
return Str::transliterate(Str::lower($this->input('email')).'|'.$this->ip());
}
Expand Down
8 changes: 3 additions & 5 deletions stubs/api/app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,20 @@ class AuthServiceProvider extends ServiceProvider
/**
* The policy mappings for the application.
*
* @var array
* @var array<class-string, class-string>
*/
protected $policies = [
// 'App\Models\Model' => 'App\Policies\ModelPolicy',
];

/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
public function boot(): void
{
$this->registerPolicies();

ResetPassword::createUrlUsing(function ($notifiable, $token) {
ResetPassword::createUrlUsing(function (object $notifiable, string $token) {
return config('app.frontend_url')."/password-reset/$token?email={$notifiable->getEmailForPasswordReset()}";
});

Expand Down
2 changes: 1 addition & 1 deletion stubs/api/pest-tests/Feature/Auth/PasswordResetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

$this->post('/forgot-password', ['email' => $user->email]);

Notification::assertSentTo($user, ResetPassword::class, function ($notification) use ($user) {
Notification::assertSentTo($user, ResetPassword::class, function (object $notification) use ($user) {
$response = $this->post('/reset-password', [
'token' => $notification->token,
'email' => $user->email,
Expand Down
4 changes: 2 additions & 2 deletions stubs/api/tests/Feature/Auth/AuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class AuthenticationTest extends TestCase
{
use RefreshDatabase;

public function test_users_can_authenticate_using_the_login_screen()
public function test_users_can_authenticate_using_the_login_screen(): void
{
$user = User::factory()->create();

Expand All @@ -23,7 +23,7 @@ public function test_users_can_authenticate_using_the_login_screen()
$response->assertNoContent();
}

public function test_users_can_not_authenticate_with_invalid_password()
public function test_users_can_not_authenticate_with_invalid_password(): void
{
$user = User::factory()->create();

Expand Down
4 changes: 2 additions & 2 deletions stubs/api/tests/Feature/Auth/EmailVerificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class EmailVerificationTest extends TestCase
{
use RefreshDatabase;

public function test_email_can_be_verified()
public function test_email_can_be_verified(): void
{
$user = User::factory()->create([
'email_verified_at' => null,
Expand All @@ -35,7 +35,7 @@ public function test_email_can_be_verified()
$response->assertRedirect(config('app.frontend_url').RouteServiceProvider::HOME.'?verified=1');
}

public function test_email_is_not_verified_with_invalid_hash()
public function test_email_is_not_verified_with_invalid_hash(): void
{
$user = User::factory()->create([
'email_verified_at' => null,
Expand Down
6 changes: 3 additions & 3 deletions stubs/api/tests/Feature/Auth/PasswordResetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class PasswordResetTest extends TestCase
{
use RefreshDatabase;

public function test_reset_password_link_can_be_requested()
public function test_reset_password_link_can_be_requested(): void
{
Notification::fake();

Expand All @@ -23,15 +23,15 @@ public function test_reset_password_link_can_be_requested()
Notification::assertSentTo($user, ResetPassword::class);
}

public function test_password_can_be_reset_with_valid_token()
public function test_password_can_be_reset_with_valid_token(): void
{
Notification::fake();

$user = User::factory()->create();

$this->post('/forgot-password', ['email' => $user->email]);

Notification::assertSentTo($user, ResetPassword::class, function ($notification) use ($user) {
Notification::assertSentTo($user, ResetPassword::class, function (object $notification) use ($user) {
$response = $this->post('/reset-password', [
'token' => $notification->token,
'email' => $user->email,
Expand Down
2 changes: 1 addition & 1 deletion stubs/api/tests/Feature/Auth/RegistrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class RegistrationTest extends TestCase
{
use RefreshDatabase;

public function test_new_users_can_register()
public function test_new_users_can_register(): void
{
$response = $this->post('/register', [
'name' => 'Test User',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,25 @@
use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\LoginRequest;
use App\Providers\RouteServiceProvider;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;

class AuthenticatedSessionController extends Controller
{
/**
* Display the login view.
*
* @return \Illuminate\View\View
*/
public function create()
public function create(): View
{
return view('auth.login');
}

/**
* Handle an incoming authentication request.
*
* @param \App\Http\Requests\Auth\LoginRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(LoginRequest $request)
public function store(LoginRequest $request): RedirectResponse
{
$request->authenticate();

Expand All @@ -37,11 +34,8 @@ public function store(LoginRequest $request)

/**
* Destroy an authenticated session.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(Request $request)
public function destroy(Request $request): RedirectResponse
{
Auth::guard('web')->logout();

Expand Down
Loading

0 comments on commit 5d9ff31

Please sign in to comment.