From: Brad Jorsch Date: Tue, 1 May 2018 17:13:37 +0000 (-0400) Subject: Have ClassCollector ignore PHP 7 anonymous classes X-Git-Tag: 1.34.0-rc.0~5565^2 X-Git-Url: https://git.cyclocoop.org/%27.WWW_URL.%27admin/?a=commitdiff_plain;h=e5abec805272980cf02d2f9b8ab2d56983a668db;p=lhc%2Fweb%2Fwiklou.git Have ClassCollector ignore PHP 7 anonymous classes PHP 7 introduces anonymous classes with a syntax like $instance = new class() extends Foo { ... }; ClassCollector is incorrectly detecting this as a class named "() extends Foo". This patch fixes that by having it ignore "new class" in much the same way it currently ignores "::class". Change-Id: I4d1985a9c04be71f7bea6cb7b61dcea74f44a6e2 --- diff --git a/includes/utils/AutoloadGenerator.php b/includes/utils/AutoloadGenerator.php index 0e2ef85dfa..98d2c0e550 100644 --- a/includes/utils/AutoloadGenerator.php +++ b/includes/utils/AutoloadGenerator.php @@ -396,6 +396,7 @@ class ClassCollector { case T_INTERFACE: case T_TRAIT: case T_DOUBLE_COLON: + case T_NEW: $this->startToken = $token; break; case T_STRING: @@ -418,6 +419,12 @@ class ClassCollector { // "self::static" which accesses the class name. It doens't define a new class. $this->startToken = null; break; + case T_NEW: + // Skip over T_CLASS after T_NEW because this is a PHP 7 anonymous class. + if ( !is_array( $token ) || $token[0] !== T_WHITESPACE ) { + $this->startToken = null; + } + break; case T_NAMESPACE: if ( $token === ';' || $token === '{' ) { $this->namespace = $this->implodeTokens() . '\\'; diff --git a/tests/phpunit/includes/utils/ClassCollectorTest.php b/tests/phpunit/includes/utils/ClassCollectorTest.php index 9e5163f9ce..9c7c50f0f6 100644 --- a/tests/phpunit/includes/utils/ClassCollectorTest.php +++ b/tests/phpunit/includes/utils/ClassCollectorTest.php @@ -43,6 +43,10 @@ class ClassCollectorTest extends PHPUnit\Framework\TestCase { "namespace Example;\nclass Foo {}\nclass_alias( Foo::class, 'Bar' );", [ 'Example\Foo', 'Bar' ], ], + [ + "new class() extends Foo {}", + [] + ] ]; }