RemovedPHP4StyleConstructorsSniff.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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\FunctionNameRestrictions;
  11. use PHPCompatibility\Sniff;
  12. use PHP_CodeSniffer_File as File;
  13. use PHP_CodeSniffer_Tokens as Tokens;
  14. /**
  15. * Detect declarations of PHP 4 style constructors which are deprecated as of PHP 7.0.0.
  16. *
  17. * PHP 4 style constructors - methods that have the same name as the class they are defined in -
  18. * are deprecated as of PHP 7.0.0, and will be removed in the future.
  19. * PHP 7 will emit `E_DEPRECATED` if a PHP 4 constructor is the only constructor defined
  20. * within a class. Classes that implement a `__construct()` method are unaffected.
  21. *
  22. * Note: Methods with the same name as the class they are defined in _within a namespace_
  23. * are not recognized as constructors anyway and therefore outside the scope of this sniff.
  24. *
  25. * PHP version 7.0
  26. *
  27. * @link https://www.php.net/manual/en/migration70.deprecated.php#migration70.deprecated.php4-constructors
  28. * @link https://wiki.php.net/rfc/remove_php4_constructors
  29. * @link https://www.php.net/manual/en/language.oop5.decon.php
  30. *
  31. * @since 7.0.0
  32. * @since 7.0.8 This sniff now throws a warning instead of an error as the functionality is
  33. * only deprecated (for now).
  34. * @since 9.0.0 Renamed from `DeprecatedPHP4StyleConstructorsSniff` to `RemovedPHP4StyleConstructorsSniff`.
  35. */
  36. class RemovedPHP4StyleConstructorsSniff extends Sniff
  37. {
  38. /**
  39. * Returns an array of tokens this test wants to listen for.
  40. *
  41. * @since 7.0.0
  42. *
  43. * @return array
  44. */
  45. public function register()
  46. {
  47. return array(
  48. \T_CLASS,
  49. );
  50. }
  51. /**
  52. * Processes this test, when one of its tokens is encountered.
  53. *
  54. * @since 7.0.0
  55. * @since 7.0.8 The message is downgraded from error to warning as - for now - support
  56. * for PHP4-style constructors is just deprecated, not yet removed.
  57. *
  58. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  59. * @param int $stackPtr The position of the current token in the
  60. * stack passed in $tokens.
  61. *
  62. * @return void
  63. */
  64. public function process(File $phpcsFile, $stackPtr)
  65. {
  66. if ($this->supportsAbove('7.0') === false) {
  67. return;
  68. }
  69. if ($this->determineNamespace($phpcsFile, $stackPtr) !== '') {
  70. /*
  71. * Namespaced methods with the same name as the class are treated as
  72. * regular methods, so we can bow out if we're in a namespace.
  73. *
  74. * Note: the exception to this is PHP 5.3.0-5.3.2. This is currently
  75. * not dealt with.
  76. */
  77. return;
  78. }
  79. $tokens = $phpcsFile->getTokens();
  80. $class = $tokens[$stackPtr];
  81. if (isset($class['scope_closer']) === false) {
  82. return;
  83. }
  84. $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
  85. if ($nextNonEmpty === false || $tokens[$nextNonEmpty]['code'] !== \T_STRING) {
  86. // Anonymous class in combination with PHPCS 2.3.x.
  87. return;
  88. }
  89. $scopeCloser = $class['scope_closer'];
  90. $className = $tokens[$nextNonEmpty]['content'];
  91. if (empty($className) || \is_string($className) === false) {
  92. return;
  93. }
  94. $nextFunc = $stackPtr;
  95. $classNameLc = strtolower($className);
  96. $newConstructorFound = false;
  97. $oldConstructorFound = false;
  98. $oldConstructorPos = -1;
  99. while (($nextFunc = $phpcsFile->findNext(array(\T_FUNCTION, \T_DOC_COMMENT_OPEN_TAG), ($nextFunc + 1), $scopeCloser)) !== false) {
  100. // Skip over docblocks.
  101. if ($tokens[$nextFunc]['code'] === \T_DOC_COMMENT_OPEN_TAG) {
  102. $nextFunc = $tokens[$nextFunc]['comment_closer'];
  103. continue;
  104. }
  105. $functionScopeCloser = $nextFunc;
  106. if (isset($tokens[$nextFunc]['scope_closer'])) {
  107. // Normal (non-interface, non-abstract) method.
  108. $functionScopeCloser = $tokens[$nextFunc]['scope_closer'];
  109. }
  110. $funcName = $phpcsFile->getDeclarationName($nextFunc);
  111. if (empty($funcName) || \is_string($funcName) === false) {
  112. $nextFunc = $functionScopeCloser;
  113. continue;
  114. }
  115. $funcNameLc = strtolower($funcName);
  116. if ($funcNameLc === '__construct') {
  117. $newConstructorFound = true;
  118. }
  119. if ($funcNameLc === $classNameLc) {
  120. $oldConstructorFound = true;
  121. $oldConstructorPos = $nextFunc;
  122. }
  123. // If both have been found, no need to continue looping through the functions.
  124. if ($newConstructorFound === true && $oldConstructorFound === true) {
  125. break;
  126. }
  127. $nextFunc = $functionScopeCloser;
  128. }
  129. if ($newConstructorFound === false && $oldConstructorFound === true) {
  130. $phpcsFile->addWarning(
  131. 'Use of deprecated PHP4 style class constructor is not supported since PHP 7.',
  132. $oldConstructorPos,
  133. 'Found'
  134. );
  135. }
  136. }
  137. }