ForbiddenToStringParametersSniff.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\MethodUse;
  11. use PHPCompatibility\Sniff;
  12. use PHP_CodeSniffer_File as File;
  13. use PHP_CodeSniffer_Tokens as Tokens;
  14. /**
  15. * As of PHP 5.3, the `__toString()` magic method can no longer be passed arguments.
  16. *
  17. * Sister-sniff to `PHPCompatibility.FunctionDeclarations.ForbiddenToStringParameters`.
  18. *
  19. * PHP version 5.3
  20. *
  21. * @link https://www.php.net/manual/en/migration53.incompatible.php
  22. * @link https://www.php.net/manual/en/language.oop5.magic.php#object.tostring
  23. *
  24. * @since 9.2.0
  25. */
  26. class ForbiddenToStringParametersSniff extends Sniff
  27. {
  28. /**
  29. * Returns an array of tokens this test wants to listen for.
  30. *
  31. * @since 9.2.0
  32. *
  33. * @return array
  34. */
  35. public function register()
  36. {
  37. return array(
  38. \T_DOUBLE_COLON,
  39. \T_OBJECT_OPERATOR,
  40. );
  41. }
  42. /**
  43. * Processes this test, when one of its tokens is encountered.
  44. *
  45. * @since 9.2.0
  46. *
  47. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  48. * @param int $stackPtr The position of the current token
  49. * in the stack passed in $tokens.
  50. *
  51. * @return void
  52. */
  53. public function process(File $phpcsFile, $stackPtr)
  54. {
  55. if ($this->supportsAbove('5.3') === false) {
  56. return;
  57. }
  58. $tokens = $phpcsFile->getTokens();
  59. $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
  60. if ($nextNonEmpty === false || $tokens[$nextNonEmpty]['code'] !== \T_STRING) {
  61. /*
  62. * Not a method call.
  63. *
  64. * Note: This disregards method calls with the method name in a variable, like:
  65. * $method = '__toString';
  66. * $obj->$method();
  67. * However, that would be very hard to examine reliably anyway.
  68. */
  69. return;
  70. }
  71. if (strtolower($tokens[$nextNonEmpty]['content']) !== '__tostring') {
  72. // Not a call to the __toString() method.
  73. return;
  74. }
  75. $openParens = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextNonEmpty + 1), null, true);
  76. if ($openParens === false || $tokens[$openParens]['code'] !== \T_OPEN_PARENTHESIS) {
  77. // Not a method call.
  78. return;
  79. }
  80. $closeParens = $phpcsFile->findNext(Tokens::$emptyTokens, ($openParens + 1), null, true);
  81. if ($closeParens === false || $tokens[$closeParens]['code'] === \T_CLOSE_PARENTHESIS) {
  82. // Not a method call.
  83. return;
  84. }
  85. // If we're still here, then this is a call to the __toString() magic method passing parameters.
  86. $phpcsFile->addError(
  87. 'The __toString() magic method will no longer accept passed arguments since PHP 5.3',
  88. $stackPtr,
  89. 'Passed'
  90. );
  91. }
  92. }