ForbiddenParameterShadowSuperGlobalsSniff.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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\FunctionDeclarations;
  11. use PHPCompatibility\Sniff;
  12. use PHPCompatibility\PHPCSHelper;
  13. use PHP_CodeSniffer_File as File;
  14. /**
  15. * Detect the use of superglobals as parameters for functions, support for which was removed in PHP 5.4.
  16. *
  17. * {@internal List of superglobals is maintained in the parent class.}
  18. *
  19. * PHP version 5.4
  20. *
  21. * @link https://www.php.net/manual/en/migration54.incompatible.php
  22. *
  23. * @since 7.0.0
  24. */
  25. class ForbiddenParameterShadowSuperGlobalsSniff extends Sniff
  26. {
  27. /**
  28. * Register the tokens to listen for.
  29. *
  30. * @since 7.0.0
  31. * @since 7.1.3 Allows for closures.
  32. *
  33. * @return array
  34. */
  35. public function register()
  36. {
  37. return array(
  38. \T_FUNCTION,
  39. \T_CLOSURE,
  40. );
  41. }
  42. /**
  43. * Processes the test.
  44. *
  45. * @since 7.0.0
  46. *
  47. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  48. * @param int $stackPtr The position of the current token.
  49. *
  50. * @return void
  51. */
  52. public function process(File $phpcsFile, $stackPtr)
  53. {
  54. if ($this->supportsAbove('5.4') === false) {
  55. return;
  56. }
  57. // Get all parameters from function signature.
  58. $parameters = PHPCSHelper::getMethodParameters($phpcsFile, $stackPtr);
  59. if (empty($parameters) || \is_array($parameters) === false) {
  60. return;
  61. }
  62. foreach ($parameters as $param) {
  63. if (isset($this->superglobals[$param['name']]) === true) {
  64. $error = 'Parameter shadowing super global (%s) causes fatal error since PHP 5.4';
  65. $errorCode = $this->stringToErrorCode(substr($param['name'], 1)) . 'Found';
  66. $data = array($param['name']);
  67. $phpcsFile->addError($error, $param['token'], $errorCode, $data);
  68. }
  69. }
  70. }
  71. }