From dd8217e2bfa4d4ac375cb5f593039c96ec0048e8 Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Mon, 8 Sep 2025 16:47:06 +1000 Subject: [PATCH] Add regex to reform core PHP into a form that this PHP Parser can understand. This also requires a patch against phpdocumentor/reflection. --- README.md | 5 +++ compat-reflector-munge.php | 20 ++++++++++ composer.json | 3 +- lib/class-file-reflector.php | 76 ++++++++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 compat-reflector-munge.php diff --git a/README.md b/README.md index 6c4390b..feeb9de 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,11 @@ After that install the dependencies using composer in the parser directory: composer install ``` +TEMPORARILY until the plugin supports the newer PHP syntax used within Core. +```bash +composer compat:munge +``` + ## Running Activate the plugin first: diff --git a/compat-reflector-munge.php b/compat-reflector-munge.php new file mode 100644 index 0000000..e330a18 --- /dev/null +++ b/compat-reflector-munge.php @@ -0,0 +1,20 @@ +node, "isAnonymous" ) && $this->node->isAnonymous() ) { + return "AnonymousClass"; + } + ', + $contents +); + +file_put_contents( __DIR__ . '/vendor/phpdocumentor/reflection/src/phpDocumentor/Reflection/BaseReflector.php', $contents ); \ No newline at end of file diff --git a/composer.json b/composer.json index aa09726..441daa2 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,8 @@ "scripts" : { "test": "phpunit", "test:watch": "phpunit-watcher watch < /dev/tty", - "test:coverage": "php -d xdebug.mode=coverage ./vendor/bin/phpunit --coverage-html coverage" + "test:coverage": "php -d xdebug.mode=coverage ./vendor/bin/phpunit --coverage-html coverage", + "compat:munge": "php compat-reflector-munge.php" }, "autoload" : { "classmap": ["lib"], diff --git a/lib/class-file-reflector.php b/lib/class-file-reflector.php index 62f401d..5cd04ab 100644 --- a/lib/class-file-reflector.php +++ b/lib/class-file-reflector.php @@ -44,6 +44,82 @@ class File_Reflector extends FileReflector { */ protected $last_doc = null; + public function __construct( $file, $validate = false, $encoding = 'utf-8' ) { + parent::__construct( $file, false /* Force validation off */, $encoding ); + + // Nullable types are unknown to the parser, pretend they don't exist. + // The nullable type should be listed in the PHPDoc anyway. + $this->contents = preg_replace_callback( + '/(function [a-z0-9-_]+\([^{:]*?)\)(\:\s*\??\S+)?\s*[{;]/im', + static function( $m ) { + return preg_replace( '/\?(\S+)/', '$1', $m[0] ) ?: $m[0]; + }, + $this->contents + ); + + // Inline callables... ( $var )( $param ) can usualy be rewritten as $var( $param ). + $this->contents = preg_replace_callback( + '/(?:\s)(\(\s*\$[^()]+\))(\(.+\))/', + static function( $m ) { + return trim( $m[1], '() ' ) . trim( $m[2] ); + }, + $this->contents + ); + + // Short list() syntax... + $this->contents = preg_replace_callback( + '/(\s)\[(\s*\$[^();.*]{3,}?)\]\s*=/ism', + static function( $m ) { + return $m[1] . 'list( ' . $m[2] . ' ) ='; + }, + $this->contents + ); + + // Visibility on constants + $this->contents = preg_replace_callback( + '/\b(public|protected|private)\s+const\s+/im', + static function( $m ) { + return 'const '; + }, + $this->contents + ); + + // constant arrays weren't supported. + $this->contents = preg_replace_callback( + '/\b(?contents + ); + + // Static calls on static calls. Mostly in tests. + $this->contents = preg_replace_callback( + '/([\$\[\]_a-z]+)::([\$\[\]_a-z]+)::([\$\[\]_a-z]+)/', + static function( $m ) { + //var_dump( $m ); + if ( 'self' === $m[1] || 'static' === $m[1] ) { + return '$this->' . $m[2] . '->' . $m[3]; + } + + return $m[1] . '->' . $m[2]. '->' . $m[3]; + }, + $this->contents + ); + + // Static calls on function results. Mostly in tests. + $this->contents = preg_replace_callback( + '/([a-z_])\(\)::/', + static function( $m ) { + return $m[1] . '()->'; + }, + $this->contents + ); + + // Recalculate the hash, since we probably changed the contents. + $this->hash = md5($this->contents); + } + /** * Add hooks to the queue and update the node stack when we enter a node. *