View source  
  <?php
namespace Drupal\Sniffs\NamingConventions;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
class ValidGlobalSniff implements Sniff {
  public $coreGlobals = array(
    '$argc',
    '$argv',
    '$base_insecure_url',
    '$base_path',
    '$base_root',
    '$base_secure_url',
    '$base_theme_info',
    '$base_url',
    '$channel',
    '$conf',
    '$config_directories',
    '$cookie_domain',
    '$databases',
    '$db_prefix',
    '$db_type',
    '$db_url',
    '$drupal_hash_salt',
    '$drupal_test_info',
    '$element',
    '$forum_topic_list_header',
    '$image',
    '$install_state',
    '$installed_profile',
    '$is_https',
    '$is_https_mock',
    '$item',
    '$items',
    '$language',
    '$language_content',
    '$language_url',
    '$locks',
    '$menu_admin',
    '$multibyte',
    '$pager_limits',
    '$pager_page_array',
    '$pager_total',
    '$pager_total_items',
    '$tag',
    '$theme',
    '$theme_engine',
    '$theme_info',
    '$theme_key',
    '$theme_path',
    '$timers',
    '$update_free_access',
    '$update_rewrite_settings',
    '$user',
  );
  
  public function register() {
    return array(
      T_GLOBAL,
    );
  }
  
  
  public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile
      ->getTokens();
    $varToken = $stackPtr;
    
    $ignore = Tokens::$emptyTokens;
    $ignore[] = T_SEMICOLON;
    while (($varToken = $phpcsFile
      ->findNext($ignore, $varToken + 1, null, true, null, true)) !== false) {
      if ($tokens[$varToken]['code'] === T_VARIABLE && in_array($tokens[$varToken]['content'], $this->coreGlobals) === false && $tokens[$varToken]['content'][1] !== '_') {
        $error = 'global variables should start with a single underscore followed by the module and another underscore';
        $phpcsFile
          ->addError($error, $varToken, 'GlobalUnderScore');
      }
    }
  }
}