Skip to content

Instantly share code, notes, and snippets.

@lyrixx
Last active January 4, 2016 03:09
Show Gist options
  • Select an option

  • Save lyrixx/8559739 to your computer and use it in GitHub Desktop.

Select an option

Save lyrixx/8559739 to your computer and use it in GitHub Desktop.
Play with Patchwork / Parser

Play with Patchwork / Parser

git clone https://gist.github.com/8559739.git try-patchwork-php-parser
cd try-patchwork-php-parser
composer install
vendor/patchwork/php-parser/bin/ppp t1.php
{
"name": "lyrixx/try-patchwork-php-parser",
"authors": [
{
"name": "Grégoire Pineau",
"email": "lyrixx@lyrixx.info"
}
],
"require": {
"patchwork/php-parser": "dev-master@dev"
}
}
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file"
],
"hash": "70bad0b5ed3ecead466edff96240557c",
"packages": [
{
"name": "patchwork/php-parser",
"version": "dev-master",
"source": {
"type": "git",
"url": "https://github.com/nicolas-grekas/Patchwork-PHP-Parser.git",
"reference": "efa452233b275cb144a0f9383e218c1c730d07db"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/nicolas-grekas/Patchwork-PHP-Parser/zipball/efa452233b275cb144a0f9383e218c1c730d07db",
"reference": "efa452233b275cb144a0f9383e218c1c730d07db",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"autoload": {
"psr-0": {
"Patchwork": "class/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"(Apache-2.0 or GPL-2.0)"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com",
"role": "Developer"
}
],
"description": "Fast and extensible PHP code analysis and transformation",
"homepage": "https://github.com/nicolas-grekas/Patchwork-PHP-Parser",
"keywords": [
"parser",
"php",
"tokenizer"
],
"time": "2014-01-22 14:06:57"
}
],
"packages-dev": [
],
"aliases": [
],
"minimum-stability": "stable",
"stability-flags": {
"patchwork/php-parser": 20
},
"platform": [
],
"platform-dev": [
]
}
<?php
function abc($s)
{
return strlen($s) * strlen($s);
}
function def(&$a = 5)
{
$a = 'oups';
return 12;
}
// Patchwork\Shim is not a real function but a parser macro
// that takes the replaced function as first arg
// the replacement function as second
// and the parameters signatures (with optional default value/reference)
// Of course these remplacement are useless and for demo purpose only.
// See https://github.com/nicolas-grekas/Patchwork/blob/master/core/compat/bootup.patchwork.php
// for more useful uses of the feature.
Patchwork\Shim(strlen, abc, $s);
Patchwork\Shim(count, def, &$a = 5);
include __DIR__.'/t2.php';
<?php
$var = 'vvv';
echo strlen($var), "\n";
echo count($var), "\n";
echo $var, "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment