requirements.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Requirements check for PHP_CodeSniffer.
  4. *
  5. * :WARNING:
  6. * This file MUST stay cross-version compatible with older PHP versions (min: PHP 5.3) to allow
  7. * for the requirements check to work correctly.
  8. *
  9. * The PHP 5.3 minimum is set as the previous PHPCS major (3.x) already had a PHP 5.4 minimum
  10. * requirement and didn't take parse errors caused due to the use of namespaces into account
  11. * in its requirements check, so running PHPCS 3.x on PHP < 5.3 would have failed with a parse
  12. * error already anyway, so PHP 5.3 seems reasonable to keep as the minimum for this.
  13. * :WARNING:
  14. *
  15. * @author Greg Sherwood <gsherwood@squiz.net>
  16. * @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
  17. * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  18. * @copyright 2024 PHPCSStandards and contributors
  19. * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  20. */
  21. namespace PHP_CodeSniffer;
  22. /**
  23. * Exits if the minimum requirements of PHP_CodeSniffer are not met.
  24. *
  25. * @return void
  26. */
  27. function checkRequirements()
  28. {
  29. // IMPORTANT: Must stay in sync with the value of the `PHP_CodeSniffer\Util\ExitCode::REQUIREMENTS_NOT_MET` constant!
  30. $exitCode = 64;
  31. // Check the PHP version.
  32. if (PHP_VERSION_ID < 70200) {
  33. $error = 'ERROR: PHP_CodeSniffer requires PHP version 7.2.0 or greater.'.PHP_EOL;
  34. fwrite(STDERR, $error);
  35. exit($exitCode);
  36. }
  37. $requiredExtensions = array(
  38. 'tokenizer',
  39. 'xmlwriter',
  40. 'SimpleXML',
  41. );
  42. $missingExtensions = array();
  43. foreach ($requiredExtensions as $extension) {
  44. if (extension_loaded($extension) === false) {
  45. $missingExtensions[] = $extension;
  46. }
  47. }
  48. if (empty($missingExtensions) === false) {
  49. $last = array_pop($requiredExtensions);
  50. $required = implode(', ', $requiredExtensions);
  51. $required .= ' and '.$last;
  52. if (count($missingExtensions) === 1) {
  53. $missing = $missingExtensions[0];
  54. } else {
  55. $last = array_pop($missingExtensions);
  56. $missing = implode(', ', $missingExtensions);
  57. $missing .= ' and '.$last;
  58. }
  59. $error = 'ERROR: PHP_CodeSniffer requires the %s extensions to be enabled. Please enable %s.'.PHP_EOL;
  60. fwrite(STDERR, sprintf($error, $required, $missing));
  61. exit($exitCode);
  62. }
  63. }//end checkRequirements()