From 41f0756232bf441e0919a76045bc6cd8d5701939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Fri, 8 Mar 2024 17:42:22 +0100 Subject: [PATCH] chore: simplify context class --- phpstan-baseline.neon | 5 -- src/Context.php | 173 +++++++-------------------------------- tools/phpstan/castor.php | 4 +- 3 files changed, 30 insertions(+), 152 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 9c18cc20..f3466632 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -10,11 +10,6 @@ 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 f44be766..b1fd9ad1 100644 --- a/src/Context.php +++ b/src/Context.php @@ -58,198 +58,64 @@ 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 new self( - $data, - $this->environment, - $this->currentDirectory, - $this->tty, - $this->pty, - $this->timeout, - $this->quiet, - $this->allowFailure, - $this->notify, - $this->verbosityLevel, - $this->name, - ); + return $this->with(data: $data); } /** @param array $environment */ public function withEnvironment(array $environment, bool $keepExisting = true): self { - 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, - ); + return $this->with(environment: $keepExisting ? [...$this->environment, ...$environment] : $environment); } public function withPath(string $path): self { - return new self( - $this->data, - $this->environment, - str_starts_with($path, '/') ? $path : PathHelper::realpath($this->currentDirectory . '/' . $path), - $this->tty, - $this->pty, - $this->timeout, - $this->quiet, - $this->allowFailure, - $this->notify, - $this->verbosityLevel, - $this->name, - ); + return $this->with(currentDirectory: str_starts_with($path, '/') ? $path : PathHelper::realpath($this->currentDirectory . '/' . $path)); } public function withTty(bool $tty = true): self { - return new self( - $this->data, - $this->environment, - $this->currentDirectory, - $tty, - $this->pty, - $this->timeout, - $this->quiet, - $this->allowFailure, - $this->notify, - $this->verbosityLevel, - $this->name, - ); + return $this->with(tty: $tty); } public function withPty(bool $pty = true): self { - return new self( - $this->data, - $this->environment, - $this->currentDirectory, - $this->tty, - $pty, - $this->timeout, - $this->quiet, - $this->allowFailure, - $this->notify, - $this->verbosityLevel, - $this->name, - ); + return $this->with(pty: $pty); } public function withTimeout(?float $timeout): self { - return new self( - $this->data, - $this->environment, - $this->currentDirectory, - $this->tty, - $this->pty, - $timeout, - $this->quiet, - $this->allowFailure, - $this->notify, - $this->verbosityLevel, - $this->name, - ); + return $this->with(timeout: $timeout); } public function withQuiet(bool $quiet = true): self { - return new self( - $this->data, - $this->environment, - $this->currentDirectory, - $this->tty, - $this->pty, - $this->timeout, - $quiet, - $this->allowFailure, - $this->notify, - $this->verbosityLevel, - $this->name, - ); + return $this->with(quiet: $quiet); } public function withAllowFailure(bool $allowFailure = true): self { - return new self( - $this->data, - $this->environment, - $this->currentDirectory, - $this->tty, - $this->pty, - $this->timeout, - $this->quiet, - $allowFailure, - $this->notify, - $this->verbosityLevel, - $this->name, - ); + return $this->with(allowFailure: $allowFailure); } public function withNotify(bool $notify = true): self { - return new self( - $this->data, - $this->environment, - $this->currentDirectory, - $this->tty, - $this->pty, - $this->timeout, - $this->quiet, - $this->allowFailure, - $notify, - $this->verbosityLevel, - $this->name, - ); + return $this->with(notify: $notify); } public function withVerbosityLevel(VerbosityLevel $verbosityLevel): self { - return new self( - $this->data, - $this->environment, - $this->currentDirectory, - $this->tty, - $this->pty, - $this->timeout, - $this->quiet, - $this->allowFailure, - $this->notify, - $verbosityLevel, - $this->name, - ); + return $this->with(verbosityLevel: $verbosityLevel); } public function withName(string $name): self { - return new self( - $this->data, - $this->environment, - $this->currentDirectory, - $this->tty, - $this->pty, - $this->timeout, - $this->quiet, - $this->allowFailure, - $this->notify, - $this->verbosityLevel, - $name, - ); + return $this->with(name: $name); } public function offsetExists(mixed $offset): bool @@ -276,6 +142,23 @@ 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 diff --git a/tools/phpstan/castor.php b/tools/phpstan/castor.php index 1256de83..596b6354 100644 --- a/tools/phpstan/castor.php +++ b/tools/phpstan/castor.php @@ -7,10 +7,10 @@ use function Castor\run; #[AsTask(description: 'Run PHPStan', aliases: ['phpstan'])] -function phpstan(): int +function phpstan(bool $generateBaseline = false): int { return run( - [__DIR__ . '/vendor/bin/phpstan', '--configuration=' . \dirname(__DIR__, 2) . '/phpstan.neon'], + [__DIR__ . '/vendor/bin/phpstan', '--configuration=' . \dirname(__DIR__, 2) . '/phpstan.neon', $generateBaseline ? '-b' : ''], allowFailure: true, )->getExitCode(); }