Skip to content

Commit

Permalink
chore: Update composer.json and OutputManager.php
Browse files Browse the repository at this point in the history
- Update composer.json to include '@rector-dry-run' in the 'scripts' section
- Refactor OutputManager.php to remove unnecessary code and improve readability
  • Loading branch information
guanguans committed Jan 4, 2024
1 parent 6e87fbf commit 54ca794
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 39 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@
"@lint",
"@style-lint",
"@test",
"@psalm"
"@psalm",
"@rector-dry-run"
],
"checks-parallel": "@composer-parallel composer-validate md-lint lint style-lint test psalm",
"composer-bin-all-update": "@composer bin all update --ansi -v",
Expand Down
39 changes: 23 additions & 16 deletions src/OutputManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,35 @@
use Guanguans\LaravelSoar\Events\OutputtedEvent;
use Guanguans\LaravelSoar\Events\OutputtingEvent;
use Guanguans\LaravelSoar\Exceptions\InvalidArgumentException;
use Guanguans\LaravelSoar\Outputs\Concerns\ShouldOutput;
use Illuminate\Console\Events\CommandFinished;
use Illuminate\Support\Collection;
use Illuminate\Support\Fluent;
use Illuminate\Support\Str;

class OutputManager extends Fluent implements Output
{
use ShouldOutput;

/**
* @param array<\Guanguans\LaravelSoar\Contracts\Output> $outputs
*
* @noinspection MagicMethodsValidityInspection
* @noinspection MissingParentCallInspection
* @noinspection PhpMissingParentConstructorInspection
*/
public function __construct(array $outputs = [], array $exclusions = [])
public function __construct(array $outputs = [])
{
foreach ($outputs as $index => $output) {
$this->offsetSet($index, $output);
}

$this->exclusions = $exclusions;
}

/**
* @noinspection MissingParentCallInspection
*
* @param mixed $offset
* @param mixed $value
*/
public function offsetSet($offset, $value): void
public function shouldOutput($dispatcher): bool
{
if (! $value instanceof Output) {
throw new InvalidArgumentException(sprintf('The value must be instance of %s', Output::class));
$exclusions = config('soar.exclusions', []);
if ($dispatcher instanceof CommandFinished) {
return ! Str::is($exclusions, $dispatcher->command);
}

$this->attributes[$offset] = $value;
return ! request()->is($exclusions) && ! request()->routeIs($exclusions);
}

public function output(Collection $scores, $dispatcher): void
Expand All @@ -74,4 +66,19 @@ public function output(Collection $scores, $dispatcher): void
event(new OutputtedEvent($output, $scores, $result));
}
}

/**
* @noinspection MissingParentCallInspection
*
* @param mixed $offset
* @param mixed $value
*/
public function offsetSet($offset, $value): void
{
if (! $value instanceof Output) {
throw new InvalidArgumentException(sprintf('The value must be instance of %s', Output::class));
}

$this->attributes[$offset] = $value;
}
}
2 changes: 1 addition & 1 deletion src/Outputs/ClockworkOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ClockworkOutput extends Output
{
public function shouldOutput($dispatcher): bool
{
return \function_exists('clock') && parent::shouldOutput($dispatcher);
return \function_exists('clock');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Outputs/ConsoleOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(string $method = 'warn')

public function shouldOutput($dispatcher): bool
{
return $this->isHtmlResponse($dispatcher) && parent::shouldOutput($dispatcher);
return $this->isHtmlResponse($dispatcher);
}

public function output(Collection $scores, $dispatcher): void
Expand Down
3 changes: 1 addition & 2 deletions src/Outputs/DebugBarOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ public function shouldOutput($dispatcher): bool
// app(LaravelDebugbar::class)->isEnabled()
return class_exists(LaravelDebugbar::class)
&& app()->has(LaravelDebugbar::class)
&& $this->isHtmlResponse($dispatcher)
&& parent::shouldOutput($dispatcher);
&& $this->isHtmlResponse($dispatcher);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Outputs/JsonOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(string $key = 'soar_scores')

public function shouldOutput($dispatcher): bool
{
return $this->isJsonResponse($dispatcher) && parent::shouldOutput($dispatcher);
return $this->isJsonResponse($dispatcher);
}

public function output(Collection $scores, $dispatcher): void
Expand Down
8 changes: 1 addition & 7 deletions src/Outputs/NullOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,10 @@

namespace Guanguans\LaravelSoar\Outputs;

use Guanguans\LaravelSoar\Contracts\Output;
use Illuminate\Support\Collection;

class NullOutput implements Output
class NullOutput extends Output
{
public function shouldOutput($dispatcher): bool
{
return true;
}

public function output(Collection $scores, $dispatcher): void
{
// noop
Expand Down
7 changes: 5 additions & 2 deletions src/Outputs/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
use Guanguans\LaravelSoar\Outputs\Concerns\OutputConditions;
use Guanguans\LaravelSoar\Outputs\Concerns\ScoresHydrator;
use Guanguans\LaravelSoar\Outputs\Concerns\ScoresSanitizer;
use Guanguans\LaravelSoar\Outputs\Concerns\ShouldOutput;

abstract class Output implements \Guanguans\LaravelSoar\Contracts\Output, Sanitizer
{
use OutputConditions;
use ScoresHydrator;
use ScoresSanitizer;
use ShouldOutput;

public function shouldOutput($dispatcher): bool
{
return true;
}
}
2 changes: 1 addition & 1 deletion src/Outputs/RayOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(string $label = 'Soar Scores')

public function shouldOutput($dispatcher): bool
{
return \function_exists('ray') && parent::shouldOutput($dispatcher);
return \function_exists('ray');
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/Outputs/SoarBarOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ public function __construct(string $name = 'Scores', string $label = 'warning')

public function shouldOutput($dispatcher): bool
{
return ! DebugBarOutput::isOutputted()
&& $this->isHtmlResponse($dispatcher)
&& parent::shouldOutput($dispatcher);
return ! DebugBarOutput::isOutputted() && $this->isHtmlResponse($dispatcher);
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/SoarServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,7 @@ protected function registerOutputManager(): void

return [$class => $container->make($class, $parameters)];
})
->pipe(static fn (Collection $collection): OutputManager => new OutputManager(
$collection->all(),
config('soar.exclusions')
))
->pipe(static fn (Collection $collection): OutputManager => new OutputManager($collection->all()))
);

$this->app->alias(OutputManager::class, $this->toAlias(OutputManager::class));
Expand Down

0 comments on commit 54ca794

Please sign in to comment.