You are here

protected function DeprecationAnalyzer::findBinPath in Upgrade Status 8.3

Same name and namespace in other branches
  1. 8.2 src/DeprecationAnalyzer.php \Drupal\upgrade_status\DeprecationAnalyzer::findBinPath()

Finds bin-dir location.

This can be set in composer.json via `bin-dir` config and may not be inside the vendor directory. The logic somewhat duplicates DrupalFinder's vendor directory detection for best developer guidance in case of errors.

Return value

string Bin directory path if found.

Throws

\Exception

1 call to DeprecationAnalyzer::findBinPath()
DeprecationAnalyzer::initEnvironment in src/DeprecationAnalyzer.php
Initialize the external environment.

File

src/DeprecationAnalyzer.php, line 210

Class

DeprecationAnalyzer

Namespace

Drupal\upgrade_status

Code

protected function findBinPath() {
  $composer_name = trim(getenv('COMPOSER')) ?: 'composer.json';
  $composer_json_path = $this->finder
    ->getComposerRoot() . '/' . $composer_name;
  if ($composer_json_path && file_exists($composer_json_path)) {
    $json = json_decode(file_get_contents($composer_json_path), TRUE);
    if (is_null($json) || !is_array($json)) {
      throw new \Exception('Unable to decode composer information from ' . $composer_json_path . '.');
    }
  }
  else {
    throw new \Exception('The composer.json file was not found at ' . $composer_json_path . '.');
  }

  // If a bin-dir is specified, that is most specific.
  if (isset($json['config']['bin-dir'])) {
    $binPath = $this->finder
      ->getComposerRoot() . '/' . $json['config']['bin-dir'];
    if (file_exists($binPath . '/phpstan')) {
      return $binPath;
    }
    else {
      throw new \Exception('The PHPStan binary was not found in the bin-dir specified by ' . $composer_json_path . '. Attempted: ' . $binPath . '/phpstan.');
    }
  }

  // If a vendor-dir is specified, that is slightly less specific.
  if (isset($json['config']['vendor-dir'])) {
    $binPath = $this->finder
      ->getComposerRoot() . '/' . $json['config']['vendor-dir'] . '/bin';
    if (file_exists($binPath . '/phpstan')) {
      return $binPath;
    }
    else {
      throw new \Exception('The PHPStan binary was not found in the vendor-dir specified by ' . $composer_json_path . '. Attempted: ' . $binPath . '/phpstan.');
    }
  }

  // Try the assumed default vendor directory as a last resort.
  $binPath = $this->finder
    ->getComposerRoot() . '/vendor/bin';
  if (file_exists($binPath . '/phpstan')) {
    return $binPath;
  }
  throw new \Exception('The PHPStan binary was not found in the default vendor directory based on the location of ' . $composer_json_path . '. You may need to configure a vendor-dir in composer.json. See https://getcomposer.org/doc/06-config.md#vendor-dir. Attempted: ' . $binPath . '/phpstan.');
}