Skip to content

Commit

Permalink
Merge branch 'hotfix/1.2.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
TravisCarden committed Feb 16, 2023
2 parents 510d3b1 + 65b5a81 commit b13cb4d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 51 deletions.
35 changes: 1 addition & 34 deletions src/Infrastructure/Service/Finder/ExecutableFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

final class ExecutableFinder implements ExecutableFinderInterface
{
/** @var array<\PhpTuf\ComposerStager\Domain\Exception\LogicException|string|null> */
private array $caches = [];

private SymfonyExecutableFinder $symfonyExecutableFinder;

public function __construct(SymfonyExecutableFinder $symfonyExecutableFinder)
Expand All @@ -19,47 +16,17 @@ public function __construct(SymfonyExecutableFinder $symfonyExecutableFinder)

public function find(string $name): string
{
$cache = $this->getCache($name);

// Throw cached exception.
if ($cache instanceof LogicException) {
throw $cache;
}

// Return cached path.
if ($cache !== null) {
return $cache;
}

// Look for executable.
$this->symfonyExecutableFinder->addSuffix('.phar');
$path = $this->symfonyExecutableFinder->find($name);

// Cache and throw exception if not found.
if ($path === null) {
$cache = new LogicException(
throw new LogicException(
sprintf('The "%s" executable cannot be found. Make sure it\'s installed and in the $PATH.', $name),
);
$this->setCache($name, $cache);

throw $cache;
}

// Cache and return path if found.
$this->setCache($name, $path);

return $path;
}

/** @return \PhpTuf\ComposerStager\Domain\Exception\LogicException|string|null */
private function getCache(string $commandName)
{
return $this->caches[$commandName] ?? null;
}

/** @param string|\PhpTuf\ComposerStager\Domain\Exception\LogicException $value */
private function setCache(string $commandName, $value): void
{
$this->caches[$commandName] = $value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
*
* @covers ::__construct
* @covers ::find
* @covers ::getCache
* @covers ::setCache
*
* @property \Symfony\Component\Process\ExecutableFinder|\Prophecy\Prophecy\ObjectProphecy $symfonyExecutableFinder
*/
Expand Down Expand Up @@ -51,8 +49,6 @@ public function testFind(string $firstCommandName, string $firstPath, string $se
$sut = $this->createSut();

$firstExpected = $sut->find($firstCommandName);
// Call again to test result caching.
$sut->find($firstCommandName);
// Find something else to test cache isolation.
$secondPath = $sut->find($secondCommandName);

Expand Down Expand Up @@ -106,26 +102,17 @@ public function providerFindNotFound(): array
/** Make sure ::find caches result when Composer is not found. */
public function testFindNotFoundCaching(): void
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('The "composer" executable cannot be found. Make sure it\'s installed and in the $PATH.');

$this->symfonyExecutableFinder
->addSuffix('.phar')
->shouldBeCalledOnce()
->willReturn(null);
$this->symfonyExecutableFinder
->find('composer')
->shouldBeCalledOnce()
->willReturn(null);
$sut = $this->createSut();

try {
$sut->find('composer');
} catch (LogicException $e) {
// @ignoreException
}

try {
$sut->find('composer');
} catch (LogicException $e) {
// @ignoreException
}
$sut->find('composer');
}
}

0 comments on commit b13cb4d

Please sign in to comment.