ExecutorWithoutErrorHandler.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer;
  13. /**
  14. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  15. *
  16. * @internal
  17. */
  18. final class ExecutorWithoutErrorHandler
  19. {
  20. private function __construct() {}
  21. /**
  22. * @template T
  23. *
  24. * @param callable(): T $callback
  25. *
  26. * @return T
  27. *
  28. * @throws ExecutorWithoutErrorHandlerException
  29. */
  30. public static function execute(callable $callback)
  31. {
  32. /** @var ?string */
  33. $error = null;
  34. set_error_handler(static function (int $errorNumber, string $errorString, string $errorFile, int $errorLine) use (&$error): bool {
  35. $error = $errorString;
  36. return true;
  37. });
  38. try {
  39. $result = $callback();
  40. } finally {
  41. restore_error_handler();
  42. }
  43. if (null !== $error) {
  44. throw new ExecutorWithoutErrorHandlerException($error);
  45. }
  46. return $result;
  47. }
  48. }