Giter Club home page Giter Club logo

patchwork's Introduction

Patchwork 1.4 (.phar)

Patchwork implements the redefinition (monkey-patching) of user-defined methods in PHP.

Internally, it uses a stream wrapper on file:// to inject a simple interceptor snippet to the beginning of every method.

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

  • 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

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

The current version of Patchwork is not suggested for AOP and other kinds of production usage. The next one will switch from stream wrappers to stream filters, which will ensure interoperability with opcode caches.

patchwork's People

Contributors

antecedent avatar deminy avatar alexey-zaharchenko avatar samsonasik avatar adri avatar rarescosma avatar phuedx avatar spudley avatar

Watchers

Pierre du Plessis avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.