dg/bypass-finals
Composer 安装命令:
composer require --dev dg/bypass-finals
包简介
Removes final keyword from source code on-the-fly and allows mocking of final methods and classes
README 文档
README
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.
Installation
The easiest way to install BypassFinals is via Composer. Just run the following command in your project directory:
composer require dg/bypass-finals --dev
Usage
To get BypassFinals up and running, just invoke:
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.
Note that final internal PHP classes like Closure are not mockable.
To avoid removing readonly keywords, you can disable this feature by passing a parameter:
DG\BypassFinals::enable(bypassReadOnly: false);
To narrow down the application scope of BypassFinals, use a whitelist to specify directories or files:
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.
Enhance performance by caching transformed files. Make sure the cache directory already exists:
DG\BypassFinals::setCacheDirectory(__DIR__ . '/cache');
For integration with PHPUnit 10 or newer, simply add BypassFinals as an extension in your PHPUnit XML configuration file:
<extensions> <bootstrap class="DG\BypassFinals\PHPUnitExtension"/> </extensions>
Optionally you can configure BypassFinals in your phpunit.xml file:
<extensions> <bootstrap class="DG\BypassFinals\PHPUnitExtension"> <parameter name="bypassFinal" value="true"/> <parameter name="bypassReadOnly" value="false"/> <parameter name="cacheDirectory" value="./cache"/> </bootstrap> </extensions>
Troubleshooting
Class is still final after enabling BypassFinals
BypassFinals can only strip the final keyword from files loaded after enable() is called. If a class is loaded before that — for example via a files entry in a package's autoload configuration, which Composer loads during require 'vendor/autoload.php' — the class will remain final.
To handle this, include src/bootstrap.php before vendor/autoload.php in your test bootstrap file:
// tests/bootstrap.php require __DIR__ . '/../vendor/dg/bypass-finals/src/bootstrap.php'; require __DIR__ . '/../vendor/autoload.php';
This activates the stream wrapper early enough to intercept every file loaded during autoloading.
If you encounter other 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:
DG\BypassFinals::debugInfo();
This will display:
- Configuration: Whether BypassFinals is enabled for removing
finaland/orreadonlykeywords. - 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.
Do you like this project?
Check out my other innovative open-source projects that might catch your interest:
✅ Latte: The only safe and intuitive templating system for PHP
✅ Tracy: An addictive debugging tool to enhance your development workflow
✅ PhpGenerator: A robust library for generating PHP code with modern features
✅ Nette Framework: A thoughtfully engineered and popular web framework.
Support Project
dg/bypass-finals 适用场景与选型建议
dg/bypass-finals 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 28.64M 次下载、GitHub Stars 达 570, 最近一次更新时间为 2017 年 09 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「testing」 「phpunit」 「unit」 「mocking」 「finals」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dg/bypass-finals 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dg/bypass-finals 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dg/bypass-finals 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
LazyPDO is a set of wrappers over PHP's standard PDO and PDOStatement classes. It enables lazy loading, serialization and decoration.
HiDev plugin for PHPUnit
Testing Suite For Lumen like Laravel does.
Symfony bundle for unigen test generator
A cli tool which generates unit tests.
PHPUnit Testing extensions for HMTL and CSS insures valid HTML and CSS via v.Nu validator
统计信息
- 总下载量: 28.64M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 572
- 点击次数: 30
- 依赖项目数: 510
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2017-09-13