RemovedMagicAutoloadSniff.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. /**
  14. * Detect declaration of the magic `__autoload()` method.
  15. *
  16. * This method has been deprecated in PHP 7.2 in favour of `spl_autoload_register()`.
  17. *
  18. * PHP version 7.2
  19. *
  20. * @link https://www.php.net/manual/en/migration72.deprecated.php#migration72.deprecated.__autoload-method
  21. * @link https://wiki.php.net/rfc/deprecations_php_7_2#autoload
  22. * @link https://www.php.net/manual/en/function.autoload.php
  23. *
  24. * @since 8.1.0
  25. * @since 9.0.0 Renamed from `DeprecatedMagicAutoloadSniff` to `RemovedMagicAutoloadSniff`.
  26. */
  27. class RemovedMagicAutoloadSniff extends Sniff
  28. {
  29. /**
  30. * Scopes to look for when testing using validDirectScope.
  31. *
  32. * @since 8.1.0
  33. *
  34. * @var array
  35. */
  36. private $checkForScopes = array(
  37. 'T_CLASS' => true,
  38. 'T_ANON_CLASS' => true,
  39. 'T_INTERFACE' => true,
  40. 'T_TRAIT' => true,
  41. 'T_NAMESPACE' => true,
  42. );
  43. /**
  44. * Returns an array of tokens this test wants to listen for.
  45. *
  46. * @since 8.1.0
  47. *
  48. * @return array
  49. */
  50. public function register()
  51. {
  52. return array(\T_FUNCTION);
  53. }
  54. /**
  55. * Processes this test, when one of its tokens is encountered.
  56. *
  57. * @since 8.1.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->supportsAbove('7.2') === false) {
  68. return;
  69. }
  70. $funcName = $phpcsFile->getDeclarationName($stackPtr);
  71. if (strtolower($funcName) !== '__autoload') {
  72. return;
  73. }
  74. if ($this->validDirectScope($phpcsFile, $stackPtr, $this->checkForScopes) !== false) {
  75. return;
  76. }
  77. if ($this->determineNamespace($phpcsFile, $stackPtr) !== '') {
  78. return;
  79. }
  80. $phpcsFile->addWarning('Use of __autoload() function is deprecated since PHP 7.2', $stackPtr, 'Found');
  81. }
  82. }