protected function getClasses(string $phpCode): array
  {
    $tokens = token_get_all($phpCode);

    $mode      = '';
    $namespace = '';
    $classes   = [];
    foreach ($tokens as $i => $token)
    {
      // If this token is the namespace declaring, then flag that the next tokens will be the namespace name
      if (is_array($token) && $token[0]==T_NAMESPACE)
      {
        $mode      = 'namespace';
        $namespace = '';
        continue;
      }

      // If this token is the class declaring, then flag that the next tokens will be the class name
      if (is_array($token) && in_array($token[0], [T_CLASS, T_INTERFACE, T_TRAIT]))
      {
        $mode = 'class';
        continue;
      }

      // While we're grabbing the namespace name...
      if ($mode=='namespace')
      {
        // If the token is a string or the namespace separator...
        if (is_array($token) && in_array($token[0], [T_STRING, T_NS_SEPARATOR]))
        {
          //Append the token's value to the name of the namespace
          $namespace .= $token[1];
        }
        elseif (is_array($token) && $token[0]==T_WHITESPACE)
        {
          // Ignore whitespace.
        }
        elseif ($token===';')
        {
          // If the token is the semicolon, then we're done with the namespace declaration
          $mode = '';
        }
        elseif ($token==='{')
        {
          throw new LogicException('Bracketed syntax for namespace not supported.');
        }
        else
        {
          throw new LogicException("Unexpected token %s", print_r($token, true));
        }
      }

      // While we're grabbing the class name...
      if ($mode=='class')
      {
        // If the token is a string, it's the name of the class
        if (is_array($token) && $token[0]==T_STRING)
        {
          // Store the token's value as the class name
          $classes[$token[2]] = ['namespace' => $namespace,
                                 'class'     => $token[1],
                                 'line'      => $token[2]];

          $mode = '';
        }
      }
    }

    return $classes;
  }
protected function getClasses(string $phpCode): array
  {
    $tokens = token_get_all($phpCode);

    $mode      = '';
    $namespace = '';
    $classes   = [];
    foreach ($tokens as $i => $token)
    {
      // If this token is the namespace declaring, then flag that the next tokens will be the namespace name
      if (is_array($token) && $token[0]==T_NAMESPACE)
      {
        $mode      = 'namespace';
        $namespace = '';
        continue;
      }

      // If this token is the class declaring, then flag that the next tokens will be the class name
      if (is_array($token) && in_array($token[0], [T_CLASS, T_INTERFACE, T_TRAIT]))
      {
        $mode = 'class';
        continue;
      }

      // While we're grabbing the namespace name...
      if ($mode=='namespace')
      {
        // If the token is a string or the namespace separator...
        if (is_array($token) && in_array($token[0], [T_STRING, T_NS_SEPARATOR]))
        {
          //Append the token's value to the name of the namespace
          $namespace .= $token[1];
        }
        elseif (is_array($token) && $token[0]==T_WHITESPACE)
        {
          // Ignore whitespace.
        }
        elseif ($token===';')
        {
          // If the token is the semicolon, then we're done with the namespace declaration
          $mode = '';
        }
        elseif ($token==='{')
        {
          throw new LogicException('Bracketed syntax for namespace not supported.');
        }
        else
        {
          throw new LogicException("Unexpected token %s", print_r($token, true));
        }
      }

      // While we're grabbing the class name...
      if ($mode=='class')
      {
        // If the token is a string, it's the name of the class
        if (is_array($token) && $token[0]==T_STRING)
        {
          // Store the token's value as the class name
          $classes[$token[2]] = ['namespace' => $namespace,
                                 'class'     => $token[1],
                                 'line'      => $token[2]];

          $mode = '';
        }
      }
    }

    return $classes;
  }
protected function getClasses ( string $ phpCode ) : array { $ tokens = token_get_all ( $ phpCode ) ; $ mode = '' ; $ namespace = '' ; $ classes = [ ] ; foreach ( $ tokens as $ i => $ token ) { // If this token is the namespace declaring, then flag that the next tokens will be the namespace name if ( is_array ( $ token ) && $ token [ 0 ] == T_NAMESPACE ) { $ mode = 'namespace' ; $ namespace = '' ; continue ; } // If this token is the class declaring, then flag that the next tokens will be the class name if ( is_array ( $ token ) && in_array ( $ token [ 0 ] , [ T_CLASS , T_INTERFACE , T_TRAIT ] ) ) { $ mode = 'class' ; continue ; } // While we're grabbing the namespace name... if ( $ mode == 'namespace' ) { // If the token is a string or the namespace separator... if ( is_array ( $ token ) && in_array ( $ token [ 0 ] , [ T_STRING , T_NS_SEPARATOR ] ) ) { //Append the token's value to the name of the namespace $ namespace .= $ token [ 1 ] ; } elseif ( is_array ( $ token ) && $ token [ 0 ] == T_WHITESPACE ) { // Ignore whitespace. } elseif ( $ token === ';' ) { // If the token is the semicolon, then we're done with the namespace declaration $ mode = '' ; } elseif ( $ token === '{' ) { throw new LogicException ( 'Bracketed syntax for namespace not supported.' ) ; } else { throw new LogicException ( "Unexpected token %s" , print_r ( $ token , true ) ) ; } } // While we're grabbing the class name... if ( $ mode == 'class' ) { // If the token is a string, it's the name of the class if ( is_array ( $ token ) && $ token [ 0 ] == T_STRING ) { // Store the token's value as the class name $ classes [ $ token [ 2 ] ] = [ 'namespace' => $ namespace , 'class' => $ token [ 1 ] , 'line' => $ token [ 2 ] ] ; $ mode = '' ; } } } return $ classes ; }
Returns an array with classes and namespaces defined in PHP code.

@param string $phpCode The PHP code.

@return array
Returns an array with classes and namespaces defined in PHP code .