Skip to content

Commit

Permalink
Merge pull request #327 from jolicode/revert-context
Browse files Browse the repository at this point in the history
Revert simplification of context
  • Loading branch information
lyrixx committed Mar 14, 2024
2 parents 4876c42 + 7b0f242 commit bfbe678
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 28 deletions.
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

0 comments on commit bfbe678

Please sign in to comment.