RemovedNewReferenceSniff.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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\Syntax;
  11. use PHPCompatibility\Sniff;
  12. use PHP_CodeSniffer_File as File;
  13. use PHP_CodeSniffer_Tokens as Tokens;
  14. /**
  15. * Detect the use of assigning the return value of `new` by reference.
  16. *
  17. * This syntax has been deprecated since PHP 5.3 and removed in PHP 7.0.
  18. *
  19. * PHP version 5.3
  20. * PHP version 7.0
  21. *
  22. * @link https://wiki.php.net/rfc/remove_deprecated_functionality_in_php7
  23. *
  24. * @since 5.5
  25. * @since 9.0.0 Renamed from `DeprecatedNewReferenceSniff` to `RemovedNewReferenceSniff`.
  26. */
  27. class RemovedNewReferenceSniff extends Sniff
  28. {
  29. /**
  30. * Returns an array of tokens this test wants to listen for.
  31. *
  32. * @since 5.5
  33. *
  34. * @return array
  35. */
  36. public function register()
  37. {
  38. return array(\T_NEW);
  39. }
  40. /**
  41. * Processes this test, when one of its tokens is encountered.
  42. *
  43. * @since 5.5
  44. *
  45. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  46. * @param int $stackPtr The position of the current token in the
  47. * stack passed in $tokens.
  48. *
  49. * @return void
  50. */
  51. public function process(File $phpcsFile, $stackPtr)
  52. {
  53. if ($this->supportsAbove('5.3') === false) {
  54. return;
  55. }
  56. $tokens = $phpcsFile->getTokens();
  57. $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
  58. if ($prevNonEmpty === false || $tokens[$prevNonEmpty]['type'] !== 'T_BITWISE_AND') {
  59. return;
  60. }
  61. $error = 'Assigning the return value of new by reference is deprecated in PHP 5.3';
  62. $isError = false;
  63. $errorCode = 'Deprecated';
  64. if ($this->supportsAbove('7.0') === true) {
  65. $error .= ' and has been removed in PHP 7.0';
  66. $isError = true;
  67. $errorCode = 'Removed';
  68. }
  69. $this->addMessage($phpcsFile, $error, $stackPtr, $isError, $errorCode);
  70. }
  71. }