dg/bypass-finals

Removes final keyword from source code on-the-fly and allows mocking of final methods and classes

Installs: 10 530 621

Dependents: 301

Suggesters: 1

Security: 0

Stars: 427

Watchers: 12

Forks: 28

Open Issues: 6

v1.6.0 2023-11-19 22:19 UTC

This package is auto-updated.

Last update: 2024-02-14 07:10:45 UTC


README

Downloads this Month Tests Latest Stable Version License

Introduction

Removes final and readonly keywords from source code on-the-fly and allows mocking of final methods and classes. It can be used together with any test tool such as PHPUnit, Mockery or Nette Tester.

Installation

The recommended way to install is through Composer:

composer require dg/bypass-finals --dev

It requires PHP version 7.1 and supports PHP up to 8.3.

Usage

Simply call this:

DG\BypassFinals::enable();

You need to enable it before the classes you want to remove the keywords from are loaded. So call it as soon as possible, preferably right after vendor/autoload.php is loaded.

Note that final internal PHP classes like Closure cannot be mocked.

The removal of readonly keywords can be disabled using the parameter:

DG\BypassFinals::enable(bypassReadOnly: false);

You can choose to only bypass keywords in specific files or directories:

DG\BypassFinals::setWhitelist([
    '*/Nette/*',
]);

This gives you finer control and can solve issues with certain frameworks and libraries.

You can try to increase performance by using the cache (the directory must exist):

DG\BypassFinals::setCacheDirectory(__DIR__ . '/cache');

To register BypassFinals in PHPUnit 10, simply add the extension to the PHPUnit XML configuration file:

<extensions>
	<bootstrap class="DG\BypassFinals\PHPUnitExtension"/>
</extensions>

Support Project

Do you like BypassFinals?

Donate