Skip to content

Commit

Permalink
refactor: mark classes as final to prevent inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
MrPunyapal committed Feb 23, 2025
1 parent ac0a516 commit de8303f
Show file tree
Hide file tree
Showing 24 changed files with 45 additions and 46 deletions.
2 changes: 1 addition & 1 deletion app/Actions/Posts/CreatePostAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use App\Models\Post;

class CreatePostAction
final class CreatePostAction
{
/**
* Create a new post.
Expand Down
2 changes: 1 addition & 1 deletion app/Actions/Posts/DeletePostAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use App\Models\Post;

class DeletePostAction
final class DeletePostAction
{
public function execute(Post $post): bool
{
Expand Down
2 changes: 1 addition & 1 deletion app/Actions/Posts/TogglePostFeatureAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use App\Enums\FeaturedStatus;
use App\Models\Post;

class TogglePostFeatureAction
final class TogglePostFeatureAction
{
public function execute(Post $post): bool
{
Expand Down
2 changes: 1 addition & 1 deletion app/Actions/Posts/UpdatePostAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use App\Models\Post;

class UpdatePostAction
final class UpdatePostAction
{
/**
* Update a post.
Expand Down
2 changes: 1 addition & 1 deletion app/Builders/PostBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @extends Builder<Post>
*/
class PostBuilder extends Builder
final class PostBuilder extends Builder
{
public function search(string $search): static
{
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Response;

class PostController extends Controller
final class PostController extends Controller
{
/**
* Display a listing of the resource.
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/FeaturedPostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use App\Models\Post;
use Illuminate\Http\RedirectResponse;

class FeaturedPostController extends Controller
final class FeaturedPostController extends Controller
{
public function __invoke(Post $post, TogglePostFeatureAction $action): RedirectResponse
{
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/LocaleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Illuminate\Http\RedirectResponse;

class LocaleController extends Controller
final class LocaleController extends Controller
{
/**
* Handle the incoming request.
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use Illuminate\Http\Request;
use Illuminate\View\View;

class PostController extends Controller
final class PostController extends Controller
{
/**
* Display a listing of the resource.
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Middleware/SetLocale.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class SetLocale
final class SetLocale
{
/**
* Handle an incoming request.
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/StorePostRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Illuminate\Foundation\Http\FormRequest;
use Override;

class StorePostRequest extends FormRequest
final class StorePostRequest extends FormRequest
{
use HasFileFromUrl;

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/UpdatePostRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Illuminate\Foundation\Http\FormRequest;
use Override;

class UpdatePostRequest extends FormRequest
final class UpdatePostRequest extends FormRequest
{
use HasFileFromUrl;

Expand Down
2 changes: 1 addition & 1 deletion app/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*
* @mixin Model
*/
class Category extends Model
final class Category extends Model
{
/** @use HasFactory<\Database\Factories\CategoryFactory> */
use HasFactory;
Expand Down
36 changes: 18 additions & 18 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*
* @mixin Model
*/
class Post extends Model
final class Post extends Model
{
/** @use HasFactory<\Database\Factories\PostFactory> */
use HasFactory;
Expand Down Expand Up @@ -88,26 +88,10 @@ public function category(): BelongsTo
return $this->belongsTo(Category::class);
}

/**
* The attributes that should be cast.
*
* @return array<string, string>
*/
#[Override]
protected function casts(): array
{
return [
'tags' => 'array',
'published_at' => 'datetime',
'is_featured' => FeaturedStatus::class,
'content' => CleanHtmlInput::class,
];
}

/**
* @return Attribute<string|null, string|null>
*/
protected function image(): Attribute
public function image(): Attribute
{
return Attribute::make(
get: function (mixed $value): ?string {
Expand All @@ -130,4 +114,20 @@ protected function image(): Attribute
}
);
}

/**
* The attributes that should be cast.
*
* @return array<string, string>
*/
#[Override]
protected function casts(): array
{
return [
'tags' => 'array',
'published_at' => 'datetime',
'is_featured' => FeaturedStatus::class,
'content' => CleanHtmlInput::class,
];
}
}
2 changes: 1 addition & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*
* @mixin Model
*/
class User extends Authenticatable // implements MustVerifyEmail
final class User extends Authenticatable // implements MustVerifyEmail
{
use HasApiTokens;

Expand Down
2 changes: 1 addition & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Illuminate\Support\ServiceProvider;
use Override;

class AppServiceProvider extends ServiceProvider
final class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
Expand Down
2 changes: 1 addition & 1 deletion app/Support/FileUploaderFromUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;

class FileUploaderFromUrl
final class FileUploaderFromUrl
{
public function __invoke(string $url): ?UploadedFile
{
Expand Down
8 changes: 4 additions & 4 deletions app/Support/QueryResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Route;

class QueryResolver
final readonly class QueryResolver
{
/**
* @var Collection<string, string>
*/
private readonly Collection $query;
private Collection $query;

public function __construct()
{
Expand All @@ -30,9 +30,9 @@ public function __construct()
}

/**
* @return array<string, string>|null
* @return array<string, string>
*/
public static function getPreviousQuery(string $routeName): ?array
public static function getPreviousQuery(string $routeName): array
{
/** @var array<string, string> */
$query = session()->get($routeName.'.previous.query', []);
Expand Down
2 changes: 1 addition & 1 deletion database/factories/CategoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* @extends Factory<Category>
*/
class CategoryFactory extends Factory
final class CategoryFactory extends Factory
{
/**
* Define the model's default state.
Expand Down
2 changes: 1 addition & 1 deletion database/factories/PostFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* @extends Factory<Post>
*/
class PostFactory extends Factory
final class PostFactory extends Factory
{
/**
* Define the model's default state.
Expand Down
7 changes: 2 additions & 5 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@

use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Override;

/**
* @extends Factory<User>
*/
class UserFactory extends Factory
final class UserFactory extends Factory
{
protected static ?string $password = null;

/**
* Define the model's default state.
*
Expand All @@ -29,7 +26,7 @@ public function definition(): array
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => static::$password ??= Hash::make('password'),
'password' => 'password',
'remember_token' => Str::random(10),
];
}
Expand Down
2 changes: 1 addition & 1 deletion database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Schema;

class DatabaseSeeder extends Seeder
final class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
Expand Down
1 change: 1 addition & 0 deletions pint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"declare_strict_types": true,
"strict_param": true,
"strict_comparison": true,
"final_class": true,
"fully_qualified_strict_types": {
"import_symbols": true,
"leading_backslash_in_global_namespace": false,
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/ArchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
arch()->preset()->php();
arch()->preset()->security();
arch()->preset()->laravel();
// arch()->preset()->strict(); // todo

0 comments on commit de8303f

Please sign in to comment.