承接 dg/bypass-finals 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

bypass-finals

Downloads this Month Tests Latest Stable Version License

 

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 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.

 

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

Donate

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 dg/bypass-finals 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 28.64M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 572
  • 点击次数: 30
  • 依赖项目数: 510
  • 推荐数: 0

GitHub 信息

  • Stars: 570
  • Watchers: 12
  • Forks: 34
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2017-09-13