Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Aug 14, 2015
1 parent f75a58e commit 64747c5
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 83 deletions.
25 changes: 12 additions & 13 deletions application/tests/_ci_phpunit_test/CIPHPUnitTestReflection.php
Expand Up @@ -12,7 +12,7 @@ class CIPHPUnitTestReflection
{
public static function getPrivateMethodInvoker($class, $method)
{
$ref_method = new \ReflectionMethod($class, $method);
$ref_method = new ReflectionMethod($class, $method);
$ref_method->setAccessible(true);
$obj = (gettype($class) === 'object') ? $class : null;

Expand All @@ -22,30 +22,29 @@ public static function getPrivateMethodInvoker($class, $method)
};
}

public static function setPrivateProperty($class, $property, $value)
protected static function getAccessibleRefProperty($class, $property)
{
if (is_object($class)) {
$ref_class = new \ReflectionObject($class);
$ref_class = new ReflectionObject($class);
} else {
$ref_class = new \ReflectionClass($class);
$ref_class = new ReflectionClass($class);
}

$ref_property = $ref_class->getProperty($property);
$ref_property->setAccessible(true);

return $ref_property;
}

public static function setPrivateProperty($class, $property, $value)
{
$ref_property = self::getAccessibleRefProperty($class, $property);
$ref_property->setValue($value);
}

public static function getPrivateProperty($class, $property)
{
if (is_object($class)) {
$ref_class = new ReflectionObject($class);
} else {
$ref_class = new ReflectionClass($class);
}

$ref_property = $ref_class->getProperty($property);
$ref_property->setAccessible(true);

$ref_property = self::getAccessibleRefProperty($class, $property);
return $ref_property->getValue();
}
}
Expand Up @@ -273,7 +273,8 @@ protected static function execPatchers($source)
foreach (self::$patcher_list as $classname)
{
$classname = 'Kenjis\MonkeyPatch\Patcher\\' . $classname;
list($source, $patched_this) = $classname::patch($source);
$patcher = new $classname;
list($source, $patched_this) = $patcher->patch($source);
$patched = $patched || $patched_this;
}

Expand Down
@@ -0,0 +1,52 @@
<?php
/**
* Part of CI PHPUnit Test
*
* @author Kenji Suzuki <https://github.com/kenjis>
* @license MIT License
* @copyright 2015 Kenji Suzuki
* @link https://github.com/kenjis/ci-phpunit-test
*/

namespace Kenjis\MonkeyPatch\Patcher;

use PhpParser\Parser;
use PhpParser\Lexer;
use PhpParser\NodeTraverser;

abstract class AbstractPatcher
{
protected $node_visitor;

public static $replacement;

public function patch($source)
{
$patched = false;
static::$replacement = [];

$parser = new Parser(new Lexer(
['usedAttributes' => ['startTokenPos', 'endTokenPos']]
));
$traverser = new NodeTraverser;
$traverser->addVisitor($this->node_visitor);

$ast_orig = $parser->parse($source);
$traverser->traverse($ast_orig);

if (static::$replacement !== [])
{
$patched = true;
$new_source = static::generateNewSource($source);
}
else
{
$new_source = $source;
}

return [
$new_source,
$patched,
];
}
}
Expand Up @@ -10,22 +10,19 @@

namespace Kenjis\MonkeyPatch\Patcher;

if (! class_exists('PhpParser\Autoloader'))
if (! class_exists('PhpParser\Autoloader', false))
{
require __DIR__ . '/../third_party/PHP-Parser/lib/bootstrap.php';
require __DIR__ . '/AbstractPatcher.php';
}
require __DIR__ . '/FunctionPatcher/NodeVisitor.php';
require __DIR__ . '/FunctionPatcher/Proxy.php';

use LogicException;

use PhpParser\Parser;
use PhpParser\Lexer;
use PhpParser\NodeTraverser;

use Kenjis\MonkeyPatch\Patcher\FunctionPatcher\NodeVisitor;

class FunctionPatcher
class FunctionPatcher extends AbstractPatcher
{
private static $lock_function_list = false;

Expand Down Expand Up @@ -85,6 +82,11 @@ class FunctionPatcher

public static $replacement;

public function __construct()
{
$this->node_visitor = new NodeVisitor();
}

protected static function checkLock($error_msg)
{
if (self::$lock_function_list)
Expand Down Expand Up @@ -159,36 +161,6 @@ public static function isBlacklisted($name)
return false;
}

public static function patch($source)
{
$patched = false;
self::$replacement = [];

$parser = new Parser(new Lexer(
['usedAttributes' => ['startTokenPos', 'endTokenPos']]
));
$traverser = new NodeTraverser;
$traverser->addVisitor(new NodeVisitor());

$ast_orig = $parser->parse($source);
$traverser->traverse($ast_orig);

if (self::$replacement !== [])
{
$patched = true;
$new_source = self::generateNewSource($source);
}
else
{
$new_source = $source;
}

return [
$new_source,
$patched,
];
}

protected static function generateNewSource($source)
{
$tokens = token_get_all($source);
Expand Down
Expand Up @@ -10,57 +10,29 @@

namespace Kenjis\MonkeyPatch\Patcher;

if (! class_exists('PhpParser\Autoloader'))
if (! class_exists('PhpParser\Autoloader', false))
{
require __DIR__ . '/../third_party/PHP-Parser/lib/bootstrap.php';
require __DIR__ . '/AbstractPatcher.php';
}
require __DIR__ . '/MethodPatcher/NodeVisitor.php';
require __DIR__ . '/MethodPatcher/PatchManager.php';

use LogicException;

use PhpParser\Parser;
use PhpParser\Lexer;
use PhpParser\NodeTraverser;

use Kenjis\MonkeyPatch\Patcher\MethodPatcher\NodeVisitor;

class MethodPatcher
class MethodPatcher extends AbstractPatcher
{
const CODE = <<<'EOL'
if (($__ret__ = \__PatchManager__::getReturn(__CLASS__, __FUNCTION__, func_get_args())) !== __GO_TO_ORIG__) return $__ret__;
EOL;

public static $replacement;

public static function patch($source)
public function __construct()
{
$patched = false;
self::$replacement = [];

$parser = new Parser(new Lexer(
['usedAttributes' => ['startTokenPos', 'endTokenPos']]
));
$traverser = new NodeTraverser;
$traverser->addVisitor(new NodeVisitor());

$ast_orig = $parser->parse($source);
$traverser->traverse($ast_orig);

if (self::$replacement !== [])
{
$patched = true;
$new_source = self::generateNewSource($source);
}
else
{
$new_source = $source;
}

return [
$new_source,
$patched,
];
$this->node_visitor = new NodeVisitor();
}

protected static function generateNewSource($source)
Expand Down

0 comments on commit 64747c5

Please sign in to comment.