Skip to content

Commit dd8217e

Browse files
committed
Add regex to reform core PHP into a form that this PHP Parser can understand. This also requires a patch against phpdocumentor/reflection.
1 parent 7fc2227 commit dd8217e

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ After that install the dependencies using composer in the parser directory:
2323
composer install
2424
```
2525

26+
TEMPORARILY until the plugin supports the newer PHP syntax used within Core.
27+
```bash
28+
composer compat:munge
29+
```
30+
2631
## Running
2732
Activate the plugin first:
2833

compat-reflector-munge.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
if ( ! file_exists( __DIR__ . '/vendor/phpdocumentor/reflection/src/phpDocumentor/Reflection/BaseReflector.php' ) ) {
4+
echo "Run 'composer install' first.\n";
5+
exit(1);
6+
}
7+
8+
$contents = file_get_contents( __DIR__ . '/vendor/phpdocumentor/reflection/src/phpDocumentor/Reflection/BaseReflector.php' );
9+
10+
$contents = preg_replace(
11+
'/function getShortName().+?{/is',
12+
'function getShortName() {
13+
if ( method_exists( $this->node, "isAnonymous" ) && $this->node->isAnonymous() ) {
14+
return "AnonymousClass";
15+
}
16+
',
17+
$contents
18+
);
19+
20+
file_put_contents( __DIR__ . '/vendor/phpdocumentor/reflection/src/phpDocumentor/Reflection/BaseReflector.php', $contents );

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"scripts" : {
3737
"test": "phpunit",
3838
"test:watch": "phpunit-watcher watch < /dev/tty",
39-
"test:coverage": "php -d xdebug.mode=coverage ./vendor/bin/phpunit --coverage-html coverage"
39+
"test:coverage": "php -d xdebug.mode=coverage ./vendor/bin/phpunit --coverage-html coverage",
40+
"compat:munge": "php compat-reflector-munge.php"
4041
},
4142
"autoload" : {
4243
"classmap": ["lib"],

lib/class-file-reflector.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,82 @@ class File_Reflector extends FileReflector {
4444
*/
4545
protected $last_doc = null;
4646

47+
public function __construct( $file, $validate = false, $encoding = 'utf-8' ) {
48+
parent::__construct( $file, false /* Force validation off */, $encoding );
49+
50+
// Nullable types are unknown to the parser, pretend they don't exist.
51+
// The nullable type should be listed in the PHPDoc anyway.
52+
$this->contents = preg_replace_callback(
53+
'/(function [a-z0-9-_]+\([^{:]*?)\)(\:\s*\??\S+)?\s*[{;]/im',
54+
static function( $m ) {
55+
return preg_replace( '/\?(\S+)/', '$1', $m[0] ) ?: $m[0];
56+
},
57+
$this->contents
58+
);
59+
60+
// Inline callables... ( $var )( $param ) can usualy be rewritten as $var( $param ).
61+
$this->contents = preg_replace_callback(
62+
'/(?:\s)(\(\s*\$[^()]+\))(\(.+\))/',
63+
static function( $m ) {
64+
return trim( $m[1], '() ' ) . trim( $m[2] );
65+
},
66+
$this->contents
67+
);
68+
69+
// Short list() syntax...
70+
$this->contents = preg_replace_callback(
71+
'/(\s)\[(\s*\$[^();.*]{3,}?)\]\s*=/ism',
72+
static function( $m ) {
73+
return $m[1] . 'list( ' . $m[2] . ' ) =';
74+
},
75+
$this->contents
76+
);
77+
78+
// Visibility on constants
79+
$this->contents = preg_replace_callback(
80+
'/\b(public|protected|private)\s+const\s+/im',
81+
static function( $m ) {
82+
return 'const ';
83+
},
84+
$this->contents
85+
);
86+
87+
// constant arrays weren't supported.
88+
$this->contents = preg_replace_callback(
89+
'/\b(?<!\$|\[)([A-Z_]{5,})\[ ([^]]+)\]/',
90+
function( $m ) {
91+
return '$' . $m[1] . '[' . $m[2] . ']';
92+
},
93+
$this->contents
94+
);
95+
96+
// Static calls on static calls. Mostly in tests.
97+
$this->contents = preg_replace_callback(
98+
'/([\$\[\]_a-z]+)::([\$\[\]_a-z]+)::([\$\[\]_a-z]+)/',
99+
static function( $m ) {
100+
//var_dump( $m );
101+
if ( 'self' === $m[1] || 'static' === $m[1] ) {
102+
return '$this->' . $m[2] . '->' . $m[3];
103+
}
104+
105+
return $m[1] . '->' . $m[2]. '->' . $m[3];
106+
},
107+
$this->contents
108+
);
109+
110+
// Static calls on function results. Mostly in tests.
111+
$this->contents = preg_replace_callback(
112+
'/([a-z_])\(\)::/',
113+
static function( $m ) {
114+
return $m[1] . '()->';
115+
},
116+
$this->contents
117+
);
118+
119+
// Recalculate the hash, since we probably changed the contents.
120+
$this->hash = md5($this->contents);
121+
}
122+
47123
/**
48124
* Add hooks to the queue and update the node stack when we enter a node.
49125
*

0 commit comments

Comments
 (0)