Verified Commit 70bbe3ba authored by Dave Long's avatar Dave Long
Browse files

ci: #3575080 [CI] Enhance PHPStan ErrorFormatter

By: mondrake
parent f59f2d15
Loading
Loading
Loading
Loading
Loading
+11 −10
Original line number Diff line number Diff line
# Configuration file for PHPStan static code checking, see https://phpstan.org .
# The PHPStan check is executed as a job in the CI pipeline.

includes:
  - .phpstan-baseline.php

services:
  errorFormatter.multiplex:
    class: Drupal\PHPStan\ErrorFormatter\MultiplexErrorFormatter
    arguments:
      basePath: ::getenv(_ARTIFACTS_DIR)
      outputs:
        gitlab: phpstan-gitlab.json
        junit: phpstan-junit.xml
        table: php://stdout

parameters:

  tmpDir: phpstan-tmp
@@ -74,3 +65,13 @@ rules:
  - PHPStan\Rules\Methods\MissingMethodReturnTypehintRule
  # The rule below can be removed when we increase level to 2.
  - PHPStan\Rules\Classes\AccessPrivateConstantThroughStaticRule

services:
  errorFormatter.multiplex:
    class: Drupal\PHPStan\ErrorFormatter\MultiplexErrorFormatter
    arguments:
      basePath: ::getenv(_ARTIFACTS_DIR)
      outputs:
        gitlab: phpstan-gitlab.json
        junit: phpstan-junit.xml
        table: null
+5 −19
Original line number Diff line number Diff line
@@ -16,10 +16,8 @@ final class FileOutput implements Output {

  /**
   * The file handle.
   *
   * @var resource
   */
  private $handle;
  private \SplFileObject $handle;

  /**
   * Constructs a FileOutput.
@@ -34,40 +32,28 @@ public function __construct(string $filePath, private OutputStyle $outputStyle)
    if ($directory && $directory !== 'php:' && !is_dir($directory)) {
      mkdir($directory, 0777, TRUE);
    }

    $handle = fopen($filePath, 'w');
    if ($handle === FALSE) {
      throw new \RuntimeException(sprintf('Unable to open file for writing: %s', $filePath));
    }
    $this->handle = $handle;
  }

  /**
   * Ensures the file is closed on destruction.
   */
  public function __destruct() {
    fclose($this->handle);
    $this->handle = new \SplFileObject($filePath, 'w');
  }

  /**
   * {@inheritdoc}
   */
  public function writeFormatted(string $message): void {
    fwrite($this->handle, $message);
    $this->handle->fwrite($message);
  }

  /**
   * {@inheritdoc}
   */
  public function writeLineFormatted(string $message): void {
    fwrite($this->handle, $message . "\n");
    $this->handle->fwrite($message . "\n");
  }

  /**
   * {@inheritdoc}
   */
  public function writeRaw(string $message): void {
    fwrite($this->handle, $message);
    $this->handle->fwrite($message);
  }

  /**
+11 −4
Original line number Diff line number Diff line
@@ -37,11 +37,18 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in
    $exitCode = 0;

    foreach ($this->outputs as $formatterName => $outputPath) {
      $formatter = $this->container->getService('errorFormatter.' . $formatterName);
      if ($outputPath !== NULL) {
        if ($this->basePath && !str_contains($outputPath, '/')) {
          $outputPath = $this->basePath . '/' . $outputPath;
        }
      $formatter = $this->container->getService('errorFormatter.' . $formatterName);
        $formatterOutput = new FileOutput($outputPath, $output->getStyle());
      }
      else {
        // When no output path is specified for the formatter, just use the
        // current output.
        $formatterOutput = $output;
      }
      $result = $formatter->formatErrors($analysisResult, $formatterOutput);
      $exitCode = max($result, $exitCode);
    }