Get your next remote job on LaraJobs.
PHP Packages

Remove final from PHP packages with Unfinalize

Benjamin Crozat
Published on Oct 3, 2023 0 comments Edit on GitHub

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.

Wait, there's more!

Be the first to comment!

Get help or share something of value with other readers!

Great deals for enterprise developers
  • ZoneWatcher
    Get instant alerts on DNS changes across all major providers, before your customers notice.
    25% off for 12 months using the promo code CROZAT.
    Try ZoneWatcher for free
  • Quickly build highly customizable admin panels for Laravel projects.
    20% off on the pro version using the promo code CROZAT.
    Try Backpack for free
  • Summarize and talk to YouTube videos. Bypass ads, sponsors, chit-chat, and get to the point.
    Try Nobinge →
  • Monitor the health of your apps: downtimes, certificates, broken links, and more.
    20% off the first 3 months using the promo code CROZAT.
    Try Oh Dear for free
  • Keep the customers coming; monitor your Google rankings.
    30% off your first month using the promo code WELCOME30
    Try Wincher for free →
The latest community links
- / -