ProcessLinter.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\Linter;
  13. use PhpCsFixer\FileReader;
  14. use PhpCsFixer\FileRemoval;
  15. use Symfony\Component\Filesystem\Exception\IOException;
  16. use Symfony\Component\Process\PhpExecutableFinder;
  17. use Symfony\Component\Process\Process;
  18. /**
  19. * Handle PHP code linting using separated process of `php -l _file_`.
  20. *
  21. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  22. *
  23. * @internal
  24. */
  25. final class ProcessLinter implements LinterInterface
  26. {
  27. private FileRemoval $fileRemoval;
  28. private ProcessLinterProcessBuilder $processBuilder;
  29. /**
  30. * Temporary file for code linting.
  31. *
  32. * @var null|string
  33. */
  34. private $temporaryFile;
  35. /**
  36. * @param null|string $executable PHP executable, null for autodetection
  37. */
  38. public function __construct(?string $executable = null)
  39. {
  40. if (null === $executable) {
  41. $executableFinder = new PhpExecutableFinder();
  42. $executable = $executableFinder->find(false);
  43. if (false === $executable) {
  44. throw new UnavailableLinterException('Cannot find PHP executable.');
  45. }
  46. if ('phpdbg' === \PHP_SAPI) {
  47. if (!str_contains($executable, 'phpdbg')) {
  48. throw new UnavailableLinterException('Automatically found PHP executable is non-standard phpdbg. Could not find proper PHP executable.');
  49. }
  50. // automatically found executable is `phpdbg`, let us try to fallback to regular `php`
  51. $executable = str_replace('phpdbg', 'php', $executable);
  52. if (!is_executable($executable)) {
  53. throw new UnavailableLinterException('Automatically found PHP executable is phpdbg. Could not find proper PHP executable.');
  54. }
  55. }
  56. }
  57. $this->processBuilder = new ProcessLinterProcessBuilder($executable);
  58. $this->fileRemoval = new FileRemoval();
  59. }
  60. public function __destruct()
  61. {
  62. if (null !== $this->temporaryFile) {
  63. $this->fileRemoval->delete($this->temporaryFile);
  64. }
  65. }
  66. /**
  67. * This class is not intended to be serialized,
  68. * and cannot be deserialized (see __wakeup method).
  69. */
  70. public function __sleep(): array
  71. {
  72. throw new \BadMethodCallException('Cannot serialize '.self::class);
  73. }
  74. /**
  75. * Disable the deserialization of the class to prevent attacker executing
  76. * code by leveraging the __destruct method.
  77. *
  78. * @see https://owasp.org/www-community/vulnerabilities/PHP_Object_Injection
  79. */
  80. public function __wakeup(): void
  81. {
  82. throw new \BadMethodCallException('Cannot unserialize '.self::class);
  83. }
  84. public function isAsync(): bool
  85. {
  86. return true;
  87. }
  88. public function lintFile(string $path): LintingResultInterface
  89. {
  90. return new ProcessLintingResult($this->createProcessForFile($path), $path);
  91. }
  92. public function lintSource(string $source): LintingResultInterface
  93. {
  94. return new ProcessLintingResult($this->createProcessForSource($source), $this->temporaryFile);
  95. }
  96. /**
  97. * @param string $path path to file
  98. */
  99. private function createProcessForFile(string $path): Process
  100. {
  101. // in case php://stdin
  102. if (!is_file($path)) {
  103. return $this->createProcessForSource(FileReader::createSingleton()->read($path));
  104. }
  105. $process = $this->processBuilder->build($path);
  106. $process->setTimeout(10);
  107. $process->start();
  108. return $process;
  109. }
  110. /**
  111. * Create process that lint PHP code.
  112. *
  113. * @param string $source code
  114. */
  115. private function createProcessForSource(string $source): Process
  116. {
  117. if (null === $this->temporaryFile) {
  118. $this->temporaryFile = tempnam(sys_get_temp_dir(), 'cs_fixer_tmp_');
  119. $this->fileRemoval->observe($this->temporaryFile);
  120. }
  121. if (false === @file_put_contents($this->temporaryFile, $source)) {
  122. throw new IOException(sprintf('Failed to write file "%s".', $this->temporaryFile), 0, null, $this->temporaryFile);
  123. }
  124. return $this->createProcessForFile($this->temporaryFile);
  125. }
  126. }