Skip to content
Closed
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
20 changes: 20 additions & 0 deletions compat-reflector-munge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

if ( ! file_exists( __DIR__ . '/vendor/phpdocumentor/reflection/src/phpDocumentor/Reflection/BaseReflector.php' ) ) {
echo "Run 'composer install' first.\n";
exit(1);
}

$contents = file_get_contents( __DIR__ . '/vendor/phpdocumentor/reflection/src/phpDocumentor/Reflection/BaseReflector.php' );

$contents = preg_replace(
'/function getShortName().+?{/is',
'function getShortName() {
if ( method_exists( $this->node, "isAnonymous" ) && $this->node->isAnonymous() ) {
return "AnonymousClass";
}
',
$contents
);

file_put_contents( __DIR__ . '/vendor/phpdocumentor/reflection/src/phpDocumentor/Reflection/BaseReflector.php', $contents );
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
76 changes: 76 additions & 0 deletions lib/class-file-reflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(?<!\$|\[)([A-Z_]{5,})\[ ([^]]+)\]/',
function( $m ) {
return '$' . $m[1] . '[' . $m[2] . ']';
},
$this->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.
*
Expand Down