From a379bebd03099ee056a5ae7466f0699423064ede Mon Sep 17 00:00:00 2001 From: addshore Date: Sun, 3 Apr 2016 11:16:42 +0300 Subject: [PATCH] Split ClassCollector class into own file Change-Id: I61210d1e963e0cd1278debff8f6826c5c79a88e9 --- autoload.php | 2 +- includes/utils/AutoloadGenerator.php | 114 -------------------------- includes/utils/ClassCollector.php | 115 +++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 115 deletions(-) create mode 100644 includes/utils/ClassCollector.php diff --git a/autoload.php b/autoload.php index fd4f873d41..67a235f11f 100644 --- a/autoload.php +++ b/autoload.php @@ -230,7 +230,7 @@ $wgAutoloadLocalClasses = [ 'CheckSyntax' => __DIR__ . '/maintenance/checkSyntax.php', 'CheckUsernames' => __DIR__ . '/maintenance/checkUsernames.php', 'ChronologyProtector' => __DIR__ . '/includes/db/ChronologyProtector.php', - 'ClassCollector' => __DIR__ . '/includes/utils/AutoloadGenerator.php', + 'ClassCollector' => __DIR__ . '/includes/utils/ClassCollector.php', 'CleanupAncientTables' => __DIR__ . '/maintenance/cleanupAncientTables.php', 'CleanupBlocks' => __DIR__ . '/maintenance/cleanupBlocks.php', 'CleanupPreferences' => __DIR__ . '/maintenance/cleanupPreferences.php', diff --git a/includes/utils/AutoloadGenerator.php b/includes/utils/AutoloadGenerator.php index 916e2f881c..4101879d77 100644 --- a/includes/utils/AutoloadGenerator.php +++ b/includes/utils/AutoloadGenerator.php @@ -250,117 +250,3 @@ EOD return str_replace( '\\', '/', $path ); } } - -/** - * Reads PHP code and returns the FQCN of every class defined within it. - */ -class ClassCollector { - - /** - * @var string Current namespace - */ - protected $namespace = ''; - - /** - * @var array List of FQCN detected in this pass - */ - protected $classes; - - /** - * @var array Token from token_get_all() that started an expect sequence - */ - protected $startToken; - - /** - * @var array List of tokens that are members of the current expect sequence - */ - protected $tokens; - - /** - * @var string $code PHP code (including namespace = ''; - $this->classes = []; - $this->startToken = null; - $this->tokens = []; - - foreach ( token_get_all( $code ) as $token ) { - if ( $this->startToken === null ) { - $this->tryBeginExpect( $token ); - } else { - $this->tryEndExpect( $token ); - } - } - - return $this->classes; - } - - /** - * Determine if $token begins the next expect sequence. - * - * @param array $token - */ - protected function tryBeginExpect( $token ) { - if ( is_string( $token ) ) { - return; - } - switch ( $token[0] ) { - case T_NAMESPACE: - case T_CLASS: - case T_INTERFACE: - case T_TRAIT: - case T_DOUBLE_COLON: - $this->startToken = $token; - } - } - - /** - * Accepts the next token in an expect sequence - * - * @param array - */ - protected function tryEndExpect( $token ) { - switch ( $this->startToken[0] ) { - case T_DOUBLE_COLON: - // Skip over T_CLASS after T_DOUBLE_COLON because this is something like - // "self::static" which accesses the class name. It doens't define a new class. - $this->startToken = null; - break; - case T_NAMESPACE: - if ( $token === ';' || $token === '{' ) { - $this->namespace = $this->implodeTokens() . '\\'; - } else { - $this->tokens[] = $token; - } - break; - - case T_CLASS: - case T_INTERFACE: - case T_TRAIT: - $this->tokens[] = $token; - if ( is_array( $token ) && $token[0] === T_STRING ) { - $this->classes[] = $this->namespace . $this->implodeTokens(); - } - } - } - - /** - * Returns the string representation of the tokens within the - * current expect sequence and resets the sequence. - * - * @return string - */ - protected function implodeTokens() { - $content = []; - foreach ( $this->tokens as $token ) { - $content[] = is_string( $token ) ? $token : $token[1]; - } - - $this->tokens = []; - $this->startToken = null; - - return trim( implode( '', $content ), " \n\t" ); - } -} diff --git a/includes/utils/ClassCollector.php b/includes/utils/ClassCollector.php new file mode 100644 index 0000000000..25231df6f9 --- /dev/null +++ b/includes/utils/ClassCollector.php @@ -0,0 +1,115 @@ +namespace = ''; + $this->classes = []; + $this->startToken = null; + $this->tokens = []; + + foreach ( token_get_all( $code ) as $token ) { + if ( $this->startToken === null ) { + $this->tryBeginExpect( $token ); + } else { + $this->tryEndExpect( $token ); + } + } + + return $this->classes; + } + + /** + * Determine if $token begins the next expect sequence. + * + * @param array $token + */ + protected function tryBeginExpect( $token ) { + if ( is_string( $token ) ) { + return; + } + switch ( $token[0] ) { + case T_NAMESPACE: + case T_CLASS: + case T_INTERFACE: + case T_TRAIT: + case T_DOUBLE_COLON: + $this->startToken = $token; + } + } + + /** + * Accepts the next token in an expect sequence + * + * @param array + */ + protected function tryEndExpect( $token ) { + switch ( $this->startToken[0] ) { + case T_DOUBLE_COLON: + // Skip over T_CLASS after T_DOUBLE_COLON because this is something like + // "self::static" which accesses the class name. It doens't define a new class. + $this->startToken = null; + break; + case T_NAMESPACE: + if ( $token === ';' || $token === '{' ) { + $this->namespace = $this->implodeTokens() . '\\'; + } else { + $this->tokens[] = $token; + } + break; + + case T_CLASS: + case T_INTERFACE: + case T_TRAIT: + $this->tokens[] = $token; + if ( is_array( $token ) && $token[0] === T_STRING ) { + $this->classes[] = $this->namespace . $this->implodeTokens(); + } + } + } + + /** + * Returns the string representation of the tokens within the + * current expect sequence and resets the sequence. + * + * @return string + */ + protected function implodeTokens() { + $content = []; + foreach ( $this->tokens as $token ) { + $content[] = is_string( $token ) ? $token : $token[1]; + } + + $this->tokens = []; + $this->startToken = null; + + return trim( implode( '', $content ), " \n\t" ); + } +} -- 2.20.1