RemovedOrphanedParentSniff.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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\Classes;
  11. use PHPCompatibility\Sniff;
  12. use PHP_CodeSniffer_File as File;
  13. /**
  14. * Using `parent` inside a class without parent is deprecated since PHP 7.4.
  15. *
  16. * This will throw a compile-time error in the future. Currently an error will only
  17. * be generated if/when the parent is accessed at run-time.
  18. *
  19. * PHP version 7.4
  20. *
  21. * @link https://www.php.net/manual/en/migration74.deprecated.php#migration74.deprecated.core.parent
  22. *
  23. * @since 9.2.0
  24. */
  25. class RemovedOrphanedParentSniff extends Sniff
  26. {
  27. /**
  28. * Class scopes to check the class declaration.
  29. *
  30. * @since 9.2.0
  31. *
  32. * @var array
  33. */
  34. public $classScopeTokens = array(
  35. 'T_CLASS' => true,
  36. 'T_ANON_CLASS' => true,
  37. );
  38. /**
  39. * Returns an array of tokens this test wants to listen for.
  40. *
  41. * @since 9.2.0
  42. *
  43. * @return array
  44. */
  45. public function register()
  46. {
  47. return array(\T_PARENT);
  48. }
  49. /**
  50. * Processes this test, when one of its tokens is encountered.
  51. *
  52. * @since 9.2.0
  53. *
  54. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  55. * @param int $stackPtr The position of the current token
  56. * in the stack passed in $tokens.
  57. *
  58. * @return void
  59. */
  60. public function process(File $phpcsFile, $stackPtr)
  61. {
  62. if ($this->supportsAbove('7.4') === false) {
  63. return;
  64. }
  65. $tokens = $phpcsFile->getTokens();
  66. if (empty($tokens[$stackPtr]['conditions']) === true) {
  67. // Use within the global namespace. Not our concern.
  68. return;
  69. }
  70. /*
  71. * Find the class within which this parent keyword is used.
  72. */
  73. $conditions = $tokens[$stackPtr]['conditions'];
  74. $conditions = array_reverse($conditions, true);
  75. $classPtr = false;
  76. foreach ($conditions as $ptr => $type) {
  77. if (isset($this->classScopeTokens[$tokens[$ptr]['type']])) {
  78. $classPtr = $ptr;
  79. break;
  80. }
  81. }
  82. if ($classPtr === false) {
  83. // Use outside of a class scope. Not our concern.
  84. return;
  85. }
  86. if (isset($tokens[$classPtr]['scope_opener']) === false) {
  87. // No scope opener known. Probably a parse error.
  88. return;
  89. }
  90. $extends = $phpcsFile->findNext(\T_EXTENDS, ($classPtr + 1), $tokens[$classPtr]['scope_opener']);
  91. if ($extends !== false) {
  92. // Class has a parent.
  93. return;
  94. }
  95. $phpcsFile->addError(
  96. 'Using "parent" inside a class without parent is deprecated since PHP 7.4',
  97. $stackPtr,
  98. 'Deprecated'
  99. );
  100. }
  101. }