From b2ac1b559a6190083232dd6c053765255ea91bc0 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Wed, 6 Aug 2025 15:46:36 +0200 Subject: [PATCH] add NoDiscard attribute on mutation free methods and the ones return Attempt --- src/Server.php | 5 +++++ src/Server/Command.php | 14 ++++++++++++++ src/Server/Process.php | 3 +++ src/Server/Process/ExitCode.php | 3 +++ src/Server/Process/Failed.php | 2 ++ src/Server/Process/Output/Chunk.php | 3 +++ src/Server/Process/Output/Type.php | 1 + src/Server/Process/Pid.php | 2 ++ src/Server/Process/Signaled.php | 1 + src/Server/Process/Success.php | 1 + src/Server/Process/TimedOut.php | 1 + src/Server/Processes.php | 2 ++ src/Server/Script.php | 2 ++ src/Server/Signal.php | 2 ++ src/Server/Volumes.php | 2 ++ src/ServerFactory.php | 1 + 16 files changed, 45 insertions(+) diff --git a/src/Server.php b/src/Server.php index 7f91a36..5cf699b 100644 --- a/src/Server.php +++ b/src/Server.php @@ -14,16 +14,21 @@ interface Server { + #[\NoDiscard] public function processes(): Processes; + + #[\NoDiscard] public function volumes(): Volumes; /** * @return Attempt */ + #[\NoDiscard] public function reboot(): Attempt; /** * @return Attempt */ + #[\NoDiscard] public function shutdown(): Attempt; } diff --git a/src/Server/Command.php b/src/Server/Command.php index d6ef66d..40513c6 100644 --- a/src/Server/Command.php +++ b/src/Server/Command.php @@ -74,6 +74,7 @@ private function __construct(bool $background, string $executable) * * @param non-empty-string $executable */ + #[\NoDiscard] public static function background(string $executable): self { return new self(true, $executable); @@ -87,11 +88,13 @@ public static function background(string $executable): self * * @param non-empty-string $executable */ + #[\NoDiscard] public static function foreground(string $executable): self { return new self(false, $executable); } + #[\NoDiscard] public function withArgument(string $value): self { $self = clone $this; @@ -103,6 +106,7 @@ public function withArgument(string $value): self /** * @param non-empty-string $key */ + #[\NoDiscard] public function withOption(string $key, ?string $value = null): self { $self = clone $this; @@ -114,6 +118,7 @@ public function withOption(string $key, ?string $value = null): self /** * @param non-empty-string $key */ + #[\NoDiscard] public function withShortOption(string $key, ?string $value = null): self { $self = clone $this; @@ -125,6 +130,7 @@ public function withShortOption(string $key, ?string $value = null): self /** * @param non-empty-string $key */ + #[\NoDiscard] public function withEnvironment(string $key, string $value): self { $self = clone $this; @@ -136,6 +142,7 @@ public function withEnvironment(string $key, string $value): self /** * @param Map $values */ + #[\NoDiscard] public function withEnvironments(Map $values): self { $self = clone $this; @@ -144,6 +151,7 @@ public function withEnvironments(Map $values): self return $self; } + #[\NoDiscard] public function withWorkingDirectory(Path $path): self { $self = clone $this; @@ -152,6 +160,7 @@ public function withWorkingDirectory(Path $path): self return $self; } + #[\NoDiscard] public function withInput(Content $input): self { $self = clone $this; @@ -160,6 +169,7 @@ public function withInput(Content $input): self return $self; } + #[\NoDiscard] public function overwrite(Path $path): self { $self = clone $this; @@ -168,6 +178,7 @@ public function overwrite(Path $path): self return $self; } + #[\NoDiscard] public function append(Path $path): self { $self = clone $this; @@ -176,6 +187,7 @@ public function append(Path $path): self return $self; } + #[\NoDiscard] public function pipe(self $command): self { $self = clone $this; @@ -195,6 +207,7 @@ public function pipe(self $command): self return $self; } + #[\NoDiscard] public function timeoutAfter(Period $timeout): self { $self = clone $this; @@ -213,6 +226,7 @@ public function timeoutAfter(Period $timeout): self * This is useful in the case you need to access the output but can't fit it * in memory like streaming large files. */ + #[\NoDiscard] public function streamOutput(): self { $self = clone $this; diff --git a/src/Server/Process.php b/src/Server/Process.php index 8cc951a..26af847 100644 --- a/src/Server/Process.php +++ b/src/Server/Process.php @@ -52,6 +52,7 @@ public static function background(Started $started): self * * @return Maybe */ + #[\NoDiscard] public function pid(): Maybe { return $this->implementation->pid(); @@ -60,6 +61,7 @@ public function pid(): Maybe /** * @return Sequence */ + #[\NoDiscard] public function output(): Sequence { return $this->implementation->output(); @@ -73,6 +75,7 @@ public function output(): Sequence * * @return Either */ + #[\NoDiscard] public function wait(): Either { return $this->implementation->wait(); diff --git a/src/Server/Process/ExitCode.php b/src/Server/Process/ExitCode.php index 69971dc..483afbb 100644 --- a/src/Server/Process/ExitCode.php +++ b/src/Server/Process/ExitCode.php @@ -17,6 +17,7 @@ public function __construct( ) { } + #[\NoDiscard] public function successful(): bool { return $this->value === 0; @@ -25,6 +26,7 @@ public function successful(): bool /** * @return int<0, 255> */ + #[\NoDiscard] public function toInt(): int { return $this->value; @@ -33,6 +35,7 @@ public function toInt(): int /** * @return non-empty-string */ + #[\NoDiscard] public function toString(): string { return (string) $this->value; diff --git a/src/Server/Process/Failed.php b/src/Server/Process/Failed.php index 9f390c6..cc36c09 100644 --- a/src/Server/Process/Failed.php +++ b/src/Server/Process/Failed.php @@ -17,6 +17,7 @@ public function __construct( ) { } + #[\NoDiscard] public function exitCode(): ExitCode { return $this->exitCode; @@ -25,6 +26,7 @@ public function exitCode(): ExitCode /** * @return Sequence */ + #[\NoDiscard] public function output(): Sequence { return $this->output; diff --git a/src/Server/Process/Output/Chunk.php b/src/Server/Process/Output/Chunk.php index 77037d3..4422ee8 100644 --- a/src/Server/Process/Output/Chunk.php +++ b/src/Server/Process/Output/Chunk.php @@ -19,16 +19,19 @@ private function __construct( /** * @psalm-pure */ + #[\NoDiscard] public static function of(Str $data, Type $type): self { return new self($data, $type); } + #[\NoDiscard] public function data(): Str { return $this->data; } + #[\NoDiscard] public function type(): Type { return $this->type; diff --git a/src/Server/Process/Output/Type.php b/src/Server/Process/Output/Type.php index 506d37e..7567eed 100644 --- a/src/Server/Process/Output/Type.php +++ b/src/Server/Process/Output/Type.php @@ -11,6 +11,7 @@ enum Type case output; case error; + #[\NoDiscard] public function toString(): string { return match ($this) { diff --git a/src/Server/Process/Pid.php b/src/Server/Process/Pid.php index 812d8ab..cc67859 100644 --- a/src/Server/Process/Pid.php +++ b/src/Server/Process/Pid.php @@ -20,6 +20,7 @@ public function __construct( /** * @return int<2, max> */ + #[\NoDiscard] public function toInt(): int { return $this->value; @@ -28,6 +29,7 @@ public function toInt(): int /** * @return non-empty-string */ + #[\NoDiscard] public function toString(): string { return (string) $this->value; diff --git a/src/Server/Process/Signaled.php b/src/Server/Process/Signaled.php index 2dd8a7e..65f6d9d 100644 --- a/src/Server/Process/Signaled.php +++ b/src/Server/Process/Signaled.php @@ -19,6 +19,7 @@ public function __construct( /** * @return Sequence */ + #[\NoDiscard] public function output(): Sequence { return $this->output; diff --git a/src/Server/Process/Success.php b/src/Server/Process/Success.php index c3fdeee..5644343 100644 --- a/src/Server/Process/Success.php +++ b/src/Server/Process/Success.php @@ -19,6 +19,7 @@ public function __construct( /** * @return Sequence */ + #[\NoDiscard] public function output(): Sequence { return $this->output; diff --git a/src/Server/Process/TimedOut.php b/src/Server/Process/TimedOut.php index bc6de75..042eb35 100644 --- a/src/Server/Process/TimedOut.php +++ b/src/Server/Process/TimedOut.php @@ -19,6 +19,7 @@ public function __construct( /** * @return Sequence */ + #[\NoDiscard] public function output(): Sequence { return $this->output; diff --git a/src/Server/Processes.php b/src/Server/Processes.php index 8510708..cb7611d 100644 --- a/src/Server/Processes.php +++ b/src/Server/Processes.php @@ -14,10 +14,12 @@ interface Processes /** * @return Attempt */ + #[\NoDiscard] public function execute(Command $command): Attempt; /** * @return Attempt */ + #[\NoDiscard] public function kill(Pid $pid, Signal $signal): Attempt; } diff --git a/src/Server/Script.php b/src/Server/Script.php index d29ec5f..06c2081 100644 --- a/src/Server/Script.php +++ b/src/Server/Script.php @@ -24,6 +24,7 @@ private function __construct( /** * @return Attempt */ + #[\NoDiscard] public function __invoke(Server $server): Attempt { $processes = $server->processes(); @@ -48,6 +49,7 @@ public function __invoke(Server $server): Attempt /** * @no-named-arguments */ + #[\NoDiscard] public static function of(Command ...$commands): self { return new self(Sequence::of(...$commands)); diff --git a/src/Server/Signal.php b/src/Server/Signal.php index 71688fd..787a0b4 100644 --- a/src/Server/Signal.php +++ b/src/Server/Signal.php @@ -16,6 +16,7 @@ enum Signal case alarm; case terminate; + #[\NoDiscard] public function toInt(): int { return match ($this) { @@ -32,6 +33,7 @@ public function toInt(): int /** * @return non-empty-string */ + #[\NoDiscard] public function toString(): string { return (string) $this->toInt(); diff --git a/src/Server/Volumes.php b/src/Server/Volumes.php index 1e78591..557dbc9 100644 --- a/src/Server/Volumes.php +++ b/src/Server/Volumes.php @@ -15,10 +15,12 @@ interface Volumes /** * @return Attempt */ + #[\NoDiscard] public function mount(Name $name, Path $mountpoint): Attempt; /** * @return Attempt */ + #[\NoDiscard] public function unmount(Name $name): Attempt; } diff --git a/src/ServerFactory.php b/src/ServerFactory.php index 20998ff..6431c76 100644 --- a/src/ServerFactory.php +++ b/src/ServerFactory.php @@ -19,6 +19,7 @@ final class ServerFactory /** * @throws UnsupportedOperatingSystem For windows system */ + #[\NoDiscard] public static function build( Clock $clock, IO $io,