ForbiddenGetClassNullSniff.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\ParameterValues;
  11. use PHPCompatibility\AbstractFunctionCallParameterSniff;
  12. use PHP_CodeSniffer_File as File;
  13. /**
  14. * Detect: Passing `null` to `get_class()` is no longer allowed as of PHP 7.2.
  15. * This will now result in an `E_WARNING` being thrown.
  16. *
  17. * PHP version 7.2
  18. *
  19. * @link https://wiki.php.net/rfc/get_class_disallow_null_parameter
  20. * @link https://www.php.net/manual/en/function.get-class.php#refsect1-function.get-class-changelog
  21. *
  22. * @since 9.0.0
  23. */
  24. class ForbiddenGetClassNullSniff extends AbstractFunctionCallParameterSniff
  25. {
  26. /**
  27. * Functions to check for.
  28. *
  29. * @since 9.0.0
  30. *
  31. * @var array
  32. */
  33. protected $targetFunctions = array(
  34. 'get_class' => true,
  35. );
  36. /**
  37. * Do a version check to determine if this sniff needs to run at all.
  38. *
  39. * @since 9.0.0
  40. *
  41. * @return bool
  42. */
  43. protected function bowOutEarly()
  44. {
  45. return ($this->supportsAbove('7.2') === false);
  46. }
  47. /**
  48. * Process the parameters of a matched function.
  49. *
  50. * @since 9.0.0
  51. *
  52. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  53. * @param int $stackPtr The position of the current token in the stack.
  54. * @param string $functionName The token content (function name) which was matched.
  55. * @param array $parameters Array with information about the parameters.
  56. *
  57. * @return int|void Integer stack pointer to skip forward or void to continue
  58. * normal file processing.
  59. */
  60. public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters)
  61. {
  62. if (isset($parameters[1]) === false) {
  63. return;
  64. }
  65. if ($parameters[1]['raw'] !== 'null') {
  66. return;
  67. }
  68. $phpcsFile->addError(
  69. 'Passing "null" as the $object to get_class() is not allowed since PHP 7.2.',
  70. $parameters[1]['start'],
  71. 'Found'
  72. );
  73. }
  74. }