/php/pear/PHP/CodeSniffer/Standards/Squiz/Sniffs/Operators/ValidLogicalOperatorsSniff.php

https://gitlab.com/trang1104/portable_project · PHP · 87 lines · 32 code · 13 blank · 42 comment · 1 complexity · 1bdd5d5dcc354b1bee5952da7dc8c811 MD5 · raw file

  1. <?php
  2. /**
  3. * Squiz_Sniffs_Operators_ValidLogicalOperatorsSniff.
  4. *
  5. * PHP version 5
  6. *
  7. * @category PHP
  8. * @package PHP_CodeSniffer
  9. * @author Greg Sherwood <gsherwood@squiz.net>
  10. * @author Marc McIntyre <mmcintyre@squiz.net>
  11. * @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
  12. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  13. * @link http://pear.php.net/package/PHP_CodeSniffer
  14. */
  15. /**
  16. * Squiz_Sniffs_Operators_ValidLogicalOperatorsSniff.
  17. *
  18. * Checks to ensure that the logical operators 'and' and 'or' are not used.
  19. * Use the && and || operators instead.
  20. *
  21. * @category PHP
  22. * @package PHP_CodeSniffer
  23. * @author Greg Sherwood <gsherwood@squiz.net>
  24. * @author Marc McIntyre <mmcintyre@squiz.net>
  25. * @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
  26. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  27. * @version Release: 1.3.3
  28. * @link http://pear.php.net/package/PHP_CodeSniffer
  29. */
  30. class Squiz_Sniffs_Operators_ValidLogicalOperatorsSniff implements PHP_CodeSniffer_Sniff
  31. {
  32. /**
  33. * Returns an array of tokens this test wants to listen for.
  34. *
  35. * @return array
  36. */
  37. public function register()
  38. {
  39. return array(
  40. T_LOGICAL_AND,
  41. T_LOGICAL_OR,
  42. T_LOGICAL_XOR,
  43. );
  44. }//end register()
  45. /**
  46. * Processes this test, when one of its tokens is encountered.
  47. *
  48. * @param PHP_CodeSniffer_File $phpcsFile The current file being scanned.
  49. * @param int $stackPtr The position of the current token in the
  50. * stack passed in $tokens.
  51. *
  52. * @return void
  53. */
  54. public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  55. {
  56. $tokens = $phpcsFile->getTokens();
  57. $replacements = array(
  58. 'and' => '&&',
  59. 'or' => '||',
  60. 'xor' => '^',
  61. );
  62. $operator = strtolower($tokens[$stackPtr]['content']);
  63. if (isset($replacements[$operator]) === false) {
  64. return;
  65. }
  66. $error = 'Logical operator "%s" is prohibited; use "%s" instead';
  67. $data = array(
  68. $operator,
  69. $replacements[$operator],
  70. );
  71. $phpcsFile->addError($error, $stackPtr, 'NotAllowed', $data);
  72. }//end process()
  73. }//end class
  74. ?>