PHP code example of nazonohito51 / monkey-patch

1. Go to this page and download the library: Download nazonohito51/monkey-patch library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nazonohito51 / monkey-patch example snippets



namespace SomeNamespace;

class SomeClass
{
    public function someMethod()
    {
        $client = new \GuzzleHttp\Client();
        return $client->request('GET', 'https://your.production.env.com/api/end_point');
    }
}

(new \MonkeyPatch\Patcher())->whenLoad('/path/to/src')->patchBy(new class extends \MonkeyPatch\Filters\AbstractCodeFilter {
    public function transformCode(string $code): string
    {
        // fix url.
        // 'https://your.production.env.com' -> 'https://your.test.env.com'
        return preg_replace('/https:\/\/your\.production\.env\.com/', 'https://your.test.env.com', $code);
    }
});


namespace SomeNamespace;

class SomeClass
{
    public function someMethod()
    {
        $client = new \GuzzleHttp\Client();
        return $client->request('GET', 'https://your.test.env.com/api/end_point');
    }
}

(new \MonkeyPatch\Patcher())->whenLoad('/path/to/src')->patchBy(new class extends \MonkeyPatch\Filters\AbstractCodeFilter {
    public function transformCode(string $code): string
    {
        // fix $code by your logic.
        return $code;
    }
});

(new \MonkeyPatch\Patcher())->whenLoad('/path/to/src')->patchBy(new class extends \MonkeyPatch\Filters\AbstractAstFilter {
    protected function getVisitor(): NodeVisitorAbstract
    {
        // fix AST by your visitor.
        return new class extends NodeVisitorAbstract {
            //...
        }
    }
});