Skip to content

Commit

Permalink
perf(Generator.php): improve process handling
Browse files Browse the repository at this point in the history
- Add a missing newline in the `__construct` method
- Update the `mustRunProcess` and `runProcess` methods to include type declarations
- Update the `getHelper`   method to include a return type declaration
- Modify the `defaultRunningCallback` method to be protected
  • Loading branch information
[email protected] committed Oct 16, 2024
1 parent f07659c commit 54b8f56
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
27 changes: 16 additions & 11 deletions app/Generators/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ abstract class Generator implements GeneratorContract

/**
* @psalm-suppress UndefinedMethod
*
* @noinspection PhpUndefinedMethodInspection
*/
public function __construct(array $config)
Expand All @@ -53,9 +54,10 @@ public function __construct(array $config)

/**
* @param array|string|\Symfony\Component\Process\Process $cmd
*
* @noinspection MissingParameterTypeDeclarationInspection
*/
public function mustRunProcess(
protected function mustRunProcess(
$cmd,
?string $error = null,
?callable $callback = null,
Expand All @@ -72,10 +74,13 @@ public function mustRunProcess(

/**
* @param array|string|\Symfony\Component\Process\Process $cmd
* @noinspection MissingParameterTypeDeclarationInspection
*
* @psalm-suppress UndefinedInterfaceMethod
*
* @noinspection MissingParameterTypeDeclarationInspection
* @noinspection PhpPossiblePolymorphicInvocationInspection
*/
public function runProcess(
protected function runProcess(
$cmd,
?string $error = null,
?callable $callback = null,
Expand All @@ -89,18 +94,18 @@ public function runProcess(
return $this->getHelper('process')->run($output ?? $this->output, $cmd, $error, $callback, $verbosity);
}

public function defaultRunningCallback(): callable
{
return function (string $type, string $data): void {
Process::OUT === $type ? $this->output->write($data) : $this->output->write("<fg=red>$data</>");
};
}

/**
* @throws InvalidArgumentException if the helper is not defined
*/
public function getHelper(string $name): HelperInterface
protected function getHelper(string $name): HelperInterface
{
return $this->helperSet->get($name);
}

protected function defaultRunningCallback(): callable
{
return function (string $type, string $data): void {
Process::OUT === $type ? $this->output->write($data) : $this->output->write("<fg=red>$data</>");
};
}
}
6 changes: 5 additions & 1 deletion tests/Unit/Generators/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,9 @@
});

it('can run string cmd', function (): void {
expect($this->generator->runProcess('echo foo'))->isSuccessful()->toBeTrue();
expect(
(function () {
return $this->runProcess('echo foo');
})->call($this->generator)
)->isSuccessful()->toBeTrue();
})->group(__DIR__, __FILE__);

0 comments on commit 54b8f56

Please sign in to comment.