php-cs-fixer 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env php
  2. <?php
  3. declare(strict_types=1);
  4. /*
  5. * This file is part of PHP CS Fixer.
  6. *
  7. * (c) Fabien Potencier <fabien@symfony.com>
  8. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  9. *
  10. * This source file is subject to the MIT license that is bundled
  11. * with this source code in the file LICENSE.
  12. */
  13. use Composer\XdebugHandler\XdebugHandler;
  14. use PhpCsFixer\Console\Application;
  15. error_reporting(E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED);
  16. set_error_handler(static function (int $severity, string $message, string $file, int $line): bool {
  17. if (0 !== ($severity & error_reporting())) {
  18. throw new ErrorException($message, 0, $severity, $file, $line);
  19. }
  20. return true;
  21. });
  22. // check environment requirements
  23. (static function (): void {
  24. if (\PHP_VERSION_ID === (int) '80000') { // TODO use 8_00_00 once only PHP 7.4+ is supported by this entry file
  25. fwrite(STDERR, "PHP CS Fixer is not able run on PHP 8.0.0 due to bug in PHP tokenizer (https://bugs.php.net/bug.php?id=80462).\n");
  26. fwrite(STDERR, "Update PHP version to unblock execution.\n");
  27. exit(1);
  28. }
  29. if (\PHP_VERSION_ID < (int) '70400' || \PHP_VERSION_ID >= (int) '80400') {
  30. fwrite(STDERR, "PHP needs to be a minimum version of PHP 7.4.0 and maximum version of PHP 8.3.*.\n");
  31. fwrite(STDERR, 'Current PHP version: '.PHP_VERSION.".\n");
  32. if (filter_var(getenv('PHP_CS_FIXER_IGNORE_ENV'), FILTER_VALIDATE_BOOLEAN)) {
  33. fwrite(STDERR, "Ignoring environment requirements because `PHP_CS_FIXER_IGNORE_ENV` is set. Execution may be unstable.\n");
  34. } else {
  35. fwrite(STDERR, "To ignore this requirement please set `PHP_CS_FIXER_IGNORE_ENV`.\n");
  36. fwrite(STDERR, "If you use PHP version higher than supported, you may experience code modified in a wrong way.\n");
  37. fwrite(STDERR, "Please report such cases at https://github.com/PHP-CS-Fixer/PHP-CS-Fixer .\n");
  38. exit(1);
  39. }
  40. }
  41. foreach (['json', 'tokenizer'] as $extension) {
  42. if (!extension_loaded($extension)) {
  43. fwrite(STDERR, sprintf("PHP extension ext-%s is missing from your system. Install or enable it.\n", $extension));
  44. if (filter_var(getenv('PHP_CS_FIXER_IGNORE_ENV'), FILTER_VALIDATE_BOOLEAN)) {
  45. fwrite(STDERR, "Ignoring environment requirements because `PHP_CS_FIXER_IGNORE_ENV` is set. Execution may be unstable.\n");
  46. } else {
  47. exit(1);
  48. }
  49. }
  50. }
  51. })();
  52. // load dependencies
  53. (static function (): void {
  54. $require = true;
  55. if (class_exists('Phar')) {
  56. // Maybe this file is used as phar-stub? Let's try!
  57. try {
  58. Phar::mapPhar('php-cs-fixer.phar');
  59. /** @phpstan-ignore requireOnce.fileNotFound */
  60. require_once 'phar://php-cs-fixer.phar/vendor/autoload.php';
  61. $require = false;
  62. } catch (PharException $e) {
  63. }
  64. }
  65. if ($require) {
  66. // OK, it's not, let give Composer autoloader a try!
  67. $possibleFiles = [__DIR__.'/../../autoload.php', __DIR__.'/../autoload.php', __DIR__.'/vendor/autoload.php'];
  68. $file = null;
  69. foreach ($possibleFiles as $possibleFile) {
  70. if (file_exists($possibleFile)) {
  71. $file = $possibleFile;
  72. break;
  73. }
  74. }
  75. if (null === $file) {
  76. throw new RuntimeException('Unable to locate autoload.php file.');
  77. }
  78. require_once $file;
  79. }
  80. })();
  81. // Restart if xdebug is loaded, unless the environment variable PHP_CS_FIXER_ALLOW_XDEBUG is set.
  82. $xdebug = new XdebugHandler('PHP_CS_FIXER');
  83. $xdebug->check();
  84. unset($xdebug);
  85. $application = new Application();
  86. $application->run();
  87. __HALT_COMPILER();