NewClassMemberAccessSniff.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 class member access on object instantiation/cloning.
  16. *
  17. * PHP 5.4: Class member access on instantiation has been added, e.g. `(new Foo)->bar()`.
  18. * PHP 7.0: Class member access on cloning has been added, e.g. `(clone $foo)->bar()`.
  19. *
  20. * As of PHP 7.0, class member access on instantiation also works when using curly braces.
  21. * While unclear, this most likely has to do with the Uniform Variable Syntax changes.
  22. *
  23. * PHP version 5.4
  24. * PHP version 7.0
  25. *
  26. * @link https://www.php.net/manual/en/language.oop5.basic.php#example-177
  27. * @link https://www.php.net/manual/en/language.oop5.cloning.php#language.oop5.traits.properties.example
  28. * @link https://www.php.net/manual/en/migration54.new-features.php
  29. * @link https://wiki.php.net/rfc/instance-method-call
  30. * @link https://wiki.php.net/rfc/uniform_variable_syntax
  31. *
  32. * {@internal The reason for splitting the logic of this sniff into different methods is
  33. * to allow re-use of the logic by the PHP 7.4 `RemovedCurlyBraceArrayAccess` sniff.}
  34. *
  35. * @since 8.2.0
  36. * @since 9.3.0 Now also detects class member access on instantiation using curly braces.
  37. */
  38. class NewClassMemberAccessSniff extends Sniff
  39. {
  40. /**
  41. * Returns an array of tokens this test wants to listen for.
  42. *
  43. * @since 8.2.0
  44. *
  45. * @return array
  46. */
  47. public function register()
  48. {
  49. return array(
  50. \T_NEW,
  51. \T_CLONE,
  52. );
  53. }
  54. /**
  55. * Processes this test, when one of its tokens is encountered.
  56. *
  57. * @since 8.2.0
  58. *
  59. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  60. * @param int $stackPtr The position of the current token in the
  61. * stack passed in $tokens.
  62. *
  63. * @return void
  64. */
  65. public function process(File $phpcsFile, $stackPtr)
  66. {
  67. if ($this->supportsBelow('5.6') === false) {
  68. return;
  69. }
  70. $pointers = $this->isClassMemberAccess($phpcsFile, $stackPtr);
  71. if (empty($pointers)) {
  72. return;
  73. }
  74. $tokens = $phpcsFile->getTokens();
  75. $supports53 = $this->supportsBelow('5.3');
  76. $error = 'Class member access on object %s was not supported in PHP %s or earlier';
  77. $data = array('instantiation', '5.3');
  78. $errorCode = 'OnNewFound';
  79. if ($tokens[$stackPtr]['code'] === \T_CLONE) {
  80. $data = array('cloning', '5.6');
  81. $errorCode = 'OnCloneFound';
  82. }
  83. foreach ($pointers as $open => $close) {
  84. $itemData = $data;
  85. $itemErrorCode = $errorCode;
  86. if ($tokens[$stackPtr]['code'] === \T_NEW
  87. && $tokens[$open]['code'] !== \T_OPEN_CURLY_BRACKET
  88. ) {
  89. if ($supports53 === true) {
  90. $phpcsFile->addError($error, $open, $itemErrorCode, $itemData);
  91. }
  92. continue;
  93. }
  94. if ($tokens[$stackPtr]['code'] === \T_NEW
  95. && $tokens[$open]['code'] === \T_OPEN_CURLY_BRACKET
  96. ) {
  97. // Non-curlies was already handled above.
  98. $itemData = array('instantiation using curly braces', '5.6');
  99. $itemErrorCode = 'OnNewFoundUsingCurlies';
  100. }
  101. $phpcsFile->addError($error, $open, $itemErrorCode, $itemData);
  102. }
  103. }
  104. /**
  105. * Check if the class being instantiated/cloned is being dereferenced.
  106. *
  107. * @since 9.3.0 Logic split off from the process method.
  108. *
  109. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  110. * @param int $stackPtr The position of the current token in
  111. * the stack passed in $tokens.
  112. *
  113. * @return array Array containing the stack pointers to the object operator or
  114. * the open/close braces involved in the class member access;
  115. * or an empty array if no class member access was detected.
  116. */
  117. public function isClassMemberAccess(File $phpcsFile, $stackPtr)
  118. {
  119. $tokens = $phpcsFile->getTokens();
  120. if (isset($tokens[$stackPtr]['nested_parenthesis']) === false) {
  121. // The `new className/clone $a` has to be in parentheses, without is not supported.
  122. return array();
  123. }
  124. $parenthesisCloser = end($tokens[$stackPtr]['nested_parenthesis']);
  125. $parenthesisOpener = key($tokens[$stackPtr]['nested_parenthesis']);
  126. if (isset($tokens[$parenthesisOpener]['parenthesis_owner']) === true) {
  127. // If there is an owner, these parentheses are for a different purpose.
  128. return array();
  129. }
  130. $prevBeforeParenthesis = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($parenthesisOpener - 1), null, true);
  131. if ($prevBeforeParenthesis !== false && $tokens[$prevBeforeParenthesis]['code'] === \T_STRING) {
  132. // This is most likely a function call with the new/cloned object as a parameter.
  133. return array();
  134. }
  135. $braces = array();
  136. $end = $parenthesisCloser;
  137. do {
  138. $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($end + 1), null, true, null, true);
  139. if ($nextNonEmpty === false) {
  140. break;
  141. }
  142. if ($tokens[$nextNonEmpty]['code'] === \T_OBJECT_OPERATOR) {
  143. // No need to walk any further if this is object access.
  144. $braces[$nextNonEmpty] = true;
  145. break;
  146. }
  147. if ($tokens[$nextNonEmpty]['code'] === \T_OPEN_SQUARE_BRACKET
  148. || $tokens[$nextNonEmpty]['code'] === \T_OPEN_CURLY_BRACKET // PHP 7.0+.
  149. ) {
  150. if (isset($tokens[$nextNonEmpty]['bracket_closer']) === false) {
  151. // Live coding or parse error.
  152. break;
  153. }
  154. $braces[$nextNonEmpty] = $tokens[$nextNonEmpty]['bracket_closer'];
  155. // Continue, just in case there is nested array access, i.e. `(new Foo())[1][0];`.
  156. $end = $tokens[$nextNonEmpty]['bracket_closer'];
  157. continue;
  158. }
  159. // If we're still here, we've reached the end.
  160. break;
  161. } while (true);
  162. return $braces;
  163. }
  164. }