Skip to content

Commit 54b8f56

Browse files
author
ityaozm@gmail.com
committed
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
1 parent f07659c commit 54b8f56

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

app/Generators/Generator.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ abstract class Generator implements GeneratorContract
4040

4141
/**
4242
* @psalm-suppress UndefinedMethod
43+
*
4344
* @noinspection PhpUndefinedMethodInspection
4445
*/
4546
public function __construct(array $config)
@@ -53,9 +54,10 @@ public function __construct(array $config)
5354

5455
/**
5556
* @param array|string|\Symfony\Component\Process\Process $cmd
57+
*
5658
* @noinspection MissingParameterTypeDeclarationInspection
5759
*/
58-
public function mustRunProcess(
60+
protected function mustRunProcess(
5961
$cmd,
6062
?string $error = null,
6163
?callable $callback = null,
@@ -72,10 +74,13 @@ public function mustRunProcess(
7274

7375
/**
7476
* @param array|string|\Symfony\Component\Process\Process $cmd
75-
* @noinspection MissingParameterTypeDeclarationInspection
77+
*
7678
* @psalm-suppress UndefinedInterfaceMethod
79+
*
80+
* @noinspection MissingParameterTypeDeclarationInspection
81+
* @noinspection PhpPossiblePolymorphicInvocationInspection
7782
*/
78-
public function runProcess(
83+
protected function runProcess(
7984
$cmd,
8085
?string $error = null,
8186
?callable $callback = null,
@@ -89,18 +94,18 @@ public function runProcess(
8994
return $this->getHelper('process')->run($output ?? $this->output, $cmd, $error, $callback, $verbosity);
9095
}
9196

92-
public function defaultRunningCallback(): callable
93-
{
94-
return function (string $type, string $data): void {
95-
Process::OUT === $type ? $this->output->write($data) : $this->output->write("<fg=red>$data</>");
96-
};
97-
}
98-
9997
/**
10098
* @throws InvalidArgumentException if the helper is not defined
10199
*/
102-
public function getHelper(string $name): HelperInterface
100+
protected function getHelper(string $name): HelperInterface
103101
{
104102
return $this->helperSet->get($name);
105103
}
104+
105+
protected function defaultRunningCallback(): callable
106+
{
107+
return function (string $type, string $data): void {
108+
Process::OUT === $type ? $this->output->write($data) : $this->output->write("<fg=red>$data</>");
109+
};
110+
}
106111
}

tests/Unit/Generators/GeneratorTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,9 @@
2727
});
2828

2929
it('can run string cmd', function (): void {
30-
expect($this->generator->runProcess('echo foo'))->isSuccessful()->toBeTrue();
30+
expect(
31+
(function () {
32+
return $this->runProcess('echo foo');
33+
})->call($this->generator)
34+
)->isSuccessful()->toBeTrue();
3135
})->group(__DIR__, __FILE__);

0 commit comments

Comments
 (0)