<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
/**
* Reads PHP code and returns the FQCN of every class defined within it.
protected $tokens;
/**
- * @var string $code PHP code (including <?php) to detect class names from
+ * @var array Class alias with target/name fields
+ */
+ protected $alias;
+
+ /**
+ * @param string $code PHP code (including <?php) to detect class names from
* @return array List of FQCN detected within the tokens
*/
public function getClasses( $code ) {
$this->namespace = '';
$this->classes = [];
$this->startToken = null;
+ $this->alias = null;
$this->tokens = [];
+ // HACK: The PHP tokenizer is slow (T225730).
+ // Speed it up by reducing the input to the three kinds of statement we care about:
+ // - namespace X;
+ // - [final] [abstract] class X … {}
+ // - class_alias( … );
+ $lines = [];
+ $matches = null;
+ preg_match_all(
+ // phpcs:ignore Generic.Files.LineLength.TooLong
+ '#^\t*(?:namespace |(final )?(abstract )?(class|interface|trait) |class_alias\()[^;{]+[;{]\s*\}?#m',
+ $code,
+ $matches
+ );
+ if ( isset( $matches[0][0] ) ) {
+ foreach ( $matches[0] as $match ) {
+ $match = trim( $match );
+ if ( substr( $match, -1 ) === '{' ) {
+ // Keep it balanced
+ $match .= '}';
+ }
+ $lines[] = $match;
+ }
+ }
+ $code = '<?php ' . implode( "\n", $lines ) . "\n";
+
foreach ( token_get_all( $code ) as $token ) {
if ( $this->startToken === null ) {
$this->tryBeginExpect( $token );
if ( is_string( $token ) ) {
return;
}
+ // Note: When changing class name discovery logic,
+ // AutoLoaderStructureTest.php may also need to be updated.
switch ( $token[0] ) {
case T_NAMESPACE:
case T_CLASS:
case T_INTERFACE:
case T_TRAIT:
case T_DOUBLE_COLON:
+ case T_NEW:
$this->startToken = $token;
+ break;
+ case T_STRING:
+ if ( $token[1] === 'class_alias' ) {
+ $this->startToken = $token;
+ $this->alias = [];
+ }
}
}
/**
* Accepts the next token in an expect sequence
*
- * @param array
+ * @param array $token
*/
protected function tryEndExpect( $token ) {
switch ( $this->startToken[0] ) {
// "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() . '\\';
}
break;
+ case T_STRING:
+ if ( $this->alias !== null ) {
+ // Flow 1 - Two string literals:
+ // - T_STRING class_alias
+ // - '('
+ // - T_CONSTANT_ENCAPSED_STRING 'TargetClass'
+ // - ','
+ // - T_WHITESPACE
+ // - T_CONSTANT_ENCAPSED_STRING 'AliasName'
+ // - ')'
+ // Flow 2 - Use of ::class syntax for first parameter
+ // - T_STRING class_alias
+ // - '('
+ // - T_STRING TargetClass
+ // - T_DOUBLE_COLON ::
+ // - T_CLASS class
+ // - ','
+ // - T_WHITESPACE
+ // - T_CONSTANT_ENCAPSED_STRING 'AliasName'
+ // - ')'
+ if ( $token === '(' ) {
+ // Start of a function call to class_alias()
+ $this->alias = [ 'target' => false, 'name' => false ];
+ } elseif ( $token === ',' ) {
+ // Record that we're past the first parameter
+ if ( $this->alias['target'] === false ) {
+ $this->alias['target'] = true;
+ }
+ } elseif ( is_array( $token ) && $token[0] === T_CONSTANT_ENCAPSED_STRING ) {
+ if ( $this->alias['target'] === true ) {
+ // We already saw a first argument, this must be the second.
+ // Strip quotes from the string literal.
+ $this->alias['name'] = substr( $token[1], 1, -1 );
+ }
+ } elseif ( $token === ')' ) {
+ // End of function call
+ $this->classes[] = $this->alias['name'];
+ $this->alias = null;
+ $this->startToken = null;
+ } elseif ( !is_array( $token ) || (
+ $token[0] !== T_STRING &&
+ $token[0] !== T_DOUBLE_COLON &&
+ $token[0] !== T_CLASS &&
+ $token[0] !== T_WHITESPACE
+ ) ) {
+ // Ignore this call to class_alias() - compat/Timestamp.php
+ $this->alias = null;
+ $this->startToken = null;
+ }
+ }
+ break;
+
case T_CLASS:
case T_INTERFACE:
case T_TRAIT: