Skip to content

Commit

Permalink
Use getApplication() instead of passing the instance around (#93)
Browse files Browse the repository at this point in the history
By passing it like so as a parameter it gives the impression that the instance itself may change (different references). This is however not the case and since the application state does mutate across the execution I do not think this helps out in the understanding of the code.
theofidry authored Jul 7, 2022

Verified

This commit was signed with the committer’s verified signature. The key has expired.
domdfcoding Dominic Davis-Foster
1 parent 2b787fe commit 8d63dae
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/BinCommand.php
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@
namespace Bamarni\Composer\Bin;

use Composer\Command\BaseCommand;
use Composer\Console\Application as ComposerApplication;
use Composer\Factory;
use Composer\IO\IOInterface;
use Symfony\Component\Console\Input\InputArgument;
@@ -44,12 +43,11 @@ protected function configure(): void
public function execute(InputInterface $input, OutputInterface $output): int
{
$config = Config::fromComposer($this->requireComposer());
$application = $this->getApplication();
$currentWorkingDir = getcwd();

// Ensures Composer is reset – we are setting some environment variables
// & co. so a fresh Composer instance is required.
$this->resetComposers($application);
$this->resetComposers();

if ($config->binLinksAreEnabled()) {
$binDir = ConfigFactory::createConfig()->get('bin-dir');
@@ -86,13 +84,11 @@ public function execute(InputInterface $input, OutputInterface $output): int

return (self::ALL_NAMESPACES !== $namespace)
? $this->executeInNamespace(
$application,
$vendorRoot.'/'.$namespace,
$binInput,
$output
)
: $this->executeAllNamespaces(
$application,
$vendorRoot,
$binInput,
$output
@@ -108,7 +104,6 @@ private static function getBinNamespaces(string $binVendorRoot): array
}

private function executeAllNamespaces(
ComposerApplication $application,
string $binVendorRoot,
InputInterface $input,
OutputInterface $output
@@ -130,18 +125,17 @@ private function executeAllNamespaces(
$exitCode = self::SUCCESS;

foreach ($namespaces as $namespace) {
$exitCode += $this->executeInNamespace($application, $namespace, $input, $output);
$exitCode += $this->executeInNamespace($namespace, $input, $output);

// Ensure we have a clean state in-between each namespace execution
$this->chdir($originalWorkingDir);
$this->resetComposers($application);
$this->resetComposers();
}

return min($exitCode, self::FAILURE);
}

private function executeInNamespace(
ComposerApplication $application,
string $namespace,
InputInterface $input,
OutputInterface $output
@@ -193,17 +187,17 @@ private function executeInNamespace(
)
);

return $application->doRun($namespaceInput, $output);
return $this->getApplication()->doRun($namespaceInput, $output);
}

public function isProxyCommand(): bool
{
return true;
}

private function resetComposers(ComposerApplication $application): void
private function resetComposers(): void
{
$application->resetComposer();
$this->getApplication()->resetComposer();

foreach ($this->getApplication()->all() as $command) {
if ($command instanceof BaseCommand) {

0 comments on commit 8d63dae

Please sign in to comment.