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

Revert simplification of context #327

Merged
merged 1 commit into from Mar 14, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Expand Up @@ -10,6 +10,11 @@ parameters:
count: 1
path: src/Context.php

-
message: "#^Parameter \\#1 \\$data of class Castor\\\\Context constructor expects array\\{name\\: string, production\\: bool, foo\\?\\: string\\}, array\\<int\\|string, mixed\\> given\\.$#"
count: 1
path: src/Context.php

-
message: "#^Function Castor\\\\get_exit_code\\(\\) has parameter \\$args with no type specified\\.$#"
count: 1
Expand Down
171 changes: 143 additions & 28 deletions src/Context.php
Expand Up @@ -64,13 +64,37 @@ public function withData(array $data, bool $keepExisting = true, bool $recursive
}
}

return $this->with(data: $data);
return new self(
$data,
$this->environment,
$this->currentDirectory,
$this->tty,
$this->pty,
$this->timeout,
$this->quiet,
$this->allowFailure,
$this->notify,
$this->verbosityLevel,
$this->name,
);
}

/** @param array<string, string|\Stringable|int> $environment */
public function withEnvironment(array $environment, bool $keepExisting = true): self
{
return $this->with(environment: $keepExisting ? [...$this->environment, ...$environment] : $environment);
return new self(
$this->data,
$keepExisting ? [...$this->environment, ...$environment] : $environment,
$this->currentDirectory,
$this->tty,
$this->pty,
$this->timeout,
$this->quiet,
$this->allowFailure,
$this->notify,
$this->verbosityLevel,
$this->name,
);
}

public function withPath(string $path): self
Expand All @@ -82,47 +106,155 @@ public function withPath(string $path): self

public function withCurrentDirectory(string $currentDirectory): self
{
return $this->with(currentDirectory: str_starts_with($currentDirectory, '/') ? $currentDirectory : PathHelper::realpath($this->currentDirectory . '/' . $currentDirectory));
return new self(
$this->data,
$this->environment,
str_starts_with($currentDirectory, '/') ? $currentDirectory : PathHelper::realpath($this->currentDirectory . '/' . $currentDirectory),
$this->tty,
$this->pty,
$this->timeout,
$this->quiet,
$this->allowFailure,
$this->notify,
$this->verbosityLevel,
$this->name,
);
}

public function withTty(bool $tty = true): self
{
return $this->with(tty: $tty);
return new self(
$this->data,
$this->environment,
$this->currentDirectory,
$tty,
$this->pty,
$this->timeout,
$this->quiet,
$this->allowFailure,
$this->notify,
$this->verbosityLevel,
$this->name,
);
}

public function withPty(bool $pty = true): self
{
return $this->with(pty: $pty);
return new self(
$this->data,
$this->environment,
$this->currentDirectory,
$this->tty,
$pty,
$this->timeout,
$this->quiet,
$this->allowFailure,
$this->notify,
$this->verbosityLevel,
$this->name,
);
}

public function withTimeout(?float $timeout): self
{
return $this->with(timeout: $timeout);
return new self(
$this->data,
$this->environment,
$this->currentDirectory,
$this->tty,
$this->pty,
$timeout,
$this->quiet,
$this->allowFailure,
$this->notify,
$this->verbosityLevel,
$this->name,
);
}

public function withQuiet(bool $quiet = true): self
{
return $this->with(quiet: $quiet);
return new self(
$this->data,
$this->environment,
$this->currentDirectory,
$this->tty,
$this->pty,
$this->timeout,
$quiet,
$this->allowFailure,
$this->notify,
$this->verbosityLevel,
$this->name,
);
}

public function withAllowFailure(bool $allowFailure = true): self
{
return $this->with(allowFailure: $allowFailure);
return new self(
$this->data,
$this->environment,
$this->currentDirectory,
$this->tty,
$this->pty,
$this->timeout,
$this->quiet,
$allowFailure,
$this->notify,
$this->verbosityLevel,
$this->name,
);
}

public function withNotify(bool $notify = true): self
{
return $this->with(notify: $notify);
return new self(
$this->data,
$this->environment,
$this->currentDirectory,
$this->tty,
$this->pty,
$this->timeout,
$this->quiet,
$this->allowFailure,
$notify,
$this->verbosityLevel,
$this->name,
);
}

public function withVerbosityLevel(VerbosityLevel $verbosityLevel): self
{
return $this->with(verbosityLevel: $verbosityLevel);
return new self(
$this->data,
$this->environment,
$this->currentDirectory,
$this->tty,
$this->pty,
$this->timeout,
$this->quiet,
$this->allowFailure,
$this->notify,
$verbosityLevel,
$this->name,
);
}

public function withName(string $name): self
{
return $this->with(name: $name);
return new self(
$this->data,
$this->environment,
$this->currentDirectory,
$this->tty,
$this->pty,
$this->timeout,
$this->quiet,
$this->allowFailure,
$this->notify,
$this->verbosityLevel,
$name,
);
}

public function offsetExists(mixed $offset): bool
Expand All @@ -149,23 +281,6 @@ public function offsetUnset(mixed $offset): void
throw new \LogicException('Context is immutable.');
}

private function with(mixed ...$args): self
{
return new self(
$args['data'] ?? $this->data,
$args['environment'] ?? $this->environment,
$args['currentDirectory'] ?? $this->currentDirectory,
$args['tty'] ?? $this->tty,
$args['pty'] ?? $this->pty,
$args['timeout'] ?? $this->timeout,
$args['quiet'] ?? $this->quiet,
$args['allowFailure'] ?? $this->allowFailure,
$args['notify'] ?? $this->notify,
$args['verbosityLevel'] ?? $this->verbosityLevel,
$args['name'] ?? $this->name,
);
}

/**
* @param array<(int|string), mixed> $array1
* @param array<(int|string), mixed> $array2
Expand Down