fix: psalm errors

This commit is contained in:
2026-02-15 17:01:51 +01:00
parent 6f2cb7e69d
commit dd94775d9c
11 changed files with 96 additions and 27 deletions

View File

@@ -53,7 +53,7 @@ final class Child
/** /**
* @param resource $process The child's process resource handle. * @param resource $process The child's process resource handle.
* @param array<int, resource> $pipes * @param array<array-key, resource> $pipes
* The indexed array of file pointers that set by {@see \proc_open()}. * The indexed array of file pointers that set by {@see \proc_open()}.
* *
* @throws InvalidArgumentException * @throws InvalidArgumentException

View File

@@ -6,12 +6,18 @@ namespace Nih\CommandBuilder;
use Override; use Override;
/**
*
*/
final class ChildStderr implements StdioInterface final class ChildStderr implements StdioInterface
{ {
use StreamReadTrait; use StreamReadTrait;
/**
* @return resource
*/
#[Override] #[Override]
public function getDescriptionSpec(int $fd): mixed public function getDescriptiorSpec(int $fd): mixed
{ {
return $this->stream; return $this->stream;
} }

View File

@@ -10,8 +10,11 @@ final class ChildStdin implements StdioInterface
{ {
use StreamWriteTrait; use StreamWriteTrait;
/**
* @return resource
*/
#[Override] #[Override]
public function getDescriptionSpec(int $fd): mixed public function getDescriptiorSpec(int $fd): mixed
{ {
return $this->stream; return $this->stream;
} }

View File

@@ -10,8 +10,11 @@ final class ChildStdout implements StdioInterface
{ {
use StreamReadTrait; use StreamReadTrait;
/**
* @return resource
*/
#[Override] #[Override]
public function getDescriptionSpec(int $fd): mixed public function getDescriptiorSpec(int $fd): mixed
{ {
return $this->stream; return $this->stream;
} }

View File

@@ -17,9 +17,18 @@ final class Command implements Stringable
{ {
public readonly string $program; public readonly string $program;
/**
* @var list<string>
*/
private array $args = []; private array $args = [];
/**
* @var array<string, string|null>
*/
private ?array $environment = null; private ?array $environment = null;
private bool $environmentInherit = true; private bool $environmentInherit = true;
private ?string $cwd = null; private ?string $cwd = null;
private ?StdioInterface $stdin = null; private ?StdioInterface $stdin = null;
@@ -90,7 +99,7 @@ final class Command implements Stringable
* escaped characters, word splitting, glob patterns, variable substitution, * escaped characters, word splitting, glob patterns, variable substitution,
* etc. have no effect. * etc. have no effect.
* *
* @param iterable<string|Stringable> $args * @param iterable<mixed, string|Stringable> $args
*/ */
public function args(iterable $args): static public function args(iterable $args): static
{ {
@@ -118,7 +127,7 @@ final class Command implements Stringable
*/ */
public function env(string $key, string|Stringable $val): static public function env(string $key, string|Stringable $val): static
{ {
$this->environment[$key] = $val; $this->environment[$key] = (string) $val;
return $this; return $this;
} }
@@ -285,13 +294,13 @@ final class Command implements Stringable
{ {
return $this->spawnWithDescriptorSpec([ return $this->spawnWithDescriptorSpec([
$this->stdin instanceof StdioInterface $this->stdin instanceof StdioInterface
? $this->stdin->getDescriptionSpec(0) ? $this->stdin->getDescriptiorSpec(0)
: STDIN, : STDIN,
$this->stdout instanceof StdioInterface $this->stdout instanceof StdioInterface
? $this->stdout->getDescriptionSpec(1) ? $this->stdout->getDescriptiorSpec(1)
: STDOUT, : STDOUT,
$this->stderr instanceof StdioInterface $this->stderr instanceof StdioInterface
? $this->stderr->getDescriptionSpec(2) ? $this->stderr->getDescriptiorSpec(2)
: STDERR, : STDERR,
]); ]);
} }
@@ -322,13 +331,13 @@ final class Command implements Stringable
{ {
return $this->spawnWithDescriptorSpec([ return $this->spawnWithDescriptorSpec([
$this->stdin instanceof StdioInterface $this->stdin instanceof StdioInterface
? $this->stdin->getDescriptionSpec(0) ? $this->stdin->getDescriptiorSpec(0)
: ['pipe', 'r'], : ['pipe', 'r'],
$this->stdout instanceof StdioInterface $this->stdout instanceof StdioInterface
? $this->stdout->getDescriptionSpec(1) ? $this->stdout->getDescriptiorSpec(1)
: ['pipe', 'w'], : ['pipe', 'w'],
$this->stderr instanceof StdioInterface $this->stderr instanceof StdioInterface
? $this->stderr->getDescriptionSpec(2) ? $this->stderr->getDescriptiorSpec(2)
: ['pipe', 'w'], : ['pipe', 'w'],
])->waitWithOutput(); ])->waitWithOutput();
} }
@@ -345,6 +354,8 @@ final class Command implements Stringable
* Returns the arguments that will be passed to the program. * Returns the arguments that will be passed to the program.
* *
* This does not include the path to the program as the first argument. * This does not include the path to the program as the first argument.
*
* @return list<string>
*/ */
public function getArgs(): array public function getArgs(): array
{ {
@@ -360,6 +371,8 @@ final class Command implements Stringable
* *
* Note that this output does not include environment variables inherited * Note that this output does not include environment variables inherited
* from the parent process. * from the parent process.
*
* @return array<string, string|null>
*/ */
public function getEnvs(): array public function getEnvs(): array
{ {
@@ -385,6 +398,9 @@ final class Command implements Stringable
]); ]);
} }
/**
* @param array<int, resource|list<string>> $descriptorSpec
*/
private function spawnWithDescriptorSpec(array $descriptorSpec): Child private function spawnWithDescriptorSpec(array $descriptorSpec): Child
{ {
// Find executable if path is not absolute. // Find executable if path is not absolute.
@@ -393,7 +409,7 @@ final class Command implements Stringable
$path = getenv('PATH'); $path = getenv('PATH');
if (is_string($path)) { if (is_string($path)) {
foreach (explode(':', $path) as $path) { foreach (explode(':', $path) as $path) {
$path = $path . '/' . $program; $path = $path . DIRECTORY_SEPARATOR . $program;
if (is_executable($path)) { if (is_executable($path)) {
$program = $path; $program = $path;
break; break;
@@ -412,10 +428,15 @@ final class Command implements Stringable
} }
} }
$command = [$program];
foreach ($this->args as $arg) {
$command[] = $arg;
}
try { try {
set_error_handler(ChildException::handleError(...)); set_error_handler(ChildException::handleError(...));
$proc = proc_open( $proc = proc_open(
[$program, ...$this->args], $command,
$descriptorSpec, $descriptorSpec,
$pipes, $pipes,
$this->cwd, $this->cwd,

View File

@@ -11,6 +11,8 @@ namespace Nih\CommandBuilder;
* child process. Child processes are created via the {@see Command} class and * child process. Child processes are created via the {@see Command} class and
* their exit status is exposed through the status method, or the wait method of * their exit status is exposed through the status method, or the wait method of
* a Child process. * a Child process.
*
* @api
*/ */
final readonly class ExitStatus final readonly class ExitStatus
{ {
@@ -24,6 +26,8 @@ final readonly class ExitStatus
/** /**
* Was termination successful? Signal termination is not considered a * Was termination successful? Signal termination is not considered a
* success, and success is defined as a zero exit status. * success, and success is defined as a zero exit status.
*
* @api
*/ */
public function success(): bool public function success(): bool
{ {
@@ -34,6 +38,8 @@ final readonly class ExitStatus
/** /**
* The exit code of the process, if any. * The exit code of the process, if any.
*
* @api
*/ */
public function code(): ?int public function code(): ?int
{ {
@@ -42,6 +48,8 @@ final readonly class ExitStatus
/** /**
* If the process was terminated by a signal, returns that signal. * If the process was terminated by a signal, returns that signal.
*
* @api
*/ */
public function signal(): ?int public function signal(): ?int
{ {
@@ -50,6 +58,8 @@ final readonly class ExitStatus
/** /**
* If the process was stopped by a signal, returns that signal. * If the process was stopped by a signal, returns that signal.
*
* @api
*/ */
public function stoppedSignal(): ?int public function stoppedSignal(): ?int
{ {

View File

@@ -23,13 +23,13 @@ abstract class Stdio
{ {
return new class implements StdioInterface { return new class implements StdioInterface {
#[Override] #[Override]
public function getDescriptionSpec(int $fd): mixed public function getDescriptiorSpec(int $fd): mixed
{ {
return match ($fd) { return match ($fd) {
0 => STDIN, 0 => STDIN,
1 => STDOUT, 1 => STDOUT,
2 => STDERR, 2 => STDERR,
default => ['php://fd/' . $fd, 'w+'], default => ['file', 'php://fd/' . $fd, 'w+'],
}; };
} }
}; };
@@ -41,8 +41,11 @@ abstract class Stdio
public static function piped(): StdioInterface public static function piped(): StdioInterface
{ {
return new class implements StdioInterface { return new class implements StdioInterface {
/**
* @return list<string>
*/
#[Override] #[Override]
public function getDescriptionSpec(int $fd): mixed public function getDescriptiorSpec(int $fd): mixed
{ {
return match ($fd) { return match ($fd) {
0 => ['pipe', 'r'], 0 => ['pipe', 'r'],
@@ -60,8 +63,11 @@ abstract class Stdio
public static function null(): StdioInterface public static function null(): StdioInterface
{ {
return new class implements StdioInterface { return new class implements StdioInterface {
/**
* @return list<string>
*/
#[Override] #[Override]
public function getDescriptionSpec(int $fd): mixed public function getDescriptiorSpec(int $fd): mixed
{ {
// TODO: Support windows (I think you just write to `nul` in any // TODO: Support windows (I think you just write to `nul` in any
// directory?) // directory?)
@@ -86,8 +92,11 @@ abstract class Stdio
private string $mode, private string $mode,
) {} ) {}
/**
* @return list<string>
*/
#[Override] #[Override]
public function getDescriptionSpec(int $fd): mixed public function getDescriptiorSpec(int $fd): mixed
{ {
return ['file', $this->filename, $this->mode]; return ['file', $this->filename, $this->mode];
} }
@@ -109,10 +118,17 @@ abstract class Stdio
} }
return new class($stream) implements StdioInterface { return new class($stream) implements StdioInterface {
public function __construct(private mixed $stream) {} /**
* @param resource $stream
*/
public function __construct(private mixed $stream)
{}
/**
* @return resource
*/
#[Override] #[Override]
public function getDescriptionSpec(int $fd): mixed public function getDescriptiorSpec(int $fd): mixed
{ {
return $this->stream; return $this->stream;
} }

View File

@@ -4,9 +4,6 @@ declare(strict_types=1);
namespace Nih\CommandBuilder; namespace Nih\CommandBuilder;
use InvalidArgumentException;
use Stringable;
/** /**
* Describes what to do with a standard I/O stream for a child process when * Describes what to do with a standard I/O stream for a child process when
* passed to the stdin, stdout, and stderr methods of {@see Command}. * passed to the stdin, stdout, and stderr methods of {@see Command}.
@@ -14,7 +11,7 @@ use Stringable;
interface StdioInterface interface StdioInterface
{ {
/** /**
* @return resource|array{0: 'file', 1: string, 2: string}|array{0: 'pipe', 1: string} * @return resource|list<string>
*/ */
public function getDescriptionSpec(int $fd): mixed; public function getDescriptiorSpec(int $fd): mixed;
} }

View File

@@ -9,6 +9,8 @@ trait StreamReadTrait
use StreamTrait; use StreamTrait;
/** /**
* @param int<1, max> $length
*
* @throws StreamException * @throws StreamException
*/ */
public function read(int $length): ?string public function read(int $length): ?string
@@ -30,6 +32,8 @@ trait StreamReadTrait
} }
/** /**
* @param int<1, max>|null $length
*
* @throws StreamException * @throws StreamException
*/ */
public function getContents(?int $length = null, int $offset = -1): string public function getContents(?int $length = null, int $offset = -1): string

View File

@@ -13,7 +13,7 @@ trait StreamTrait
* *
* @throws InvalidArgumentException When stream is not a stream * @throws InvalidArgumentException When stream is not a stream
*/ */
public function __construct(private readonly mixed $stream) public function __construct(private mixed $stream)
{ {
if (get_resource_type($stream) !== 'stream') { if (get_resource_type($stream) !== 'stream') {
throw new InvalidArgumentException('resource is not a stream'); throw new InvalidArgumentException('resource is not a stream');
@@ -33,6 +33,13 @@ trait StreamTrait
return; return;
} }
/**
* Psalm is not happy that we close the resource, but I could not
* find any way to annotate it so psalm understands what we do
* here...
*
* @psalm-suppress InvalidPropertyAssignmentValue
*/
try { try {
set_error_handler(StreamException::handleError(...)); set_error_handler(StreamException::handleError(...));
$success = fclose($this->stream); $success = fclose($this->stream);

View File

@@ -9,6 +9,8 @@ trait StreamWriteTrait
use StreamTrait; use StreamTrait;
/** /**
* @param int<0, max>|null $length
*
* @throws StreamException * @throws StreamException
*/ */
public function write(string $data, ?int $length = null): int public function write(string $data, ?int $length = null): int