Skip to content

Commit

Permalink
Merge pull request #315 from jolicode/context-simplify
Browse files Browse the repository at this point in the history
chore: simplify context class
  • Loading branch information
lyrixx committed Mar 9, 2024
2 parents a3f8467 + 41f0756 commit 667611a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 152 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Expand Up @@ -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\\<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
173 changes: 28 additions & 145 deletions src/Context.php
Expand Up @@ -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<string, string|\Stringable|int> $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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tools/phpstan/castor.php
Expand Up @@ -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();
}

0 comments on commit 667611a

Please sign in to comment.