Split ClassCollector class into own file
authoraddshore <addshorewiki@gmail.com>
Sun, 3 Apr 2016 08:16:42 +0000 (11:16 +0300)
committeraddshore <addshorewiki@gmail.com>
Sun, 3 Apr 2016 08:16:42 +0000 (11:16 +0300)
Change-Id: I61210d1e963e0cd1278debff8f6826c5c79a88e9

autoload.php
includes/utils/AutoloadGenerator.php
includes/utils/ClassCollector.php [new file with mode: 0644]

index fd4f873..67a235f 100644 (file)
@@ -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',
index 916e2f8..4101879 100644 (file)
@@ -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 <?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->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 (file)
index 0000000..25231df
--- /dev/null
@@ -0,0 +1,115 @@
+<?php
+
+/**
+ * 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 <?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->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" );
+       }
+}