diff --git a/.idea/php.xml b/.idea/php.xml index 0c0236e..1a10fe4 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -1,5 +1,15 @@ + + + + + + @@ -44,9 +54,16 @@ + + - \ No newline at end of file + + + + diff --git a/composer.json b/composer.json index fd0df87..7aa0396 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/MonkeyPatchManager.php b/src/MonkeyPatchManager.php index 5fa392b..5006cc0 100644 --- a/src/MonkeyPatchManager.php +++ b/src/MonkeyPatchManager.php @@ -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. @@ -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 $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']; + } } /** @@ -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); diff --git a/src/Patcher/AbstractPatcher.php b/src/Patcher/AbstractPatcher.php index 61dd62c..644bf55 100644 --- a/src/Patcher/AbstractPatcher.php +++ b/src/Patcher/AbstractPatcher.php @@ -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; @@ -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(); diff --git a/src/bootstrap.php b/src/bootstrap.php index aedd833..296ac82 100644 --- a/src/bootstrap.php +++ b/src/bootstrap.php @@ -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',