NonStaticMagicMethodsSniff.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 PHP_CodeSniffer_File as File;
  13. /**
  14. * Verifies the use of the correct visibility and static properties of magic methods.
  15. *
  16. * The requirements have always existed, but as of PHP 5.3, a warning will be thrown
  17. * when magic methods have the wrong modifiers.
  18. *
  19. * PHP version 5.3
  20. *
  21. * @link https://www.php.net/manual/en/language.oop5.magic.php
  22. *
  23. * @since 5.5
  24. * @since 5.6 Now extends the base `Sniff` class.
  25. */
  26. class NonStaticMagicMethodsSniff extends Sniff
  27. {
  28. /**
  29. * A list of PHP magic methods and their visibility and static requirements.
  30. *
  31. * Method names in the array should be all *lowercase*.
  32. * Visibility can be either 'public', 'protected' or 'private'.
  33. * Static can be either 'true' - *must* be static, or 'false' - *must* be non-static.
  34. * When a method does not have a specific requirement for either visibility or static,
  35. * do *not* add the key.
  36. *
  37. * @since 5.5
  38. * @since 5.6 The array format has changed to allow the sniff to also verify the
  39. * use of the correct visibility for a magic method.
  40. *
  41. * @var array(string)
  42. */
  43. protected $magicMethods = array(
  44. '__construct' => array(
  45. 'static' => false,
  46. ),
  47. '__destruct' => array(
  48. 'visibility' => 'public',
  49. 'static' => false,
  50. ),
  51. '__clone' => array(
  52. 'static' => false,
  53. ),
  54. '__get' => array(
  55. 'visibility' => 'public',
  56. 'static' => false,
  57. ),
  58. '__set' => array(
  59. 'visibility' => 'public',
  60. 'static' => false,
  61. ),
  62. '__isset' => array(
  63. 'visibility' => 'public',
  64. 'static' => false,
  65. ),
  66. '__unset' => array(
  67. 'visibility' => 'public',
  68. 'static' => false,
  69. ),
  70. '__call' => array(
  71. 'visibility' => 'public',
  72. 'static' => false,
  73. ),
  74. '__callstatic' => array(
  75. 'visibility' => 'public',
  76. 'static' => true,
  77. ),
  78. '__sleep' => array(
  79. 'visibility' => 'public',
  80. ),
  81. '__tostring' => array(
  82. 'visibility' => 'public',
  83. ),
  84. '__set_state' => array(
  85. 'visibility' => 'public',
  86. 'static' => true,
  87. ),
  88. '__debuginfo' => array(
  89. 'visibility' => 'public',
  90. 'static' => false,
  91. ),
  92. '__invoke' => array(
  93. 'visibility' => 'public',
  94. 'static' => false,
  95. ),
  96. '__serialize' => array(
  97. 'visibility' => 'public',
  98. 'static' => false,
  99. ),
  100. '__unserialize' => array(
  101. 'visibility' => 'public',
  102. 'static' => false,
  103. ),
  104. );
  105. /**
  106. * Returns an array of tokens this test wants to listen for.
  107. *
  108. * @since 5.5
  109. * @since 5.6 Now also checks traits.
  110. * @since 7.1.4 Now also checks anonymous classes.
  111. *
  112. * @return array
  113. */
  114. public function register()
  115. {
  116. $targets = array(
  117. \T_CLASS,
  118. \T_INTERFACE,
  119. \T_TRAIT,
  120. );
  121. if (\defined('T_ANON_CLASS')) {
  122. $targets[] = \T_ANON_CLASS;
  123. }
  124. return $targets;
  125. }
  126. /**
  127. * Processes this test, when one of its tokens is encountered.
  128. *
  129. * @since 5.5
  130. *
  131. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  132. * @param int $stackPtr The position of the current token in the
  133. * stack passed in $tokens.
  134. *
  135. * @return void
  136. */
  137. public function process(File $phpcsFile, $stackPtr)
  138. {
  139. // Should be removed, the requirement was previously also there, 5.3 just started throwing a warning about it.
  140. if ($this->supportsAbove('5.3') === false) {
  141. return;
  142. }
  143. $tokens = $phpcsFile->getTokens();
  144. if (isset($tokens[$stackPtr]['scope_closer']) === false) {
  145. return;
  146. }
  147. $classScopeCloser = $tokens[$stackPtr]['scope_closer'];
  148. $functionPtr = $stackPtr;
  149. // Find all the functions in this class or interface.
  150. while (($functionToken = $phpcsFile->findNext(\T_FUNCTION, $functionPtr, $classScopeCloser)) !== false) {
  151. /*
  152. * Get the scope closer for this function in order to know how
  153. * to advance to the next function.
  154. * If no body of function (e.g. for interfaces), there is
  155. * no closing curly brace; advance the pointer differently.
  156. */
  157. if (isset($tokens[$functionToken]['scope_closer'])) {
  158. $scopeCloser = $tokens[$functionToken]['scope_closer'];
  159. } else {
  160. $scopeCloser = ($functionToken + 1);
  161. }
  162. $methodName = $phpcsFile->getDeclarationName($functionToken);
  163. $methodNameLc = strtolower($methodName);
  164. if (isset($this->magicMethods[$methodNameLc]) === false) {
  165. $functionPtr = $scopeCloser;
  166. continue;
  167. }
  168. $methodProperties = $phpcsFile->getMethodProperties($functionToken);
  169. $errorCodeBase = $this->stringToErrorCode($methodNameLc);
  170. if (isset($this->magicMethods[$methodNameLc]['visibility']) && $this->magicMethods[$methodNameLc]['visibility'] !== $methodProperties['scope']) {
  171. $error = 'Visibility for magic method %s must be %s. Found: %s';
  172. $errorCode = $errorCodeBase . 'MethodVisibility';
  173. $data = array(
  174. $methodName,
  175. $this->magicMethods[$methodNameLc]['visibility'],
  176. $methodProperties['scope'],
  177. );
  178. $phpcsFile->addError($error, $functionToken, $errorCode, $data);
  179. }
  180. if (isset($this->magicMethods[$methodNameLc]['static']) && $this->magicMethods[$methodNameLc]['static'] !== $methodProperties['is_static']) {
  181. $error = 'Magic method %s cannot be defined as static.';
  182. $errorCode = $errorCodeBase . 'MethodStatic';
  183. $data = array($methodName);
  184. if ($this->magicMethods[$methodNameLc]['static'] === true) {
  185. $error = 'Magic method %s must be defined as static.';
  186. $errorCode = $errorCodeBase . 'MethodNonStatic';
  187. }
  188. $phpcsFile->addError($error, $functionToken, $errorCode, $data);
  189. }
  190. // Advance to next function.
  191. $functionPtr = $scopeCloser;
  192. }
  193. }
  194. }