From 3b5e7d9e6b30229685be987077a8de678744d92a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Thu, 14 Mar 2024 14:58:02 +0100 Subject: [PATCH] Revert simplification of context --- phpstan-baseline.neon | 5 ++ src/Context.php | 173 +++++++++++++++++++++++++++++++++++------- 2 files changed, 150 insertions(+), 28 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index f3466632..9c18cc20 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -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\\ given\\.$#" + count: 1 + path: src/Context.php + - message: "#^Function Castor\\\\get_exit_code\\(\\) has parameter \\$args with no type specified\\.$#" count: 1 diff --git a/src/Context.php b/src/Context.php index 7a8ee474..257eebcc 100644 --- a/src/Context.php +++ b/src/Context.php @@ -58,19 +58,45 @@ public function withData(array $data, bool $keepExisting = true, bool $recursive if ($keepExisting) { if ($recursive) { + /* @var array<(int|string), mixed> */ $data = $this->arrayMergeRecursiveDistinct($this->data, $data); } else { + /* @var array<(int|string), mixed> */ $data = array_merge($this->data, $data); } } - 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 $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 @@ -82,47 +108,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 @@ -149,23 +283,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