From 54b8f56d70c69c9f7fb569e133908474895b6d24 Mon Sep 17 00:00:00 2001 From: "ityaozm@gmail.com" Date: Wed, 16 Oct 2024 17:23:07 +0800 Subject: [PATCH] perf(Generator.php): improve process handling - 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 --- app/Generators/Generator.php | 27 +++++++++++++++---------- tests/Unit/Generators/GeneratorTest.php | 6 +++++- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/app/Generators/Generator.php b/app/Generators/Generator.php index 7de0f8a..4eeb30a 100644 --- a/app/Generators/Generator.php +++ b/app/Generators/Generator.php @@ -40,6 +40,7 @@ abstract class Generator implements GeneratorContract /** * @psalm-suppress UndefinedMethod + * * @noinspection PhpUndefinedMethodInspection */ public function __construct(array $config) @@ -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, @@ -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, @@ -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("$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("$data"); + }; + } } diff --git a/tests/Unit/Generators/GeneratorTest.php b/tests/Unit/Generators/GeneratorTest.php index 3f90465..3d1f79b 100644 --- a/tests/Unit/Generators/GeneratorTest.php +++ b/tests/Unit/Generators/GeneratorTest.php @@ -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__);