Overview
This document outlines the refactoring process applied to the password validation rules within the PHP Laravel application. The primary goal was to eliminate code duplication across different parts of the application by centralizing the password rules in a single, reusable method.
Background
Previously, the password validation rules were duplicated in multiple files (reset-password-blade.php, register.blade.php, and LoginForm.php). This duplication not only made the codebase larger and harder to maintain but also increased the risk of inconsistencies in password policy enforcement across different modules.
Refactoring Details
Centralized Password Rules
A new method named passwordRule() was created in the Customer.php model. This method encapsulates the password validation rules as follows:
php
public static function passwordRule(): array
{
return [
'required',
'string',
'confirmed',
Password::min(8)->numbers()->mixedCase()->symbols(),
];
}
Integration of the Centralized Method
The passwordRule() method was then integrated into various parts of the application where password rules were previously defined. The changes made are detailed below:
- Reset Password Blade (reset-password-blade.php)
Replaced the direct rule definition with a call to the centralized method:
Old Code vs. New Code
php Old Code:
rules([
'token' => ['required'],
'email' => ['required', 'string', 'email'],
'password' => [
'required',
'string',
'confirmed',
Rules\Password::min(8)->numbers()->mixedCase()->symbols(),
],
]);
php New Code:
rules([
'token' => ['required'],
'email' => ['required', 'string', 'email'],
'password' => \App\Models\Customer::passwordRule(),
]);
- Registration Blade (register.blade.php)
Integrated the centralized password rules into the registration form validation:
php Old Code:
rules([
'first_name' => ['required', 'string', 'max:255'],
'last_name' => ['required', 'string', 'max:255'],
'email' => [
'required',
'string',
'lowercase',
'email',
'max:255',
'unique:'.Customer::class,
],
'password' => [
'required',
'string',
'confirmed',
Rules\Password::min(8)->numbers()->mixedCase()->symbols(),
],
]);
php New Code:
rules([
'first_name' => ['required', 'string', 'max:255'],
'last_name' => ['required', 'string', 'max:255'],
'email' => [
'required',
'string',
'lowercase',
'email',
'max:255',
'unique:'.Customer::class,
],
'password' => \App\Models\Customer::passwordRule(),
]);
- Login Form (LoginForm.php)
Replaced the password validation in the login form rules with the centralized method:
php Old Code:
public function rules()
{
return [
'email' => [
'required',
'string',
'lowercase',
'email',
'max:255',
],
'password' => [
'required',
'string',
'confirmed',
Password::min(8)->numbers()->mixedCase()->symbols(),
],
'remember' => 'boolean',
];
}
php New Code:
public function rules()
{
return [
'email' => [
'required',
'string',
'lowercase',
'email',
'max:255',
],
'password' => \App\Models\Customer::passwordRule(),
'remember' => 'boolean',
];
}
Benefits of Refactoring
DRY Principle: Adheres to the "Don't Repeat Yourself" principle, reducing code duplication.
Ease of Maintenance: Changes to the password rules now only need to be made in one place, improving maintainability.
Consistency: Ensures consistent enforcement of password rules across the application.
Conclusion
The refactoring of the password validation rules into a single, centralized method in the Customer model has streamlined the codebase, reduced duplication, and improved the maintainability and consistency of the application. This change aligns with best practices in software development and contributes to a cleaner and more manageable codebase.