<commit_before><?php
/**
 * Manages processes external to PHP, including interactive processes.
 *
 * @todo Test on Windows, someday.
 *
 * @copyright Copyright (c) 2011 Poluza.
 * @author <NAME> [au] <<EMAIL>>
 * @license http://www.opensource.org/licenses/mit-license.php
 *
 * @package Sutra
 * @link http://www.example.com/
 *
 * @version 1.0
 */
class sProcess {
  /**
   * The process that will be run.
   *
   * @var string
   */
  private $program;

  /**
   * Array of arguments for the process.
   *
   * @var array
   */
  private $arguments = array();

  /**
   * Paths for the system delimited by : (Linux and similar) or ; (Windows).
   *
   * @var string
   */
  private static $path = NULL;

  /**
   * Toss if the program returns an unexpected exit code.
   *
   * @var boolean
   */
  private $toss = FALSE;

  /**
   * Handle to popen().
   *
   * @var resource
   */
  private $popen_handle = NULL;

  /**
   * File to pipe output to when using mode 'w' with popen().
   * @var fFile
   */
  private $pipe_file;

  /**
   * Working directory. Defaults to current directory.
   *
   * @var fDirectory
   */
  private $work_dir = NULL;

  /**
   * Current directory before going into working directory.
   *
   * @var fDirectory
   */
  private $prior_dir = NULL;

  /**
   * Redirect standard error.
   *
   * @var boolean
   */
  private $redirect_standard_error = FALSE;

  /**
   * Where standard error gets sent to.
   *
   * @var string
   */
  private $stderr_target = '/dev/null';

  /**
   * Strips double and singular surrounding quotes out of all arguments.
   *
   * @return void
   */
  private function stripQuotesInArguments() {
    foreach ($this->arguments as $key => $arg) {
      $end = strlen($arg) - 1;
      if (($arg[0] === '"' && $arg[$end] === '"') || ($arg[0] === '\'' && $arg[$end] === '\'')) {
        $this->arguments[$key] = substr($arg, 1, $end - 1);
      }

      $this->arguments[$key] = trim($arg);
    }
  }

  /**
   * Parses the name and arguments from the arguments given to __construct().
   *
   * @param array $arguments Arguments passed.
   * @return void
   */
  private function parseNameAndArguments(array $arguments) {
    $count = count($arguments);

    // new sProcess(array('curl', 'a', 'b', 'c'))
    if (is_array($arguments[0]) &&  $count == 1) {
      $this->program = $arguments[0][0];
      $this->arguments = array_slice($arguments[0], 1);
    }
    // new sProcess('curl', array('a', 'b', 'c'))
    else if (is_string($arguments[0]) && $count == 2) {
      $this->program = $arguments[0];

      if (is_array($arguments[1])) {
        $this->arguments = $arguments[1];
      }
      else {
        // new sProcess('curl', 'a', 'b', 'c', 'd')
        $this->arguments = array_slice($arguments, 1);
      }
    }
    // new sProcess('curl a b c d')
    else {
      $args = explode(' ', $arguments[0]);
      $this->program = trim($args[0]);
      $this->arguments = array_slice($args, 1);
    }

    if (isset($this->arguments[0]) && is_array($this->arguments[0])) {
      $this->arguments = $this->arguments[0];
    }

    $this->stripQuotesInArguments();
  }

  /**
   * Constructor.
   *
   * On Windows, can include the .exe but this will be removed.
   *
   * You may also pass arguments to this instead of an array.
   *   Example: new sProcess('app', '--help').
   *
   * @throws fProgrammerException If the binary is invalid.
   *
   * @param array|string $name If string, the program to run, optionally with
   *   path and arguments. If array, each part of the command line separated.
   *   These will be implode()'d with spaces.
   * @return sProcess The object.
   */<commit_msg>Fix some other __construct() signatures<commit_after><?php
/**
 * Manages processes external to PHP, including interactive processes.
 *
 * @todo Test on Windows, someday.
 *
 * @copyright Copyright (c) 2011 Poluza.
 * @author <NAME> [au] <<EMAIL>>
 * @license http://www.opensource.org/licenses/mit-license.php
 *
 * @package Sutra
 * @link http://www.example.com/
 *
 * @version 1.0
 */
class sProcess {
  /**
   * The process that will be run.
   *
   * @var string
   */
  private $program;

  /**
   * Array of arguments for the process.
   *
   * @var array
   */
  private $arguments = array();

  /**
   * Paths for the system delimited by : (Linux and similar) or ; (Windows).
   *
   * @var string
   */
  private static $path = NULL;

  /**
   * Toss if the program returns an unexpected exit code.
   *
   * @var boolean
   */
  private $toss = FALSE;

  /**
   * Handle to popen().
   *
   * @var resource
   */
  private $popen_handle = NULL;

  /**
   * File to pipe output to when using mode 'w' with popen().
   * @var fFile
   */
  private $pipe_file;

  /**
   * Working directory. Defaults to current directory.
   *
   * @var fDirectory
   */
  private $work_dir = NULL;

  /**
   * Current directory before going into working directory.
   *
   * @var fDirectory
   */
  private $prior_dir = NULL;

  /**
   * Redirect standard error.
   *
   * @var boolean
   */
  private $redirect_standard_error = FALSE;

  /**
   * Where standard error gets sent to.
   *
   * @var string
   */
  private $stderr_target = '/dev/null';

  /**
   * Strips double and singular surrounding quotes out of all arguments.
   *
   * @return void
   */
  private function stripQuotesInArguments() {
    foreach ($this->arguments as $key => $arg) {
      $end = strlen($arg) - 1;
      if (($arg[0] === '"' && $arg[$end] === '"') || ($arg[0] === '\'' && $arg[$end] === '\'')) {
        $this->arguments[$key] = substr($arg, 1, $end - 1);
      }
    }
  }

  /**
   * Parses the name and arguments from the arguments given to __construct().
   *
   * @param array $arguments Arguments passed.
   * @return void
   */
  private function parseNameAndArguments(array $arguments) {
    $count = count($arguments);
    $is_string = is_string($arguments[0]);

    // new sProcess(array('curl', 'a', 'b', 'c'))
    if (is_array($arguments[0]) &&  $count == 1) {
      $this->program = trim($arguments[0][0]);
      $this->arguments = array_slice($arguments[0], 1);
    }
    // new sProcess('curl', array('a', 'b', 'c'))
    // new sProcess('curl', 'a')
    else if ($is_string && $count == 2) {
      $this->program = trim($arguments[0]);

      if (is_array($arguments[1])) {
        $this->arguments = $arguments[1];
      }
      else {
        $this->arguments = array((string)$arguments[1]);
      }
    }
    // new sProcess('curl a b c d')
    else if ($is_string && $count == 1) {
      $args = explode(' ', $arguments[0]);
      $this->program = trim($args[0]);
      $this->arguments = array_slice($args, 1);
    }
    // new sProcess('curl', 'a', 'b', 'c','d')
    else {
      $this->program = trim($arguments[0]);
      $this->arguments = array_slice($arguments, 1);
    }

    $this->stripQuotesInArguments();
  }

  /**
   * Constructor.
   *
   * On Windows, can include the .exe but this will be removed.
   *
   * You may also pass arguments to this instead of an array.
   *   Example: new sProcess('app', '--help').
   *
   * @throws fProgrammerException If the binary is invalid.
   *
   * @param array|string $name If string, the program to run, optionally with
   *   path and arguments. If array, each part of the command line separated.
   *   These will be implode()'d with spaces.
   * @return sProcess The object.
   */
{'fraction_non_alphanumeric': 0.1652267818574514, 'fraction_numerical': 0.00782937365010799, 'mean_word_length': 2.4964605946201037, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7016903', 'n_tokens_mistral': 2462, 'n_tokens_neox': 2380, 'n_words': 684}