Get your next remote job on LaraJobs.
1 contributor Edit on GitHub PHP Packages

Remove final from PHP packages with Unfinalize

Table of contents:

PHP’s final keyword is a well-known feature that allows developers to restrict further inheritance or overriding of classes and methods.

But what if you need the freedom to extend or override a third-party class that has been marked as final? Meet Unfinalize from Steve Bauman, a tool that helps us be anarchists by bypassing this restriction safely and efficiently.

Introduction to Unfinalize

Unfinalize is a tool that utilizes PHP CS Fixer to remove the final keywords from classes and methods in the Composer packages you use.

Here are some of its features:

  • Safely removes final keywords from classes and methods.
  • Operates quickly and efficiently, with no performance impact.
  • Requires no additional dependencies; everything is compiled into a single PHAR file.

Let’s dive into how you can install and use Unfinalize in your project.

Install Unfinalize

Open your terminal and run the following command:

composer require stevebauman/unfinalize

Still with me? Great!

Configure Unfinalize

In your project’s composer.json file, add an “unfinalize” section. Here you’ll specify which packages should have the final keywords removed.

{
"unfinalize": [
"vendor/package"
]
}

Still in your composer.json, add the unfinalize command to the scripts section so that it runs whenever you execute composer update.

{
"scripts": {
"post-update-cmd": [
"@php vendor/bin/unfinalize run"
]
}
}

Finally, run composer update to apply the changes:

composer update

Additional Unfinalize options

Annotate classes and methods as @final

If you want to annotate classes and methods with @final instead of outright removing the final keyword, you can add the --mark-final option when running the command.

Update your composer.json like this:

{
"scripts": {
"post-update-cmd": [
"@php vendor/bin/unfinalize run --mark-final"
]
}
}

This will docblock the classes and methods with @final while still allow them to be inherited or overridden.

Simulate the changes using the dry run mode

To see what changes Unfinalize would make without actually altering any files, use the --dry option:

php vendor/bin/unfinalize run --dry

This will print out a list of files that would be modified.

Benjamin Crozat

Written by Benjamin Crozat

Indie hacker, blogger, and AI enthusiast building things with the TALL stack. 🔥

Follow me on:

Recommended articles

20+ Laravel best practices, tips and tricks to use in 2023 20+ Laravel best practices, tips and tricks to use in 2023

Learning a framework can be overwhelming, but time and execution will make you a master. Here are some best practices to help you toward your goal.

Modified on Oct 17, 2023

9 testing best practices for Laravel in 2023 9 testing best practices for Laravel in 2023

Are you familiar with testing? Good. Here are a bunch of best practices to help you level up even more!

Modified on Oct 27, 2023

Laravel interview questions and answers for 2023 Laravel interview questions and answers for 2023

Nailing a Laravel job interview can be a daunting task, but with the right preparation and mindset, you can set yourself up for success.

Modified on Sep 19, 2023 Audio available

PHP for Mac: get started fast using Laravel Valet PHP for Mac: get started fast using Laravel Valet

Use your Mac as an ideal PHP environment thanks to the power of Laravel Valet. You can finally say goodbye to Docker!

Modified on Aug 12, 2023

7 Laravel RESTful APIs best practices for 2023 7 Laravel RESTful APIs best practices for 2023

Master the art of crafting RESTful APIs with Laravel thanks to these best practices.

Modified on Oct 10, 2023

Gold sponsors New

  • Wire Elements
    Beautiful handcrafted Livewire components.
    Check site
Your business here

Partners

If you buy from one of my partners below, I will be compensated at no cost to you. These are services I use or used, and 100% stand behind.

  • Scalable and reliable VPS hosting.
    Bonus: $200 of free credits
    Check site
  • The Google Analytics alternative without compromise.
    Free trial: 30 days
    Bonus: $10 off your first invoice
    Check site
  • Flare
    Track PHP and JavaScript errors in one place.
    Free trial: 10 days
    Check site
  • Keep track of your Google rankings.
    Free trial: 7 days
    Promo code: WELCOME30
    Check site
- / -