GlobalVariablesSniff.php 869 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sniffs\Variables;
  7. use PHP_CodeSniffer\Sniffs\Sniff;
  8. use PHP_CodeSniffer\Files\File;
  9. /**
  10. * Sniff prohibiting usage of global variables.
  11. */
  12. class GlobalVariablesSniff implements Sniff
  13. {
  14. /**
  15. * @inheritdoc
  16. */
  17. public function register()
  18. {
  19. return [T_VARIABLE];
  20. }
  21. /**
  22. * @inheritdoc
  23. */
  24. public function process(File $phpcsFile, $stackPtr)
  25. {
  26. $tokens = $phpcsFile->getTokens();
  27. if (preg_match('/^\$[_A-Z0-9]+$/', $tokens[$stackPtr]['content'])) {
  28. $phpcsFile->addError(
  29. 'Usage of global variables is not allowed: ' . $tokens[$stackPtr]['content'],
  30. $stackPtr,
  31. 'ERROR'
  32. );
  33. return;
  34. }
  35. }
  36. }