NewDirectCallsToCloneSniff.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * Detect direct calls to the `__clone()` magic method, which is allowed since PHP 7.0.
  16. *
  17. * "Doing calls like `$obj->__clone()` is now allowed. This was the only magic method
  18. * that had a compile-time check preventing some calls to it, which doesn't make sense.
  19. * If we allow all other magic methods to be called, there's no reason to forbid this one."
  20. *
  21. * PHP version 7.0
  22. *
  23. * @link https://wiki.php.net/rfc/abstract_syntax_tree#directly_calling_clone_is_allowed
  24. * @link https://www.php.net/manual/en/language.oop5.cloning.php
  25. *
  26. * @since 9.1.0
  27. */
  28. class NewDirectCallsToCloneSniff extends Sniff
  29. {
  30. /**
  31. * Tokens which indicate class internal use.
  32. *
  33. * @since 9.3.2
  34. *
  35. * @var array
  36. */
  37. protected $classInternal = array(
  38. \T_PARENT => true,
  39. \T_SELF => true,
  40. \T_STATIC => true,
  41. );
  42. /**
  43. * Returns an array of tokens this test wants to listen for.
  44. *
  45. * @since 9.1.0
  46. *
  47. * @return array
  48. */
  49. public function register()
  50. {
  51. return array(
  52. \T_DOUBLE_COLON,
  53. \T_OBJECT_OPERATOR,
  54. );
  55. }
  56. /**
  57. * Processes this test, when one of its tokens is encountered.
  58. *
  59. * @since 9.1.0
  60. *
  61. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  62. * @param int $stackPtr The position of the current token in
  63. * the stack passed in $tokens.
  64. *
  65. * @return void
  66. */
  67. public function process(File $phpcsFile, $stackPtr)
  68. {
  69. if ($this->supportsBelow('5.6') === false) {
  70. return;
  71. }
  72. $tokens = $phpcsFile->getTokens();
  73. $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
  74. if ($nextNonEmpty === false || $tokens[$nextNonEmpty]['code'] !== \T_STRING) {
  75. /*
  76. * Not a method call.
  77. *
  78. * Note: This disregards method calls with the method name in a variable, like:
  79. * $method = '__clone';
  80. * $obj->$method();
  81. * However, that would be very hard to examine reliably anyway.
  82. */
  83. return;
  84. }
  85. if (strtolower($tokens[$nextNonEmpty]['content']) !== '__clone') {
  86. // Not a call to the __clone() method.
  87. return;
  88. }
  89. $nextNextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextNonEmpty + 1), null, true);
  90. if ($nextNextNonEmpty === false || $tokens[$nextNextNonEmpty]['code'] !== \T_OPEN_PARENTHESIS) {
  91. // Not a method call.
  92. return;
  93. }
  94. $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
  95. if ($prevNonEmpty === false || isset($this->classInternal[$tokens[$prevNonEmpty]['code']])) {
  96. // Class internal call to __clone().
  97. return;
  98. }
  99. $phpcsFile->addError(
  100. 'Direct calls to the __clone() magic method are not allowed in PHP 5.6 or earlier.',
  101. $nextNonEmpty,
  102. 'Found'
  103. );
  104. }
  105. }