From a6c023022a80d239a15565c416e8adca723dd4ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bojan=20=C3=90urkovi=C4=87?= Date: Thu, 11 Jul 2024 00:28:47 +0200 Subject: [PATCH 1/5] 1.x-phpunit10-compatibility - bump version of nikic/php-parser to 5 and update instantiation of a Parser --- .idea/monkey-patch.iml | 2 +- .idea/php.xml | 16 ++++++++++++++++ composer.json | 10 +++++----- src/MonkeyPatchManager.php | 34 +++++++++++++++++++++++++-------- src/Patcher/AbstractPatcher.php | 10 +++++----- src/bootstrap.php | 5 +++-- 6 files changed, 56 insertions(+), 21 deletions(-) diff --git a/.idea/monkey-patch.iml b/.idea/monkey-patch.iml index a0c987b..0a3df29 100644 --- a/.idea/monkey-patch.iml +++ b/.idea/monkey-patch.iml @@ -2,8 +2,8 @@ - + diff --git a/.idea/php.xml b/.idea/php.xml index 0c0236e..bfea9e8 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -1,5 +1,15 @@ + + + + + + @@ -44,9 +54,15 @@ + + + + \ No newline at end of file diff --git a/composer.json b/composer.json index 7ca6d0c..55f5c46 100644 --- a/composer.json +++ b/composer.json @@ -1,9 +1,9 @@ { - "name": "kenjis/monkey-patch", + "name": "bdurkovic/monkey-patch", "type": "library", "description": "Monkey patching for exit(), functions, methods, and constants", "keywords": ["monkey patch","monkey patching","mock","testing","phpunit"], - "homepage": "https://github.com/kenjis/monkey-patch", + "homepage": "https://github.com/bdurkovic/monkey-patch", "license": "MIT", "authors": [ { @@ -15,12 +15,12 @@ "php": "^7.3 || ^8.0", "ext-openssl": "*", "ext-tokenizer": "*", - "phpunit/phpunit": "^9.5", - "nikic/php-parser": "^4.10" + "nikic/php-parser": "^5.1" }, "require-dev": { "bamarni/composer-bin-plugin": "^1.4", - "kenjis/phpunit-helper": "^1.1" + "kenjis/phpunit-helper": "^1.1", + "phpunit/phpunit": "^9.5" }, "autoload": { "psr-4": { 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', From f6a77d1c4868b077553d0b06f63ab95ba38a1099 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bojan=20=C3=90urkovi=C4=87?= Date: Thu, 11 Jul 2024 09:04:04 +0200 Subject: [PATCH 2/5] chore: update nikic/php-parser to 5.1 --- .idea/monkey-patch.iml | 2 +- composer.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.idea/monkey-patch.iml b/.idea/monkey-patch.iml index 0a3df29..a0c987b 100644 --- a/.idea/monkey-patch.iml +++ b/.idea/monkey-patch.iml @@ -2,8 +2,8 @@ + - diff --git a/composer.json b/composer.json index 55f5c46..b8922b4 100644 --- a/composer.json +++ b/composer.json @@ -1,9 +1,9 @@ { - "name": "bdurkovic/monkey-patch", + "name": "kenjis/monkey-patch", "type": "library", "description": "Monkey patching for exit(), functions, methods, and constants", "keywords": ["monkey patch","monkey patching","mock","testing","phpunit"], - "homepage": "https://github.com/bdurkovic/monkey-patch", + "homepage": "https://github.com/kenjis/monkey-patch", "license": "MIT", "authors": [ { From 65c079e698162c8bc99dba063647b531d4e5fab3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bojan=20=C4=90urkovi=C4=87?= Date: Thu, 11 Jul 2024 09:13:09 +0200 Subject: [PATCH 3/5] Delete .idea/php.xml --- .idea/php.xml | 68 --------------------------------------------------- 1 file changed, 68 deletions(-) delete mode 100644 .idea/php.xml diff --git a/.idea/php.xml b/.idea/php.xml deleted file mode 100644 index bfea9e8..0000000 --- a/.idea/php.xml +++ /dev/null @@ -1,68 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file From 286c392748fc984e5dbad7e02bd2ad4c72f066ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bojan=20=C3=90urkovi=C4=87?= Date: Thu, 11 Jul 2024 09:15:40 +0200 Subject: [PATCH 4/5] chore: update nikic/php-parser to 5.1 - remove duplicate phpunit req --- composer.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 4fa9168..7aa0396 100644 --- a/composer.json +++ b/composer.json @@ -20,8 +20,7 @@ "require-dev": { "phpunit/phpunit": "^9.5", "bamarni/composer-bin-plugin": "^1.4", - "kenjis/phpunit-helper": "^1.1", - "phpunit/phpunit": "^9.5" + "kenjis/phpunit-helper": "^1.1" }, "autoload": { "psr-4": { From 640c1ce18247d92c3f57aa35dfda2d1a279739fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bojan=20=C3=90urkovi=C4=87?= Date: Thu, 11 Jul 2024 09:19:12 +0200 Subject: [PATCH 5/5] add back .idea/php.xml --- .idea/php.xml | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 .idea/php.xml diff --git a/.idea/php.xml b/.idea/php.xml new file mode 100644 index 0000000..1a10fe4 --- /dev/null +++ b/.idea/php.xml @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +