Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"php": "^7.3 || ^8.0",
"ext-openssl": "*",
"ext-tokenizer": "*",
"nikic/php-parser": "^4.10"
"nikic/php-parser": "^5.1"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
Expand Down
34 changes: 26 additions & 8 deletions src/MonkeyPatchManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ class MonkeyPatchManager
public static $debug = false;

/** @var int */
private static $php_parser = ParserFactory::PREFER_PHP5;
private static $php_version_major = 5;

/** @var int */
private static $php_version_minor = 1;

/**
* The path to the log file if `$debug` is true.
Expand Down Expand Up @@ -94,9 +97,28 @@ public static function getExitExceptionClassname(): string
return self::$exit_exception_classname;
}

public static function getPhpParser(): int
public static function getPhpVersionMajor(): int
{
return self::$php_version_major;
}

public static function getPhpVersionMinor(): int
{
return self::$php_parser;
return self::$php_version_minor;
}

/**
* @param array<string, mixed> $config
*/
protected static function setPhpVersion(array $config): void
{
if (isset($config['php_version_major'])) {
self::$php_version_major = $config['php_version_major'];
}

if (isset($config['php_version_minor'])) {
self::$php_version_minor = $config['php_version_minor'];
}
}

/**
Expand Down Expand Up @@ -155,11 +177,7 @@ protected static function setPaths(array $config): void
public static function init(array $config): void
{
self::setDebug($config);

if (isset($config['php_parser'])) {
self::$php_parser = constant('PhpParser\ParserFactory::' . $config['php_parser']);
}

self::setPhpVersion($config);
self::setDir($config);
self::setPaths($config);

Expand Down
10 changes: 5 additions & 5 deletions src/Patcher/AbstractPatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
namespace Kenjis\MonkeyPatch\Patcher;

use Kenjis\MonkeyPatch\MonkeyPatchManager;
use PhpParser\Lexer;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use PhpParser\ParserFactory;
use PhpParser\PhpVersion;
use PhpParser\PrettyPrinter;

use function assert;
Expand All @@ -38,10 +38,10 @@ public function patch(string $source): array
$patched = false;

$parser = (new ParserFactory())
->create(
MonkeyPatchManager::getPhpParser(),
new Lexer(
['usedAttributes' => ['startTokenPos', 'endTokenPos']]
->createForVersion(
PhpVersion::fromComponents(
MonkeyPatchManager::getPhpVersionMajor(),
MonkeyPatchManager::getPhpVersionMinor()
)
);
$traverser = new NodeTraverser();
Expand Down
5 changes: 3 additions & 2 deletions src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ class_alias(FuncProxy::class, '__FuncProxy__');
// If you want debug log, set `debug` true, and optionally you can set the log file path
'debug' => true,
'log_file' => __DIR__ . '/../tmp/monkey-patch-debug.log',
// PHP Parser: PREFER_PHP7, PREFER_PHP5, ONLY_PHP7, ONLY_PHP5
'php_parser' => 'PREFER_PHP7',
// PHP version
'php_version_major' => 7,
'php_version_minor' => 0,
// Project root directory
'root_dir' => __DIR__ . '/../',
'cache_dir' => __DIR__ . '/../tmp/cache',
Expand Down