RemovedNamespacedAssertSniff.php 2.8 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\FunctionNameRestrictions;
  11. use PHPCompatibility\Sniff;
  12. use PHP_CodeSniffer_File as File;
  13. /**
  14. * Detect declaration of a namespaced function called `assert()`.
  15. *
  16. * As of PHP 7.3, a compile-time deprecation warning will be thrown when a function
  17. * called `assert()` is declared. In PHP 8 this will become a compile-error.
  18. *
  19. * Methods are unaffected.
  20. * Global, non-namespaced, `assert()` function declarations were always a fatal
  21. * "function already declared" error, so not the concern of this sniff.
  22. *
  23. * PHP version 7.3
  24. *
  25. * @link https://www.php.net/manual/en/migration73.deprecated.php#migration73.deprecated.core.assert
  26. * @link https://wiki.php.net/rfc/deprecations_php_7_3#defining_a_free-standing_assert_function
  27. * @link https://www.php.net/manual/en/function.assert.php
  28. *
  29. * @since 9.0.0
  30. */
  31. class RemovedNamespacedAssertSniff extends Sniff
  32. {
  33. /**
  34. * Scopes in which an `assert` function can be declared without issue.
  35. *
  36. * @since 9.0.0
  37. *
  38. * @var array
  39. */
  40. private $scopes = array(
  41. \T_CLASS,
  42. \T_INTERFACE,
  43. \T_TRAIT,
  44. \T_CLOSURE,
  45. );
  46. /**
  47. * Returns an array of tokens this test wants to listen for.
  48. *
  49. * @since 9.0.0
  50. *
  51. * @return array
  52. */
  53. public function register()
  54. {
  55. // Enrich the scopes list.
  56. if (\defined('T_ANON_CLASS')) {
  57. $this->scopes[] = \T_ANON_CLASS;
  58. }
  59. return array(\T_FUNCTION);
  60. }
  61. /**
  62. * Processes this test, when one of its tokens is encountered.
  63. *
  64. * @since 9.0.0
  65. *
  66. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  67. * @param int $stackPtr The position of the current token in the
  68. * stack passed in $tokens.
  69. *
  70. * @return void
  71. */
  72. public function process(File $phpcsFile, $stackPtr)
  73. {
  74. if ($this->supportsAbove('7.3') === false) {
  75. return;
  76. }
  77. $funcName = $phpcsFile->getDeclarationName($stackPtr);
  78. if (strtolower($funcName) !== 'assert') {
  79. return;
  80. }
  81. if ($phpcsFile->hasCondition($stackPtr, $this->scopes) === true) {
  82. return;
  83. }
  84. if ($this->determineNamespace($phpcsFile, $stackPtr) === '') {
  85. // Not a namespaced function declaration. This may be a parse error, but not our concern.
  86. return;
  87. }
  88. $phpcsFile->addWarning('Declaring a free-standing function called assert() is deprecated since PHP 7.3.', $stackPtr, 'Found');
  89. }
  90. }