ForbiddenBreakContinueVariableArgumentsSniff.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * PHPCompatibility, an external standard for PHP_CodeSniffer.
  4. *
  5. * @package PHPCompatibility
  6. * @copyright 2012-2019 PHPCompatibility Contributors
  7. * @license https://opensource.org/licenses/LGPL-3.0 LGPL3
  8. * @link https://github.com/PHPCompatibility/PHPCompatibility
  9. */
  10. namespace PHPCompatibility\Sniffs\ControlStructures;
  11. use PHPCompatibility\Sniff;
  12. use PHP_CodeSniffer_File as File;
  13. use PHP_CodeSniffer_Tokens as Tokens;
  14. /**
  15. * Detects using 0 and variable numeric arguments on `break` and `continue` statements.
  16. *
  17. * This sniff checks for:
  18. * - Using `break` and/or `continue` with a variable as the numeric argument.
  19. * - Using `break` and/or `continue` with a zero - 0 - as the numeric argument.
  20. *
  21. * PHP version 5.4
  22. *
  23. * @link https://www.php.net/manual/en/migration54.incompatible.php
  24. * @link https://www.php.net/manual/en/control-structures.break.php
  25. * @link https://www.php.net/manual/en/control-structures.continue.php
  26. *
  27. * @since 5.5
  28. * @since 5.6 Now extends the base `Sniff` class.
  29. */
  30. class ForbiddenBreakContinueVariableArgumentsSniff extends Sniff
  31. {
  32. /**
  33. * Error types this sniff handles for forbidden break/continue arguments.
  34. *
  35. * Array key is the error code. Array value will be used as part of the error message.
  36. *
  37. * @since 7.0.5
  38. * @since 7.1.0 Changed from class constants to property.
  39. *
  40. * @var array
  41. */
  42. private $errorTypes = array(
  43. 'variableArgument' => 'a variable argument',
  44. 'zeroArgument' => '0 as an argument',
  45. );
  46. /**
  47. * Returns an array of tokens this test wants to listen for.
  48. *
  49. * @since 5.5
  50. *
  51. * @return array
  52. */
  53. public function register()
  54. {
  55. return array(\T_BREAK, \T_CONTINUE);
  56. }
  57. /**
  58. * Processes this test, when one of its tokens is encountered.
  59. *
  60. * @since 5.5
  61. *
  62. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  63. * @param int $stackPtr The position of the current token in the
  64. * stack passed in $tokens.
  65. *
  66. * @return void
  67. */
  68. public function process(File $phpcsFile, $stackPtr)
  69. {
  70. if ($this->supportsAbove('5.4') === false) {
  71. return;
  72. }
  73. $tokens = $phpcsFile->getTokens();
  74. $nextSemicolonToken = $phpcsFile->findNext(array(\T_SEMICOLON, \T_CLOSE_TAG), ($stackPtr), null, false);
  75. $errorType = '';
  76. for ($curToken = $stackPtr + 1; $curToken < $nextSemicolonToken; $curToken++) {
  77. if ($tokens[$curToken]['type'] === 'T_STRING') {
  78. // If the next non-whitespace token after the string
  79. // is an opening parenthesis then it's a function call.
  80. $openBracket = $phpcsFile->findNext(Tokens::$emptyTokens, $curToken + 1, null, true);
  81. if ($tokens[$openBracket]['code'] === \T_OPEN_PARENTHESIS) {
  82. $errorType = 'variableArgument';
  83. break;
  84. }
  85. } elseif (\in_array($tokens[$curToken]['type'], array('T_VARIABLE', 'T_FUNCTION', 'T_CLOSURE'), true)) {
  86. $errorType = 'variableArgument';
  87. break;
  88. } elseif ($tokens[$curToken]['type'] === 'T_LNUMBER' && $tokens[$curToken]['content'] === '0') {
  89. $errorType = 'zeroArgument';
  90. break;
  91. }
  92. }
  93. if ($errorType !== '') {
  94. $error = 'Using %s on break or continue is forbidden since PHP 5.4';
  95. $errorCode = $errorType . 'Found';
  96. $data = array($this->errorTypes[$errorType]);
  97. $phpcsFile->addError($error, $stackPtr, $errorCode, $data);
  98. }
  99. }
  100. }