Download the PHP package asmblah/php-code-shift without Composer
On this page you can find all versions of the php package asmblah/php-code-shift. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package php-code-shift
PHP Code Shift
Allows running PHP code to be monkey-patched either ahead of time or on the fly.
Why?
To allow stubbing of built-in functions during testing, for example.
Usage
Install this package with Composer:
Hooking built-in functions
runner.php
substr_test.php
The output will be:
Hooking classes
References to a class may be replaced with references to a different class. This only works
for statically-referenced classes, i.e. where it is referenced with a bareword, e.g. new MyClass
.
Dynamic/variable references are not supported, e.g. new $myClassName
as they can only be resolved at runtime.
Any matching types are not replaced - the replacement class must extend the original class or interface in order to pass type checks.
runner.php
class_test.php
The output will be:
Static method calls (MyClass::myStaticMethod()
) and class constant fetches (MyClass:MY_CONST
) are also supported.
Custom stream handlers
Filesystem access is intercepted using a stream wrapper for the special file://
and phar://
protocols.
By default, the stream wrapper uses a stream handler (a PHP Code Shift -specific concept)
that for the most part delegates to PHP's native filesystem handling.
A custom stream handler may be registered in its place in order to hook into filesystem operations.
See Nytris Antilag and Nytris Boost for examples of libraries that register custom stream handlers.
Example
Limitations
Functionality is extremely limited at the moment, you may well be better off using one of the alternatives listed in See also below.
- Does not yet support
eval(...)
. FunctionHookShiftType
does not yet support variable function calls.FunctionHookShiftType
does not yet supportcall_user_func(...)
and friends, nor any other functions accepting callable parameters that may refer to functions.
See also
- dg/bypass-finals, which uses the same technique as this library.
- PHP PreProcessor
- Patchwork
All versions of php-code-shift with dependencies
nikic/php-parser Version ^4.18 || ^5.0
nytris/nytris Version ^0.1
psr/log Version ^1|^2|^3
symfony/filesystem Version ^5.4