PHPackages                             antecedent/patchwork - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. antecedent/patchwork

ActiveLibrary[Testing &amp; Quality](/categories/testing)

antecedent/patchwork
====================

Method redefinition (monkey-patching) functionality for PHP.

2.2.3(8mo ago)45018.7M—2.5%42[22 issues](https://github.com/antecedent/patchwork/issues)[2 PRs](https://github.com/antecedent/patchwork/pulls)20MITPHPPHP &gt;=7.1.0CI passing

Since Aug 3Pushed 1mo ago12 watchersCompare

[ Source](https://github.com/antecedent/patchwork)[ Packagist](https://packagist.org/packages/antecedent/patchwork)[ Docs](https://antecedent.github.io/patchwork/)[ RSS](/packages/antecedent-patchwork/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (1)Versions (74)Used By (20)

Patchwork
=========

[](#patchwork)

Patchwork implements the redefinition ([monkey-patching](https://en.wikipedia.org/wiki/Monkey_patch)) of functions and methods in PHP. This includes both user-defined and internal callables, which can be functions, class methods, or instance methods. In addition, [many](https://github.com/antecedent/patchwork/blob/master/src/Redefinitions/LanguageConstructs.php) function-like constructs, such as `exit` or `include`, are supported in an analogous way.

Internally, Patchwork uses a [stream wrapper](http://php.net/manual/en/class.streamwrapper.php) on `file://`. In the case of user-defined functions and methods, it is used to inject a simple interceptor snippet to the beginning of every such callable. For the remaining types of callables, various other strategies are applied.

Example: a DIY profiler
-----------------------

[](#example-a-diy-profiler)

```
use function Patchwork\{redefine, relay, getMethod};

$profiling = fopen('profiling.csv', 'w');

redefine('App\*', function(...$args) use ($profiling) {
    $begin = microtime(true);
    relay(); # calls the original definition
    $end = microtime(true);
    fputcsv($profiling, [getMethod(), $end - $begin]);
});
```

Notes
-----

[](#notes)

- *Method redefinition* is the internally preferred metaphor for Patchwork's behavior.
- `restoreAll()` and `restore($handle)` end the lifetime of, respectively, all redefinitions, or only one of them, where `$handle = redefine(...)`.
- Closure `$this` is automatically re-bound to the enclosing class of the method being redefined.
- The behavior of `__CLASS__`, `static::class` etc. inside redefinitions disregards the metaphor. `getClass()`, `getCalledClass()`, `getMethod()` and `getFunction()` from the `Patchwork` namespace should be used instead.

Testing-related uses
--------------------

[](#testing-related-uses)

Patchwork can be used to stub static methods, which, however, is a controversial practice.

It should be applied prudently, that is, only after making oneself familiar with its pitfalls and temptations in other programming languages. For instance, in Javascript, Ruby, Python and some others, the native support for monkey-patching has made its testing-related uses more commonplace than in PHP.

Tests that use monkey-patching are often no longer *unit* tests, because they become sensitive to details of implementation, not only those of interface: for example, such a test might no longer pass after switching from `time()` to `DateTime`.

That being said, they still have their place where the only economically viable alternative is having no tests at all.

Other use cases
---------------

[](#other-use-cases)

Patchwork is not suggested for [AOP](https://en.wikipedia.org/wiki/Aspect-oriented_programming) and other kinds of production usage. Its impact on the application's performance is highly likely to be prohibitively large. Additionally, while no *particular* Patchwork-related security risks are either known or anticipated, please keep in mind that Patchwork was never developed with production environments in mind.

###  Health Score

67

—

FairBetter than 100% of packages

Maintenance76

Regular maintenance activity

Popularity68

Solid adoption and visibility

Community41

Growing community involvement

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 77.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~80 days

Recently: every ~147 days

Total

56

Last Release

243d ago

Major Versions

1.4.3 → 2.0.02016-08-12

PHP version history (3 changes)1.2.6PHP &gt;=5.3.0

1.4.0PHP &gt;=5.4.0

2.2.0PHP &gt;=7.1.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/d767cb9596fb2f64721631ae324cec039e77ba56eb9f8348aab3783e7b52325e?d=identicon)[antecedent](/maintainers/antecedent)

---

Top Contributors

[![antecedent](https://avatars.githubusercontent.com/u/388219?v=4)](https://github.com/antecedent "antecedent (398 commits)")[![jrfnl](https://avatars.githubusercontent.com/u/663378?v=4)](https://github.com/jrfnl "jrfnl (54 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (15 commits)")[![anomiex](https://avatars.githubusercontent.com/u/1030580?v=4)](https://github.com/anomiex "anomiex (9 commits)")[![romm](https://avatars.githubusercontent.com/u/6342901?v=4)](https://github.com/romm "romm (5 commits)")[![kagg-design](https://avatars.githubusercontent.com/u/28291450?v=4)](https://github.com/kagg-design "kagg-design (4 commits)")[![MichaONO](https://avatars.githubusercontent.com/u/143389554?v=4)](https://github.com/MichaONO "MichaONO (3 commits)")[![wvega](https://avatars.githubusercontent.com/u/45068?v=4)](https://github.com/wvega "wvega (2 commits)")[![alexey-zaharchenko](https://avatars.githubusercontent.com/u/1793816?v=4)](https://github.com/alexey-zaharchenko "alexey-zaharchenko (2 commits)")[![computamike](https://avatars.githubusercontent.com/u/464876?v=4)](https://github.com/computamike "computamike (2 commits)")[![deminy](https://avatars.githubusercontent.com/u/865547?v=4)](https://github.com/deminy "deminy (2 commits)")[![emlove](https://avatars.githubusercontent.com/u/1386547?v=4)](https://github.com/emlove "emlove (2 commits)")[![Igor-WPML](https://avatars.githubusercontent.com/u/43881443?v=4)](https://github.com/Igor-WPML "Igor-WPML (2 commits)")[![Stadly](https://avatars.githubusercontent.com/u/7263579?v=4)](https://github.com/Stadly "Stadly (2 commits)")[![pierredup](https://avatars.githubusercontent.com/u/144858?v=4)](https://github.com/pierredup "pierredup (1 commits)")[![rarescosma](https://avatars.githubusercontent.com/u/520445?v=4)](https://github.com/rarescosma "rarescosma (1 commits)")[![clxmstaab](https://avatars.githubusercontent.com/u/47448731?v=4)](https://github.com/clxmstaab "clxmstaab (1 commits)")[![samsonasik](https://avatars.githubusercontent.com/u/459648?v=4)](https://github.com/samsonasik "samsonasik (1 commits)")[![Spudley](https://avatars.githubusercontent.com/u/1370806?v=4)](https://github.com/Spudley "Spudley (1 commits)")[![staabm](https://avatars.githubusercontent.com/u/120441?v=4)](https://github.com/staabm "staabm (1 commits)")

---

Tags

testingaopaspectrunkitredefinitionmonkeypatchinginterception

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/antecedent-patchwork/health.svg)

```
[![Health](https://phpackages.com/badges/antecedent-patchwork/health.svg)](https://phpackages.com/packages/antecedent-patchwork)
```

###  Alternatives

[phpunit/phpunit

The PHP Unit Testing framework.

20.0k910.7M134.8k](/packages/phpunit-phpunit)[brain/monkey

Mocking utility for PHP functions and WordPress plugin API

33412.5M350](/packages/brain-monkey)[phpunit/php-code-coverage

Library that provides collection, processing, and rendering functionality for PHP code coverage information.

8.9k892.4M1.5k](/packages/phpunit-php-code-coverage)[mockery/mockery

Mockery is a simple yet flexible PHP mock object framework

10.7k497.0M23.6k](/packages/mockery-mockery)[behat/behat

Scenario-oriented BDD framework for PHP

4.0k96.8M2.0k](/packages/behat-behat)[symfony/phpunit-bridge

Provides utilities for PHPUnit, especially user deprecation notices management

2.5k201.2M4.2k](/packages/symfony-phpunit-bridge)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
