Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code modernization with finals and readonly #7

Merged
merged 1 commit into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/Assault/ExceptionAssault.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@
use Chaos\Monkey\Assault;
use Chaos\Monkey\Settings;

class ExceptionAssault implements Assault
final class ExceptionAssault implements Assault
{
private Settings $settings;

public function __construct(Settings $settings)
public function __construct(private readonly Settings $settings)
{
$this->settings = $settings;
}

public function isActive(): bool
Expand Down
5 changes: 1 addition & 4 deletions src/Assault/KillAppAssault.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@

class KillAppAssault implements Assault
{
private Settings $settings;

public function __construct(Settings $settings)
public function __construct(private readonly Settings $settings)
{
$this->settings = $settings;
}

public function isActive(): bool
Expand Down
9 changes: 3 additions & 6 deletions src/Assault/LatencyAssault.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@
use Chaos\Monkey\Assault;
use Chaos\Monkey\Settings;

class LatencyAssault implements Assault
final class LatencyAssault implements Assault
{
private Settings $settings;

public function __construct(Settings $settings)
public function __construct(private readonly Settings $settings)
{
$this->settings = $settings;
}

public function isActive(): bool
Expand All @@ -23,7 +20,7 @@ public function isActive(): bool

public function attack(): void
{
$latencyMs = random_int($this->settings->latencyMinMs(), $this->settings->latencyMaxMs());
$latencyMs = \random_int($this->settings->latencyMinMs(), $this->settings->latencyMaxMs());
usleep($latencyMs * 1000);
}
}
10 changes: 4 additions & 6 deletions src/Assault/MemoryAssault.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@
use Chaos\Monkey\Assault;
use Chaos\Monkey\Settings;

class MemoryAssault implements Assault
final class MemoryAssault implements Assault
{
private Settings $settings;
private int $totalMemory;

/**
* @var mixed[]
* @var int[][]
*/
private array $memoryVector;

public function __construct(Settings $settings)
public function __construct(private readonly Settings $settings)
{
$this->settings = $settings;
$this->totalMemory = (new Runtime())->totalMemory();
}

Expand All @@ -37,7 +35,7 @@ public function attack(): void
}

/**
* @return mixed[]
* @return int[][]
*/
public function memoryVector(): array
{
Expand Down
25 changes: 7 additions & 18 deletions src/ChaosMonkey.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,13 @@

namespace Chaos\Monkey;

class ChaosMonkey
final class ChaosMonkey
{
/**
* @var Assault[]
* @param list<Assault> $assaults
*/
private array $assaults = [];

private Settings $settings;

/**
* @param iterable<Assault> $assaults
*/
public function __construct(iterable $assaults, Settings $settings)
public function __construct(private readonly array $assaults, private readonly Settings $settings)
{
foreach ($assaults as $assault) {
$this->assaults[] = $assault;
}
$this->settings = $settings;
}

public function call(): void
Expand All @@ -43,7 +32,7 @@ private function isEnable(): bool

private function isTrouble(): bool
{
return $this->settings->probability() >= random_int(0, 100);
return $this->settings->probability() >= \random_int(0, 100);
}

private function chooseAndRunAttack(): void
Expand All @@ -57,15 +46,15 @@ private function chooseAndRunAttack(): void
}

/**
* @return Assault[]
* @return list<Assault>
*/
private function getActiveAssaults(): array
{
return array_filter($this->assaults, fn (Assault $assault) => $assault->isActive());
return array_values(array_filter($this->assaults, fn (Assault $assault) => $assault->isActive()));
}

/**
* @param Assault[] $assaults
* @param list<Assault> $assaults
*/
private function getRandomAssault(array $assaults): Assault
{
Expand Down
2 changes: 1 addition & 1 deletion src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Chaos\Monkey;

class Settings
final class Settings
{
private bool $enabled;
private int $probability;
Expand Down
Loading