From dd081c59b7f57f529d2b5e4b31ea9f4fa2af11b0 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Sun, 5 Dec 2021 17:48:51 +0000 Subject: [PATCH] refacto: memory plugin --- composer.json | 6 +++--- src/Plugins/Coverage.php | 16 +++++++--------- src/Plugins/Environment.php | 6 +----- src/Plugins/Init.php | 3 +++ src/Plugins/Memory.php | 36 +++++++++++++++++++++--------------- src/Plugins/Version.php | 5 ++++- 6 files changed, 39 insertions(+), 33 deletions(-) diff --git a/composer.json b/composer.json index 6348d258..4be59682 100644 --- a/composer.json +++ b/composer.json @@ -79,12 +79,12 @@ }, "pest": { "plugins": [ - "Pest\\Plugins\\Memory", "Pest\\Plugins\\Coverage", "Pest\\Plugins\\Init", - "Pest\\Plugins\\Version", "Pest\\Plugins\\Environment", - "Pest\\Plugins\\Retry" + "Pest\\Plugins\\Memory", + "Pest\\Plugins\\Retry", + "Pest\\Plugins\\Version" ] }, "laravel": { diff --git a/src/Plugins/Coverage.php b/src/Plugins/Coverage.php index 9cf45f3b..18c1aa39 100644 --- a/src/Plugins/Coverage.php +++ b/src/Plugins/Coverage.php @@ -46,9 +46,7 @@ final class Coverage implements AddsOutput, HandlesArguments } /** - * @param array $originals - * - * @return array + * {@inheritdoc} */ public function handleArguments(array $originals): array { @@ -90,11 +88,11 @@ final class Coverage implements AddsOutput, HandlesArguments } /** - * Allows to add custom output after the test suite was executed. + * {@inheritdoc} */ - public function addOutput(int $result): int + public function addOutput(int $exitCode): int { - if ($result === 0 && $this->coverage) { + if ($exitCode === 0 && $this->coverage) { if (!\Pest\Support\Coverage::isAvailable()) { $this->output->writeln( "\n ERROR No code coverage driver is available.", @@ -104,9 +102,9 @@ final class Coverage implements AddsOutput, HandlesArguments $coverage = \Pest\Support\Coverage::report($this->output); - $result = (int) ($coverage < $this->coverageMin); + $exitCode = (int) ($coverage < $this->coverageMin); - if ($result === 1) { + if ($exitCode === 1) { $this->output->writeln(sprintf( "\n FAIL Code coverage below expected: %s %%. Minimum: %s %%.", number_format($coverage, 1), @@ -115,6 +113,6 @@ final class Coverage implements AddsOutput, HandlesArguments } } - return $result; + return $exitCode; } } diff --git a/src/Plugins/Environment.php b/src/Plugins/Environment.php index 52ad75ae..3b0e028f 100644 --- a/src/Plugins/Environment.php +++ b/src/Plugins/Environment.php @@ -27,11 +27,7 @@ final class Environment implements HandlesArguments private static ?string $name = null; /** - * Allows to handle custom command line arguments. - * - * @param array $arguments - * - * @return array the updated list of arguments + * {@inheritdoc} */ public function handleArguments(array $arguments): array { diff --git a/src/Plugins/Init.php b/src/Plugins/Init.php index abe9f966..331f2a99 100644 --- a/src/Plugins/Init.php +++ b/src/Plugins/Init.php @@ -38,6 +38,9 @@ final class Init implements HandlesArguments // .. } + /** + * {@inheritdoc} + */ public function handleArguments(array $arguments): array { if (!array_key_exists(1, $arguments) || $arguments[1] !== self::INIT_OPTION) { diff --git a/src/Plugins/Memory.php b/src/Plugins/Memory.php index ffaf3358..11d67d7e 100644 --- a/src/Plugins/Memory.php +++ b/src/Plugins/Memory.php @@ -13,30 +13,36 @@ use Symfony\Component\Console\Output\OutputInterface; */ final class Memory implements AddsOutput, HandlesArguments { - /** @var OutputInterface */ - private $output; + use Concerns\HandleArguments; + /** + * If memory should be displayed. + */ private bool $enabled = false; - public function __construct(OutputInterface $output) - { - $this->output = $output; + /** + * Creates a new Plugin instance. + */ + public function __construct( + private OutputInterface $output + ) { + // .. } + /** + * {@inheritdoc} + */ public function handleArguments(array $arguments): array { - foreach ($arguments as $index => $argument) { - if ($argument === '--memory') { - unset($arguments[$index]); + $this->enabled = $this->hasArgument('--memory', $arguments); - $this->enabled = true; - } - } - - return array_values($arguments); + return $this->popArgument('--memory', $arguments); } - public function addOutput(int $result): int + /** + * {@inheritdoc} + */ + public function addOutput(int $exitCode): int { if ($this->enabled) { $this->output->writeln(sprintf( @@ -45,6 +51,6 @@ final class Memory implements AddsOutput, HandlesArguments )); } - return $result; + return $exitCode; } } diff --git a/src/Plugins/Version.php b/src/Plugins/Version.php index a6383ecd..5452237a 100644 --- a/src/Plugins/Version.php +++ b/src/Plugins/Version.php @@ -16,7 +16,7 @@ final class Version implements HandlesArguments use Concerns\HandleArguments; /** - * Creates a new instance of the plugin. + * Creates a new Plugin instance. */ public function __construct( private OutputInterface $output @@ -24,6 +24,9 @@ final class Version implements HandlesArguments // .. } + /** + * {@inheritDoc} + */ public function handleArguments(array $arguments): array { if ($this->hasArgument('--version', $arguments)) {