IsNullSniff.php 843 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sniffs\MicroOptimizations;
  7. use PHP_CodeSniffer\Sniffs\Sniff;
  8. use PHP_CodeSniffer\Files\File;
  9. class IsNullSniff implements Sniff
  10. {
  11. /**
  12. * @var string
  13. */
  14. protected $blacklist = 'is_null';
  15. /**
  16. * @inheritdoc
  17. */
  18. public function register()
  19. {
  20. return [T_STRING];
  21. }
  22. /**
  23. * @inheritdoc
  24. */
  25. public function process(File $sourceFile, $stackPtr)
  26. {
  27. $tokens = $sourceFile->getTokens();
  28. if ($tokens[$stackPtr]['content'] === $this->blacklist) {
  29. $sourceFile->addError(
  30. "is_null must be avoided. Use strict comparison instead.",
  31. $stackPtr,
  32. 'IsNullUsage'
  33. );
  34. }
  35. }
  36. }