2017 © Pedro Peláez
 

library bypass-finals

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

image

dg/bypass-finals

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

  • Friday, July 20, 2018
  • by david@grudl.com
  • Repository
  • 6 Watchers
  • 19 Stars
  • 3,076 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 4 Versions
  • 43 % Grown

The README.md

bypass-finals, (*1)

Downloads this Month Tests Latest Stable Version License, (*2)

 , (*3)

Introduction

BypassFinals effortlessly strips away final and readonly keywords from your PHP code on-the-fly. This handy tool makes it possible to mock final methods and classes, seamlessly integrating with popular testing frameworks like PHPUnit, Mockery, or Nette Tester., (*4)

 , (*5)

Installation

The easiest way to install BypassFinals is via Composer. Just run the following command in your project directory:, (*6)

composer require dg/bypass-finals --dev

It pretty much runs everywhere: PHP 7.1 through 8.4 are all supported!, (*7)

 , (*8)

Usage

To get BypassFinals up and running, just invoke:, (*9)

DG\BypassFinals::enable();

Make sure to call this method early, preferably immediately after your vendor/autoload.php is loaded, to ensure all classes are processed before they are used., (*10)

Note that final internal PHP classes like Closure are not mockable., (*11)

To avoid removing readonly keywords, you can disable this feature by passing a parameter:, (*12)

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

To narrow down the application scope of BypassFinals, use a whitelist to specify directories or files:, (*13)

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

Or, conversely, you can specify which paths not to search using DG\BypassFinals::denyPaths(). This gives you finer control and can solve issues with certain frameworks and libraries., (*14)

Enhance performance by caching transformed files. Make sure the cache directory already exists:, (*15)

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

For integration with PHPUnit 10 or newer, simply add BypassFinals as an extension in your PHPUnit XML configuration file:, (*16)

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

Optionally you can configure BypassFinals in your phpunit.xml file:, (*17)

<extensions>
    <bootstrap class="DG\BypassFinals\PHPUnitExtension">
        <parameter name="bypassFinal" value="true"/>
        <parameter name="bypassReadOnly" value="false"/>
        <parameter name="cacheDirectory" value="./cache"/>
    </bootstrap>
</extensions>

 , (*18)

Troubleshooting

If you encounter issues with BypassFinals not working as expected, you can use the debugInfo() method to gain insights into its internal state. Calling this method will output valuable information to help diagnose the problem:, (*19)

DG\BypassFinals::debugInfo();

This will display:, (*20)

  • Configuration: Whether BypassFinals is enabled for removing final and/or readonly keywords.
  • BypassFinals startup call stack: The sequence of function calls that led to DG\BypassFinals::enable(), helping you verify when and where BypassFinals was started.
  • Classes loaded before BypassFinals startup: A list of classes that were already loaded in PHP before BypassFinals was started. Keywords in these classes cannot be removed, as the classes are already defined. This can help identify potential conflicts or reasons why certain classes aren't being modified.
  • Modified files: A list of the files that BypassFinals has successfully modified. If the file containing a class you expect to be modified isn't in this list, it suggests a problem with path matching or the timing of the BypassFinals startup.

By examining this output, you can better understand how BypassFinals is configured and whether it's operating on the intended files and classes. This can significantly speed up the process of identifying and resolving issues., (*21)

 , (*22)

Do you like this project?

Check out my other innovative open-source projects that might catch your interest:, (*23)

✅ [Latte](https://latte.nette.org): The only safe and intuitive templating system for PHP
✅ [Tracy](https://tracy.nette.org): An addictive debugging tool to enhance your development workflow
✅ [PhpGenerator](https://doc.nette.org/en/php-generator): A robust library for generating PHP code with modern features
✅ [Nette Framework](https://nette.org): A thoughtfully engineered and popular web framework.

 , (*24)

Support Project

Donate, (*25)

The Versions

20/07 2018

dev-master

9999999-dev

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

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.6

 

The Development Requires

phpunit testing unit mocking finals

20/07 2018

v1.1.0

1.1.0.0

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

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.6

 

The Development Requires

phpunit testing unit mocking finals

07/06 2018

v1.0.1

1.0.1.0

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

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.6

 

The Development Requires

phpunit testing unit mocking finals

13/09 2017

v1.0

1.0.0.0

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

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.6

 

The Development Requires

phpunit testing unit mocking finals