NewConstantArraysUsingDefineSniff.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\InitialValue;
  11. use PHPCompatibility\Sniff;
  12. use PHP_CodeSniffer_File as File;
  13. /**
  14. * Detect declaration of constants using `define()` with a (constant) array value
  15. * as supported since PHP 7.0.
  16. *
  17. * PHP version 7.0
  18. *
  19. * @link https://www.php.net/manual/en/migration70.new-features.php#migration70.new-features.define-array
  20. * @link https://www.php.net/manual/en/language.constants.syntax.php
  21. *
  22. * @since 7.0.0
  23. * @since 9.0.0 Renamed from `ConstantArraysUsingDefineSniff` to `NewConstantArraysUsingDefineSniff`.
  24. */
  25. class NewConstantArraysUsingDefineSniff extends Sniff
  26. {
  27. /**
  28. * Returns an array of tokens this test wants to listen for.
  29. *
  30. * @since 7.0.0
  31. *
  32. * @return array
  33. */
  34. public function register()
  35. {
  36. return array(\T_STRING);
  37. }
  38. /**
  39. * Processes this test, when one of its tokens is encountered.
  40. *
  41. * @since 7.0.0
  42. *
  43. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  44. * @param int $stackPtr The position of the current token in the
  45. * stack passed in $tokens.
  46. *
  47. * @return void
  48. */
  49. public function process(File $phpcsFile, $stackPtr)
  50. {
  51. if ($this->supportsBelow('5.6') !== true) {
  52. return;
  53. }
  54. $tokens = $phpcsFile->getTokens();
  55. $ignore = array(
  56. \T_DOUBLE_COLON => true,
  57. \T_OBJECT_OPERATOR => true,
  58. \T_FUNCTION => true,
  59. \T_CONST => true,
  60. );
  61. $prevToken = $phpcsFile->findPrevious(\T_WHITESPACE, ($stackPtr - 1), null, true);
  62. if (isset($ignore[$tokens[$prevToken]['code']]) === true) {
  63. // Not a call to a PHP function.
  64. return;
  65. }
  66. $functionLc = strtolower($tokens[$stackPtr]['content']);
  67. if ($functionLc !== 'define') {
  68. return;
  69. }
  70. $secondParam = $this->getFunctionCallParameter($phpcsFile, $stackPtr, 2);
  71. if (isset($secondParam['start'], $secondParam['end']) === false) {
  72. return;
  73. }
  74. $targetNestingLevel = 0;
  75. if (isset($tokens[$secondParam['start']]['nested_parenthesis'])) {
  76. $targetNestingLevel = \count($tokens[$secondParam['start']]['nested_parenthesis']);
  77. }
  78. $array = $phpcsFile->findNext(array(\T_ARRAY, \T_OPEN_SHORT_ARRAY), $secondParam['start'], ($secondParam['end'] + 1));
  79. if ($array !== false) {
  80. if ((isset($tokens[$array]['nested_parenthesis']) === false && $targetNestingLevel === 0) || \count($tokens[$array]['nested_parenthesis']) === $targetNestingLevel) {
  81. $phpcsFile->addError(
  82. 'Constant arrays using define are not allowed in PHP 5.6 or earlier',
  83. $array,
  84. 'Found'
  85. );
  86. }
  87. }
  88. }
  89. }