Skip to content

Commit

Permalink
fixed PHPStan issues with previous 2 commits
Browse files Browse the repository at this point in the history
  • Loading branch information
llaville committed Sep 4, 2024
1 parent 266e0ff commit 75b81b0
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 17 deletions.
6 changes: 4 additions & 2 deletions src/Console/Command/Make.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$payload = [
'pid' => $pid,
'configuration' => $config,
'config' => $config->getConfigurationFile(),
'ansiSupport' => $output->isDecorated(),
'immutableCopy' => $io->getTypedOption(ManifestOptions::IMMUTABLE_OPTION)->asBoolean(),
'versions' => [
Expand Down Expand Up @@ -289,7 +288,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$logger->error('Workflow has failed', $context);
$isSuccessful = false;
} finally {
if ($isSuccessful) {
if ($isSuccessful) { // @phpstan-ignore variable.undefined
$logger->notice(
sprintf(
'Workflow has finished. Elapsed time %s',
Expand Down Expand Up @@ -322,6 +321,9 @@ private function changeWorkingDir(?string $newWorkingDir): ?string
return $newWorkingDir;
}

/**
* @param array{pid: string} $context
*/
private function getCustomStage(string $stageClass, IO $io, Command $command, LoggerInterface $logger, array $context): StageInterface
{
if (!class_exists($stageClass)) {
Expand Down
4 changes: 3 additions & 1 deletion src/Console/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public function __construct(
}

/**
* @param string $level
* @param string $message
* @param array{
* status?: string,
* id?: int,
Expand All @@ -48,7 +50,7 @@ public function __construct(
public function log($level, $message, array $context = []): void
{
if (isset($context['status']) && isset($context['id'])) {
$id = $context['id'];
$id = sprintf('%d', $context['id']);
$error = $context['error'] ?? false;
$message = match ($context['status']) {
self::STATUS_STARTED => $this->helper->start($id, $message, $context['prefix'] ?? 'RUN'),
Expand Down
31 changes: 20 additions & 11 deletions src/Pipeline/AbstractStage.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
use Psr\Log\LoggerInterface;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\HelperInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\StreamOutput;

use function file_exists;
use function file_get_contents;
use function fopen;
use function get_resource_id;
use function realpath;
use function sprintf;
use function unserialize;
Expand All @@ -32,33 +32,38 @@
public const BOX_MANIFESTS_DIR = '.box.manifests/';
public const META_DATA_FILE = '.box.manifests.bin';

private ?HelperInterface $debugFormatterHelper;

/**
* @param array{pid: string} $context
*/
public function __construct(
final public function __construct(
protected IO $io,
protected Command $command,
protected LoggerInterface $logger,
protected array $context
) {
$this->debugFormatterHelper = $this->command->getHelperSet()?->has('debug_formatter')
? $this->command->getHelper('debug_formatter')
: null
;
}

/**
* @param array{pid: string} $context
*/
public static function create(IO $io, Command $command, LoggerInterface $logger, array $context): static
{
return new static($io, $command, $logger, $context);
}

/**
* @param string|string[] $contents
* @param array{
* status?: string,
* id?: string
* } $context
*/
protected function writeToStream(string $filename, string|iterable $contents, string $reason = 'Unable to write', array $context = []): int
{
protected function writeToStream(
string $filename,
string|iterable $contents,
string $reason = 'Unable to write',
array $context = []
): int {
$resource = fopen($filename, 'w');
if (!$resource) {
$message = sprintf('%s to file "%s"', $reason, realpath($filename));
Expand All @@ -70,7 +75,11 @@ protected function writeToStream(string $filename, string|iterable $contents, st
$stream->write($contents, true, OutputInterface::OUTPUT_RAW);
fclose($stream->getStream());
$this->logger->debug(
sprintf('%s written to "%s"', $resource, str_starts_with($filename, 'php://') ? $filename : realpath($filename)),
sprintf(
'Resource id #%d written to "%s"',
get_resource_id($resource),
str_starts_with($filename, 'php://') ? $filename : realpath($filename)
),
$context
);
return 1;
Expand Down
2 changes: 1 addition & 1 deletion src/Pipeline/CompileStage.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __invoke(array $payload): array
{
$context = ['status' => Logger::STATUS_RUNNING, 'id' => $payload['pid']];

$configurationFile = $payload['outputConf'] ?? $payload['config'];
$configurationFile = $payload['outputConf'] ?? $payload['configurationFile'];

$process = $this->createBoxProcess($configurationFile);
$process->run();
Expand Down
6 changes: 4 additions & 2 deletions src/Pipeline/InterruptibleTimedProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function process($payload, callable ...$stages)
$stopwatch = new Stopwatch();

foreach ($stages as $stage) {
$name = $stage::class;
$name = $stage::class; // @phpstan-ignore classConstant.nonObject
$stopwatch->start($name);
$pid = $payload['pid'] = uniqid();

Expand All @@ -66,13 +66,15 @@ public function process($payload, callable ...$stages)
$isSuccessful = false;
} finally {
if (!$isSuccessful) {
// @phpstan-ignore-next-line variable.undefined
$message = sprintf('The stage "%s" has failed : %s', $name, $exception->getMessage());
}
// @phpstan-ignore-next-line variable.undefined
$this->logger->log($level, $message, ['status' => Logger::STATUS_STOPPED, 'id' => $pid, 'error' => !$isSuccessful]);

if (!$isSuccessful) {
// circuit breaker or critical conditions lead to abort the workflow
throw $exception;
throw $exception; // @phpstan-ignore variable.undefined
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/Pipeline/StageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ interface StageInterface
public const COMPILE_STAGE = 'compile';
public const STDOUT = 'php://stdout';

/**
* @param array{pid: string} $context
*/
public static function create(IO $io, Command $command, LoggerInterface $logger, array $context): static;

/**
* @param array{
* pid: string,
* configuration: Configuration,
* ansiSupport: bool,
* immutableCopy: bool,
Expand All @@ -47,6 +51,7 @@ public static function create(IO $io, Command $command, LoggerInterface $logger,
* configurationFile: string|null
* } $payload
* @return array{
* pid: string,
* configuration: Configuration,
* ansiSupport: bool,
* immutableCopy: bool,
Expand Down

0 comments on commit 75b81b0

Please sign in to comment.