<commit_before><?php

/**
 * This linter invokes checkstyle for verifying code style standards.
 */
class UberCheckstyleLinter extends ArcanistFutureLinter {

  private $checkstyleScript = null;
  private $checkstyleJar = null;
  private $checkstyleConfig = null;
  private $useScript = false;
  private $maxFiles = 100;

  public function getInfoName() {
    return 'Java checkstyle linter';
  }

  public function getLinterName() {
    return 'CHECKSTYLE';
  }

  public function getInfoURI() {
    return 'http://checkstyle.sourceforge.net';
  }

  public function getInfoDescription() {
    return 'Use checkstyle to perform static analysis on Java code';
  }

  public function getLinterConfigurationName() {
    return 'checkstyle';
  }

  public function getLinterConfigurationOptions() {
    $options = array(
      'checkstyle.script' => array(
        'type' => 'optional string',
        'help' => pht('Checkstyle script to execute. Script must output checkstyle in XML to $stdout'),
      ),
      'checkstyle.jar' => array(
        'type' => 'optional string',
        'help' => pht('Checkstyle jar [from https://sourceforge.net/projects/checkstyle/files/checkstyle/]'),
      ),
      'checkstyle.config' => array(
        'type' => 'optional string',
        'help' => pht('Checkstyle configuration file'),
      ),
      'checkstyle.maxfiles' => array(
        'type' => 'optional int',
        'help' => pht('The maximum number of files to check per call of checkstyle. If there are more files, they will be split up into multiple checkstyle invocations.'),
      ),
    );
    return $options + parent::getLinterConfigurationOptions();
  }

  public function setLinterConfigurationValue($key, $value) {
    switch ($key) {
      case 'checkstyle.script':
        $this->checkstyleScript = $value;
        return;
      case 'checkstyle.jar':
        $this->checkstyleJar = $value;
        return;
      case 'checkstyle.config':
        $this->checkstyleConfig = $value;
        return;
      case 'checkstyle.maxfiles':
        $this->maxFiles = $value;
        return;
    }
    return parent::setLinterConfigurationValue($key, $value);
  }

  /**
   * Check that the checkstyle libraries are installed.
   * @return void
   */
  private function checkConfiguration() {
    if ($this->checkstyleJar != null) {
      $this->checkJarConfiguration();
    } else if ($this->checkstyleScript != null) {
      $this->checkScriptConfiguration();
      $this->useScript = true;
    } else {
      throw new ArcanistMissingLinterException(
        pht('Missing config.  Either \'checkstyle.script\' or \'checkstyle.jar\' need to be set. ')
      );
    }
  }

  private function checkScriptConfiguration() {
    // Some scripts have command line arguments
    $script = explode(' ', $this->checkstyleScript)[0];
    $absScriptPath = Filesystem::resolvePath($script, $this->getProjectRoot());
    if (!Filesystem::pathExists($absScriptPath)) {
      throw new ArcanistMissingLinterException(
        pht(
          'Unable to locate script "%s" to run linter %s. You may need ' .
          'to install the script, or adjust your linter configuration.',
          $absScriptPath,
          get_class($this)));
    }
  }

  private function checkJarConfiguration() {
    if (!Filesystem::binaryExists("java")) {
      throw new ArcanistMissingLinterException(
        pht('Java is not installed', get_class($this)));
    }

    $absJarPath = Filesystem::resolvePath($this->checkstyleJar, $this->getProjectRoot());
    if (!Filesystem::pathExists($absJarPath)) {
      throw new ArcanistMissingLinterException(
        sprintf(
          "%s\n%s",
          pht(
            'Unable to locate jar "%s" to run linter %s. You may need ' .
            'to install the script, or adjust your linter configuration.',
            $absJarPath,
            get_class($this)),
          pht('
                TO INSTALL: 
                1) Download checkstyle jar from https://sourceforge.net/projects/checkstyle/files/checkstyle/
                2) Set `checkstyle.jar` in `.arclint` to the location of the jar.'
          )));
    }
  }

  private function getCommand() {
    if ($this->useScript === true) {
      return $this->checkstyleScript;
    }  else {
      $command = sprintf('java -jar %s -f xml -c %s ',
        $this->checkstyleJar,
        $this->checkstyleConfig);
      return $command;
    }
  }

  final protected function buildFutures(array $paths) {
    $this->checkConfiguration();
    $futures = array();
    // Call checkstyle in batches
    $chunks = array_chunk($paths, this->$maxFiles);

    $command = $this->getCommand();
    foreach ($chunks as $chunk) {
      $future = new ExecFuture(sprintf("%s %s", $command, implode(" ", $chunk)));
      $future->setCWD($this->getProjectRoot());

      foreach ($chunk as $path) {
        $futures[$path] = $future;
      }
    }

    return $futures;
  }

  final protected function resolveFuture($path, Future $future) {
    list($err, $stdout, $stderr) = $future->resolve();<commit_msg>Undo maxfiles change as there is a bug currently<commit_after><?php

/**
 * This linter invokes checkstyle for verifying code style standards.
 */
class UberCheckstyleLinter extends ArcanistFutureLinter {

  private $checkstyleScript = null;
  private $checkstyleJar = null;
  private $checkstyleConfig = null;
  private $useScript = false;
  private $maxFiles = 100;

  public function getInfoName() {
    return 'Java checkstyle linter';
  }

  public function getLinterName() {
    return 'CHECKSTYLE';
  }

  public function getInfoURI() {
    return 'http://checkstyle.sourceforge.net';
  }

  public function getInfoDescription() {
    return 'Use checkstyle to perform static analysis on Java code';
  }

  public function getLinterConfigurationName() {
    return 'checkstyle';
  }

  public function getLinterConfigurationOptions() {
    $options = array(
      'checkstyle.script' => array(
        'type' => 'optional string',
        'help' => pht('Checkstyle script to execute. Script must output checkstyle in XML to $stdout'),
      ),
      'checkstyle.jar' => array(
        'type' => 'optional string',
        'help' => pht('Checkstyle jar [from https://sourceforge.net/projects/checkstyle/files/checkstyle/]'),
      ),
      'checkstyle.config' => array(
        'type' => 'optional string',
        'help' => pht('Checkstyle configuration file'),
      ),
      'checkstyle.maxfiles' => array(
        'type' => 'optional int',
        'help' => pht('The maximum number of files to check per call of checkstyle. If there are more files, they will be split up into multiple checkstyle invocations.'),
      ),
    );
    return $options + parent::getLinterConfigurationOptions();
  }

  public function setLinterConfigurationValue($key, $value) {
    switch ($key) {
      case 'checkstyle.script':
        $this->checkstyleScript = $value;
        return;
      case 'checkstyle.jar':
        $this->checkstyleJar = $value;
        return;
      case 'checkstyle.config':
        $this->checkstyleConfig = $value;
        return;
      case 'checkstyle.maxfiles':
        $this->maxFiles = $value;
        return;
    }
    return parent::setLinterConfigurationValue($key, $value);
  }

  /**
   * Check that the checkstyle libraries are installed.
   * @return void
   */
  private function checkConfiguration() {
    if ($this->checkstyleJar != null) {
      $this->checkJarConfiguration();
    } else if ($this->checkstyleScript != null) {
      $this->checkScriptConfiguration();
      $this->useScript = true;
    } else {
      throw new ArcanistMissingLinterException(
        pht('Missing config.  Either \'checkstyle.script\' or \'checkstyle.jar\' need to be set. ')
      );
    }
  }

  private function checkScriptConfiguration() {
    // Some scripts have command line arguments
    $script = explode(' ', $this->checkstyleScript)[0];
    $absScriptPath = Filesystem::resolvePath($script, $this->getProjectRoot());
    if (!Filesystem::pathExists($absScriptPath)) {
      throw new ArcanistMissingLinterException(
        pht(
          'Unable to locate script "%s" to run linter %s. You may need ' .
          'to install the script, or adjust your linter configuration.',
          $absScriptPath,
          get_class($this)));
    }
  }

  private function checkJarConfiguration() {
    if (!Filesystem::binaryExists("java")) {
      throw new ArcanistMissingLinterException(
        pht('Java is not installed', get_class($this)));
    }

    $absJarPath = Filesystem::resolvePath($this->checkstyleJar, $this->getProjectRoot());
    if (!Filesystem::pathExists($absJarPath)) {
      throw new ArcanistMissingLinterException(
        sprintf(
          "%s\n%s",
          pht(
            'Unable to locate jar "%s" to run linter %s. You may need ' .
            'to install the script, or adjust your linter configuration.',
            $absJarPath,
            get_class($this)),
          pht('
                TO INSTALL: 
                1) Download checkstyle jar from https://sourceforge.net/projects/checkstyle/files/checkstyle/
                2) Set `checkstyle.jar` in `.arclint` to the location of the jar.'
          )));
    }
  }

  private function getCommand() {
    if ($this->useScript === true) {
      return $this->checkstyleScript;
    }  else {
      $command = sprintf('java -jar %s -f xml -c %s ',
        $this->checkstyleJar,
        $this->checkstyleConfig);
      return $command;
    }
  }

  final protected function buildFutures(array $paths) {
    $this->checkConfiguration();
    $futures = array();
    // Call checkstyle in batches
    $chunks = array_chunk($paths, 100);

    $command = $this->getCommand();
    foreach ($chunks as $chunk) {
      $future = new ExecFuture(sprintf("%s %s", $command, implode(" ", $chunk)));
      $future->setCWD($this->getProjectRoot());

      foreach ($chunk as $path) {
        $futures[$path] = $future;
      }
    }

    return $futures;
  }

  final protected function resolveFuture($path, Future $future) {
    list($err, $stdout, $stderr) = $future->resolve();
{'fraction_non_alphanumeric': 0.12831640201203273, 'fraction_numerical': 0.001479435841798994, 'mean_word_length': 2.763919821826281, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 72, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6332253', 'n_tokens_mistral': 2938, 'n_tokens_neox': 2876, 'n_words': 777}