diff --git a/Application.php b/Application.php index 07cc6d674..9df983cc8 100644 --- a/Application.php +++ b/Application.php @@ -137,7 +137,7 @@ public function setSignalsToDispatchEvent(int ...$signalsToDispatchEvent): void * * @throws \Exception When running fails. Bypass this when {@link setCatchExceptions()}. */ - public function run(InputInterface $input = null, OutputInterface $output = null): int + public function run(?InputInterface $input = null, ?OutputInterface $output = null): int { if (\function_exists('putenv')) { @putenv('LINES='.$this->terminal->getHeight()); @@ -762,7 +762,7 @@ public function find(string $name): Command * * @return Command[] */ - public function all(string $namespace = null): array + public function all(?string $namespace = null): array { $this->init(); @@ -1130,7 +1130,7 @@ private function getAbbreviationSuggestions(array $abbrevs): string * * This method is not part of public API and should not be used directly. */ - public function extractNamespace(string $name, int $limit = null): string + public function extractNamespace(string $name, ?int $limit = null): string { $parts = explode(':', $name, -1); diff --git a/CI/GithubActionReporter.php b/CI/GithubActionReporter.php index 7e5565469..2cae6fd8b 100644 --- a/CI/GithubActionReporter.php +++ b/CI/GithubActionReporter.php @@ -57,7 +57,7 @@ public static function isGithubActionEnvironment(): bool * * @see https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-an-error-message */ - public function error(string $message, string $file = null, int $line = null, int $col = null): void + public function error(string $message, ?string $file = null, ?int $line = null, ?int $col = null): void { $this->log('error', $message, $file, $line, $col); } @@ -67,7 +67,7 @@ public function error(string $message, string $file = null, int $line = null, in * * @see https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-a-warning-message */ - public function warning(string $message, string $file = null, int $line = null, int $col = null): void + public function warning(string $message, ?string $file = null, ?int $line = null, ?int $col = null): void { $this->log('warning', $message, $file, $line, $col); } @@ -77,12 +77,12 @@ public function warning(string $message, string $file = null, int $line = null, * * @see https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-a-debug-message */ - public function debug(string $message, string $file = null, int $line = null, int $col = null): void + public function debug(string $message, ?string $file = null, ?int $line = null, ?int $col = null): void { $this->log('debug', $message, $file, $line, $col); } - private function log(string $type, string $message, string $file = null, int $line = null, int $col = null): void + private function log(string $type, string $message, ?string $file = null, ?int $line = null, ?int $col = null): void { // Some values must be encoded. $message = strtr($message, self::ESCAPED_DATA); diff --git a/Command/Command.php b/Command/Command.php index c49891777..07b56ec16 100644 --- a/Command/Command.php +++ b/Command/Command.php @@ -77,7 +77,7 @@ public static function getDefaultDescription(): ?string * * @throws LogicException When the command name is empty */ - public function __construct(string $name = null) + public function __construct(?string $name = null) { $this->definition = new InputDefinition(); @@ -413,7 +413,7 @@ public function getNativeDefinition(): InputDefinition * * @throws InvalidArgumentException When argument mode is not valid */ - public function addArgument(string $name, int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = []): static + public function addArgument(string $name, ?int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = []): static { $this->definition->addArgument(new InputArgument($name, $mode, $description, $default, $suggestedValues)); $this->fullDefinition?->addArgument(new InputArgument($name, $mode, $description, $default, $suggestedValues)); @@ -433,7 +433,7 @@ public function addArgument(string $name, int $mode = null, string $description * * @throws InvalidArgumentException If option mode is invalid or incompatible */ - public function addOption(string $name, string|array $shortcut = null, int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = []): static + public function addOption(string $name, string|array|null $shortcut = null, ?int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = []): static { $this->definition->addOption(new InputOption($name, $shortcut, $mode, $description, $default, $suggestedValues)); $this->fullDefinition?->addOption(new InputOption($name, $shortcut, $mode, $description, $default, $suggestedValues)); diff --git a/Command/LazyCommand.php b/Command/LazyCommand.php index 7279724a6..d2fe8a1b6 100644 --- a/Command/LazyCommand.php +++ b/Command/LazyCommand.php @@ -113,7 +113,7 @@ public function getNativeDefinition(): InputDefinition /** * @param array|\Closure(CompletionInput,CompletionSuggestions):list $suggestedValues The values used for input completion */ - public function addArgument(string $name, int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = []): static + public function addArgument(string $name, ?int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = []): static { $this->getCommand()->addArgument($name, $mode, $description, $default, $suggestedValues); @@ -123,7 +123,7 @@ public function addArgument(string $name, int $mode = null, string $description /** * @param array|\Closure(CompletionInput,CompletionSuggestions):list $suggestedValues The values used for input completion */ - public function addOption(string $name, string|array $shortcut = null, int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = []): static + public function addOption(string $name, string|array|null $shortcut = null, ?int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = []): static { $this->getCommand()->addOption($name, $shortcut, $mode, $description, $default, $suggestedValues); diff --git a/Command/LockableTrait.php b/Command/LockableTrait.php index c1006a65c..cd7548f02 100644 --- a/Command/LockableTrait.php +++ b/Command/LockableTrait.php @@ -29,7 +29,7 @@ trait LockableTrait /** * Locks a command. */ - private function lock(string $name = null, bool $blocking = false): bool + private function lock(?string $name = null, bool $blocking = false): bool { if (!class_exists(SemaphoreStore::class)) { throw new LogicException('To enable the locking feature you must install the symfony/lock component. Try running "composer require symfony/lock".'); diff --git a/Command/TraceableCommand.php b/Command/TraceableCommand.php index d8c46b7fa..9ffb68da3 100644 --- a/Command/TraceableCommand.php +++ b/Command/TraceableCommand.php @@ -134,7 +134,7 @@ public function ignoreValidationErrors(): void parent::ignoreValidationErrors(); } - public function setApplication(Application $application = null): void + public function setApplication(?Application $application = null): void { $this->command->setApplication($application); } @@ -209,14 +209,14 @@ public function getNativeDefinition(): InputDefinition return $this->command->getNativeDefinition(); } - public function addArgument(string $name, int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = []): static + public function addArgument(string $name, ?int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = []): static { $this->command->addArgument($name, $mode, $description, $default, $suggestedValues); return $this; } - public function addOption(string $name, string|array $shortcut = null, int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = []): static + public function addOption(string $name, string|array|null $shortcut = null, ?int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = []): static { $this->command->addOption($name, $shortcut, $mode, $description, $default, $suggestedValues); diff --git a/DataCollector/CommandDataCollector.php b/DataCollector/CommandDataCollector.php index 16a0eadf4..45138c7dc 100644 --- a/DataCollector/CommandDataCollector.php +++ b/DataCollector/CommandDataCollector.php @@ -27,7 +27,7 @@ */ final class CommandDataCollector extends DataCollector { - public function collect(Request $request, Response $response, \Throwable $exception = null): void + public function collect(Request $request, Response $response, ?\Throwable $exception = null): void { if (!$request instanceof CliRequest) { return; diff --git a/Descriptor/ApplicationDescription.php b/Descriptor/ApplicationDescription.php index f8ed18045..ef9e8a63b 100644 --- a/Descriptor/ApplicationDescription.php +++ b/Descriptor/ApplicationDescription.php @@ -39,7 +39,7 @@ class ApplicationDescription */ private array $aliases = []; - public function __construct(Application $application, string $namespace = null, bool $showHidden = false) + public function __construct(Application $application, ?string $namespace = null, bool $showHidden = false) { $this->application = $application; $this->namespace = $namespace; diff --git a/Descriptor/XmlDescriptor.php b/Descriptor/XmlDescriptor.php index 72580fd98..866c71856 100644 --- a/Descriptor/XmlDescriptor.php +++ b/Descriptor/XmlDescriptor.php @@ -79,7 +79,7 @@ public function getCommandDocument(Command $command, bool $short = false): \DOMD return $dom; } - public function getApplicationDocument(Application $application, string $namespace = null, bool $short = false): \DOMDocument + public function getApplicationDocument(Application $application, ?string $namespace = null, bool $short = false): \DOMDocument { $dom = new \DOMDocument('1.0', 'UTF-8'); $dom->appendChild($rootXml = $dom->createElement('symfony')); diff --git a/Event/ConsoleErrorEvent.php b/Event/ConsoleErrorEvent.php index d4a691216..7be2ff83e 100644 --- a/Event/ConsoleErrorEvent.php +++ b/Event/ConsoleErrorEvent.php @@ -25,7 +25,7 @@ final class ConsoleErrorEvent extends ConsoleEvent private \Throwable $error; private int $exitCode; - public function __construct(InputInterface $input, OutputInterface $output, \Throwable $error, Command $command = null) + public function __construct(InputInterface $input, OutputInterface $output, \Throwable $error, ?Command $command = null) { parent::__construct($command, $input, $output); diff --git a/EventListener/ErrorListener.php b/EventListener/ErrorListener.php index 5c38e8ef8..1e41e7594 100644 --- a/EventListener/ErrorListener.php +++ b/EventListener/ErrorListener.php @@ -26,7 +26,7 @@ class ErrorListener implements EventSubscriberInterface { private ?LoggerInterface $logger; - public function __construct(LoggerInterface $logger = null) + public function __construct(?LoggerInterface $logger = null) { $this->logger = $logger; } diff --git a/Exception/CommandNotFoundException.php b/Exception/CommandNotFoundException.php index 1e9f1c793..541b32b23 100644 --- a/Exception/CommandNotFoundException.php +++ b/Exception/CommandNotFoundException.php @@ -26,7 +26,7 @@ class CommandNotFoundException extends \InvalidArgumentException implements Exce * @param int $code Exception code * @param \Throwable|null $previous Previous exception used for the exception chaining */ - public function __construct(string $message, array $alternatives = [], int $code = 0, \Throwable $previous = null) + public function __construct(string $message, array $alternatives = [], int $code = 0, ?\Throwable $previous = null) { parent::__construct($message, $code, $previous); diff --git a/Formatter/OutputFormatterStyle.php b/Formatter/OutputFormatterStyle.php index 4582ccd05..20a65b517 100644 --- a/Formatter/OutputFormatterStyle.php +++ b/Formatter/OutputFormatterStyle.php @@ -33,7 +33,7 @@ class OutputFormatterStyle implements OutputFormatterStyleInterface * @param string|null $foreground The style foreground color name * @param string|null $background The style background color name */ - public function __construct(string $foreground = null, string $background = null, array $options = []) + public function __construct(?string $foreground = null, ?string $background = null, array $options = []) { $this->color = new Color($this->foreground = $foreground ?: '', $this->background = $background ?: '', $this->options = $options); } diff --git a/Formatter/OutputFormatterStyleStack.php b/Formatter/OutputFormatterStyleStack.php index c3726a35d..4985213ab 100644 --- a/Formatter/OutputFormatterStyleStack.php +++ b/Formatter/OutputFormatterStyleStack.php @@ -26,7 +26,7 @@ class OutputFormatterStyleStack implements ResetInterface private OutputFormatterStyleInterface $emptyStyle; - public function __construct(OutputFormatterStyleInterface $emptyStyle = null) + public function __construct(?OutputFormatterStyleInterface $emptyStyle = null) { $this->emptyStyle = $emptyStyle ?? new OutputFormatterStyle(); $this->reset(); @@ -53,7 +53,7 @@ public function push(OutputFormatterStyleInterface $style): void * * @throws InvalidArgumentException When style tags incorrectly nested */ - public function pop(OutputFormatterStyleInterface $style = null): OutputFormatterStyleInterface + public function pop(?OutputFormatterStyleInterface $style = null): OutputFormatterStyleInterface { if (!$this->styles) { return $this->emptyStyle; diff --git a/Helper/Dumper.php b/Helper/Dumper.php index 8c6a94d51..a3b8e3952 100644 --- a/Helper/Dumper.php +++ b/Helper/Dumper.php @@ -26,7 +26,7 @@ final class Dumper private ?ClonerInterface $cloner; private \Closure $handler; - public function __construct(OutputInterface $output, CliDumper $dumper = null, ClonerInterface $cloner = null) + public function __construct(OutputInterface $output, ?CliDumper $dumper = null, ?ClonerInterface $cloner = null) { $this->output = $output; $this->dumper = $dumper; diff --git a/Helper/Helper.php b/Helper/Helper.php index afb20ad50..de090063a 100644 --- a/Helper/Helper.php +++ b/Helper/Helper.php @@ -74,7 +74,7 @@ public static function length(?string $string): int /** * Returns the subset of a string, using mb_substr if it is available. */ - public static function substr(?string $string, int $from, int $length = null): string + public static function substr(?string $string, int $from, ?int $length = null): string { $string ??= ''; diff --git a/Helper/HelperSet.php b/Helper/HelperSet.php index 42153b68d..30df9f958 100644 --- a/Helper/HelperSet.php +++ b/Helper/HelperSet.php @@ -35,7 +35,7 @@ public function __construct(array $helpers = []) } } - public function set(HelperInterface $helper, string $alias = null): void + public function set(HelperInterface $helper, ?string $alias = null): void { $this->helpers[$helper->getName()] = $helper; if (null !== $alias) { diff --git a/Helper/ProcessHelper.php b/Helper/ProcessHelper.php index 26d35a1a8..3ef6f71f7 100644 --- a/Helper/ProcessHelper.php +++ b/Helper/ProcessHelper.php @@ -32,7 +32,7 @@ class ProcessHelper extends Helper * @param callable|null $callback A PHP callback to run whenever there is some * output available on STDOUT or STDERR */ - public function run(OutputInterface $output, array|Process $cmd, string $error = null, callable $callback = null, int $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE): Process + public function run(OutputInterface $output, array|Process $cmd, ?string $error = null, ?callable $callback = null, int $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE): Process { if (!class_exists(Process::class)) { throw new \LogicException('The ProcessHelper cannot be run as the Process component is not installed. Try running "compose require symfony/process".'); @@ -94,7 +94,7 @@ public function run(OutputInterface $output, array|Process $cmd, string $error = * * @see run() */ - public function mustRun(OutputInterface $output, array|Process $cmd, string $error = null, callable $callback = null): Process + public function mustRun(OutputInterface $output, array|Process $cmd, ?string $error = null, ?callable $callback = null): Process { $process = $this->run($output, $cmd, $error, $callback); @@ -108,7 +108,7 @@ public function mustRun(OutputInterface $output, array|Process $cmd, string $err /** * Wraps a Process callback to add debugging output. */ - public function wrapCallback(OutputInterface $output, Process $process, callable $callback = null): callable + public function wrapCallback(OutputInterface $output, Process $process, ?callable $callback = null): callable { if ($output instanceof ConsoleOutputInterface) { $output = $output->getErrorOutput(); diff --git a/Helper/ProgressBar.php b/Helper/ProgressBar.php index 64389c4a2..f4eec051c 100644 --- a/Helper/ProgressBar.php +++ b/Helper/ProgressBar.php @@ -313,7 +313,7 @@ public function maxSecondsBetweenRedraws(float $seconds): void * * @return iterable */ - public function iterate(iterable $iterable, int $max = null): iterable + public function iterate(iterable $iterable, ?int $max = null): iterable { $this->start($max ?? (is_countable($iterable) ? \count($iterable) : 0)); @@ -332,7 +332,7 @@ public function iterate(iterable $iterable, int $max = null): iterable * @param int|null $max Number of steps to complete the bar (0 if indeterminate), null to leave unchanged * @param int $startAt The starting point of the bar (useful e.g. when resuming a previously started bar) */ - public function start(int $max = null, int $startAt = 0): void + public function start(?int $max = null, int $startAt = 0): void { $this->startTime = time(); $this->step = $startAt; diff --git a/Helper/ProgressIndicator.php b/Helper/ProgressIndicator.php index 8865ecc34..2ffac2ec9 100644 --- a/Helper/ProgressIndicator.php +++ b/Helper/ProgressIndicator.php @@ -50,7 +50,7 @@ class ProgressIndicator * @param int $indicatorChangeInterval Change interval in milliseconds * @param array|null $indicatorValues Animated indicator characters */ - public function __construct(OutputInterface $output, string $format = null, int $indicatorChangeInterval = 100, array $indicatorValues = null) + public function __construct(OutputInterface $output, ?string $format = null, int $indicatorChangeInterval = 100, ?array $indicatorValues = null) { $this->output = $output; diff --git a/Helper/Table.php b/Helper/Table.php index fe2ac87c1..e71e83ac3 100644 --- a/Helper/Table.php +++ b/Helper/Table.php @@ -462,7 +462,7 @@ public function render(): void * * +-----+-----------+-------+ */ - private function renderRowSeparator(int $type = self::SEPARATOR_MID, string $title = null, string $titleFormat = null): void + private function renderRowSeparator(int $type = self::SEPARATOR_MID, ?string $title = null, ?string $titleFormat = null): void { if (!$count = $this->numberOfColumns) { return; @@ -527,7 +527,7 @@ private function renderColumnSeparator(int $type = self::BORDER_OUTSIDE): string * * | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens | */ - private function renderRow(array $row, string $cellFormat, string $firstCellFormat = null): void + private function renderRow(array $row, string $cellFormat, ?string $firstCellFormat = null): void { $rowContent = $this->renderColumnSeparator(self::BORDER_OUTSIDE); $columns = $this->getRowColumns($row); diff --git a/Helper/TableStyle.php b/Helper/TableStyle.php index bbad98e73..be956c109 100644 --- a/Helper/TableStyle.php +++ b/Helper/TableStyle.php @@ -88,7 +88,7 @@ public function getPaddingChar(): string * * @return $this */ - public function setHorizontalBorderChars(string $outside, string $inside = null): static + public function setHorizontalBorderChars(string $outside, ?string $inside = null): static { $this->horizontalOutsideBorderChar = $outside; $this->horizontalInsideBorderChar = $inside ?? $outside; @@ -113,7 +113,7 @@ public function setHorizontalBorderChars(string $outside, string $inside = null) * * @return $this */ - public function setVerticalBorderChars(string $outside, string $inside = null): static + public function setVerticalBorderChars(string $outside, ?string $inside = null): static { $this->verticalOutsideBorderChar = $outside; $this->verticalInsideBorderChar = $inside ?? $outside; @@ -167,7 +167,7 @@ public function getBorderChars(): array * * @return $this */ - public function setCrossingChars(string $cross, string $topLeft, string $topMid, string $topRight, string $midRight, string $bottomRight, string $bottomMid, string $bottomLeft, string $midLeft, string $topLeftBottom = null, string $topMidBottom = null, string $topRightBottom = null): static + public function setCrossingChars(string $cross, string $topLeft, string $topMid, string $topRight, string $midRight, string $bottomRight, string $bottomMid, string $bottomLeft, string $midLeft, ?string $topLeftBottom = null, ?string $topMidBottom = null, ?string $topRightBottom = null): static { $this->crossingChar = $cross; $this->crossingTopLeftChar = $topLeft; diff --git a/Input/ArgvInput.php b/Input/ArgvInput.php index 9ae2f54f5..8621d62cf 100644 --- a/Input/ArgvInput.php +++ b/Input/ArgvInput.php @@ -43,7 +43,7 @@ class ArgvInput extends Input private array $tokens; private array $parsed; - public function __construct(array $argv = null, InputDefinition $definition = null) + public function __construct(?array $argv = null, ?InputDefinition $definition = null) { $argv ??= $_SERVER['argv'] ?? []; diff --git a/Input/ArrayInput.php b/Input/ArrayInput.php index 03b200b13..9f1fdafc7 100644 --- a/Input/ArrayInput.php +++ b/Input/ArrayInput.php @@ -27,7 +27,7 @@ class ArrayInput extends Input { private array $parameters; - public function __construct(array $parameters, InputDefinition $definition = null) + public function __construct(array $parameters, ?InputDefinition $definition = null) { $this->parameters = $parameters; diff --git a/Input/Input.php b/Input/Input.php index 6a9248b7a..5a8b9a27a 100644 --- a/Input/Input.php +++ b/Input/Input.php @@ -34,7 +34,7 @@ abstract class Input implements InputInterface, StreamableInputInterface protected array $arguments = []; protected bool $interactive = true; - public function __construct(InputDefinition $definition = null) + public function __construct(?InputDefinition $definition = null) { if (null === $definition) { $this->definition = new InputDefinition(); diff --git a/Input/InputArgument.php b/Input/InputArgument.php index 642ae6600..bcc464c96 100644 --- a/Input/InputArgument.php +++ b/Input/InputArgument.php @@ -44,7 +44,7 @@ class InputArgument * * @throws InvalidArgumentException When argument mode is not valid */ - public function __construct(string $name, int $mode = null, string $description = '', string|bool|int|float|array $default = null, \Closure|array $suggestedValues = []) + public function __construct(string $name, ?int $mode = null, string $description = '', string|bool|int|float|array|null $default = null, \Closure|array $suggestedValues = []) { if (null === $mode) { $mode = self::OPTIONAL; diff --git a/Input/InputOption.php b/Input/InputOption.php index f8e9b0dd6..cc2115d0e 100644 --- a/Input/InputOption.php +++ b/Input/InputOption.php @@ -65,7 +65,7 @@ class InputOption * * @throws InvalidArgumentException If option mode is invalid or incompatible */ - public function __construct(string $name, string|array $shortcut = null, int $mode = null, string $description = '', string|bool|int|float|array $default = null, array|\Closure $suggestedValues = []) + public function __construct(string $name, string|array|null $shortcut = null, ?int $mode = null, string $description = '', string|bool|int|float|array|null $default = null, array|\Closure $suggestedValues = []) { if (str_starts_with($name, '--')) { $name = substr($name, 2); @@ -75,7 +75,7 @@ public function __construct(string $name, string|array $shortcut = null, int $mo throw new InvalidArgumentException('An option name cannot be empty.'); } - if (empty($shortcut)) { + if ('' === $shortcut || [] === $shortcut) { $shortcut = null; } @@ -84,10 +84,10 @@ public function __construct(string $name, string|array $shortcut = null, int $mo $shortcut = implode('|', $shortcut); } $shortcuts = preg_split('{(\|)-?}', ltrim($shortcut, '-')); - $shortcuts = array_filter($shortcuts); + $shortcuts = array_filter($shortcuts, 'strlen'); $shortcut = implode('|', $shortcuts); - if (empty($shortcut)) { + if ('' === $shortcut) { throw new InvalidArgumentException('An option shortcut cannot be empty.'); } } diff --git a/Output/ConsoleOutput.php b/Output/ConsoleOutput.php index f9e6c7710..2ad3dbcf3 100644 --- a/Output/ConsoleOutput.php +++ b/Output/ConsoleOutput.php @@ -37,7 +37,7 @@ class ConsoleOutput extends StreamOutput implements ConsoleOutputInterface * @param bool|null $decorated Whether to decorate messages (null for auto-guessing) * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter) */ - public function __construct(int $verbosity = self::VERBOSITY_NORMAL, bool $decorated = null, OutputFormatterInterface $formatter = null) + public function __construct(int $verbosity = self::VERBOSITY_NORMAL, ?bool $decorated = null, ?OutputFormatterInterface $formatter = null) { parent::__construct($this->openOutputStream(), $verbosity, $decorated, $formatter); diff --git a/Output/ConsoleSectionOutput.php b/Output/ConsoleSectionOutput.php index d5b5aff76..ded97c70e 100644 --- a/Output/ConsoleSectionOutput.php +++ b/Output/ConsoleSectionOutput.php @@ -61,7 +61,7 @@ public function setMaxHeight(int $maxHeight): void * * @param int $lines Number of lines to clear. If null, then the entire output of this section is cleared */ - public function clear(int $lines = null): void + public function clear(?int $lines = null): void { if (empty($this->content) || !$this->isDecorated()) { return; diff --git a/Output/Output.php b/Output/Output.php index fe8564bb9..2bb105748 100644 --- a/Output/Output.php +++ b/Output/Output.php @@ -37,7 +37,7 @@ abstract class Output implements OutputInterface * @param bool $decorated Whether to decorate messages * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter) */ - public function __construct(?int $verbosity = self::VERBOSITY_NORMAL, bool $decorated = false, OutputFormatterInterface $formatter = null) + public function __construct(?int $verbosity = self::VERBOSITY_NORMAL, bool $decorated = false, ?OutputFormatterInterface $formatter = null) { $this->verbosity = $verbosity ?? self::VERBOSITY_NORMAL; $this->formatter = $formatter ?? new OutputFormatter(); diff --git a/Output/StreamOutput.php b/Output/StreamOutput.php index 55e403d33..304aa1e5d 100644 --- a/Output/StreamOutput.php +++ b/Output/StreamOutput.php @@ -40,7 +40,7 @@ class StreamOutput extends Output * * @throws InvalidArgumentException When first argument is not a real stream */ - public function __construct($stream, int $verbosity = self::VERBOSITY_NORMAL, bool $decorated = null, OutputFormatterInterface $formatter = null) + public function __construct($stream, int $verbosity = self::VERBOSITY_NORMAL, ?bool $decorated = null, ?OutputFormatterInterface $formatter = null) { if (!\is_resource($stream) || 'stream' !== get_resource_type($stream)) { throw new InvalidArgumentException('The StreamOutput class needs a stream as its first argument.'); @@ -94,6 +94,10 @@ protected function hasColorSupport(): bool return false; } + if (!$this->isTty()) { + return false; + } + if (\DIRECTORY_SEPARATOR === '\\' && \function_exists('sapi_windows_vt100_support') && @sapi_windows_vt100_support($this->stream) @@ -104,7 +108,36 @@ protected function hasColorSupport(): bool return 'Hyper' === getenv('TERM_PROGRAM') || false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI') - || str_starts_with((string) getenv('TERM'), 'xterm') - || stream_isatty($this->stream); + || str_starts_with((string) getenv('TERM'), 'xterm'); + } + + /** + * Checks if the stream is a TTY, i.e; whether the output stream is connected to a terminal. + * + * Reference: Composer\Util\Platform::isTty + * https://github.com/composer/composer + */ + private function isTty(): bool + { + // Detect msysgit/mingw and assume this is a tty because detection + // does not work correctly, see https://github.com/composer/composer/issues/9690 + if (\in_array(strtoupper((string) getenv('MSYSTEM')), ['MINGW32', 'MINGW64'], true)) { + return true; + } + + // Modern cross-platform function, includes the fstat fallback so if it is present we trust it + if (\function_exists('stream_isatty')) { + return stream_isatty($this->stream); + } + + // Only trusting this if it is positive, otherwise prefer fstat fallback. + if (\function_exists('posix_isatty') && posix_isatty($this->stream)) { + return true; + } + + $stat = @fstat($this->stream); + + // Check if formatted mode is S_IFCHR + return $stat ? 0020000 === ($stat['mode'] & 0170000) : false; } } diff --git a/Output/TrimmedBufferOutput.php b/Output/TrimmedBufferOutput.php index 5655e7bc8..c1862a2bd 100644 --- a/Output/TrimmedBufferOutput.php +++ b/Output/TrimmedBufferOutput.php @@ -24,7 +24,7 @@ class TrimmedBufferOutput extends Output private int $maxLength; private string $buffer = ''; - public function __construct(int $maxLength, ?int $verbosity = self::VERBOSITY_NORMAL, bool $decorated = false, OutputFormatterInterface $formatter = null) + public function __construct(int $maxLength, ?int $verbosity = self::VERBOSITY_NORMAL, bool $decorated = false, ?OutputFormatterInterface $formatter = null) { if ($maxLength <= 0) { throw new InvalidArgumentException(sprintf('"%s()" expects a strictly positive maxLength. Got %d.', __METHOD__, $maxLength)); diff --git a/Question/Question.php b/Question/Question.php index c79683cd5..6afe8ea6b 100644 --- a/Question/Question.php +++ b/Question/Question.php @@ -36,7 +36,7 @@ class Question * @param string $question The question to ask to the user * @param string|bool|int|float|null $default The default answer to return if the user enters nothing */ - public function __construct(string $question, string|bool|int|float $default = null) + public function __construct(string $question, string|bool|int|float|null $default = null) { $this->question = $question; $this->default = $default; diff --git a/SingleCommandApplication.php b/SingleCommandApplication.php index 4f0b5ba3c..ff1c17247 100644 --- a/SingleCommandApplication.php +++ b/SingleCommandApplication.php @@ -46,7 +46,7 @@ public function setAutoExit(bool $autoExit): static return $this; } - public function run(InputInterface $input = null, OutputInterface $output = null): int + public function run(?InputInterface $input = null, ?OutputInterface $output = null): int { if ($this->running) { return parent::run($input, $output); diff --git a/Style/StyleInterface.php b/Style/StyleInterface.php index 869b16090..fcc5bc775 100644 --- a/Style/StyleInterface.php +++ b/Style/StyleInterface.php @@ -71,12 +71,12 @@ public function table(array $headers, array $rows): void; /** * Asks a question. */ - public function ask(string $question, string $default = null, callable $validator = null): mixed; + public function ask(string $question, ?string $default = null, ?callable $validator = null): mixed; /** * Asks a question with the user input hidden. */ - public function askHidden(string $question, callable $validator = null): mixed; + public function askHidden(string $question, ?callable $validator = null): mixed; /** * Asks for confirmation. diff --git a/Style/SymfonyStyle.php b/Style/SymfonyStyle.php index 0da5d6981..ca557d6c1 100644 --- a/Style/SymfonyStyle.php +++ b/Style/SymfonyStyle.php @@ -61,7 +61,7 @@ public function __construct(InputInterface $input, OutputInterface $output) /** * Formats a message as a block of text. */ - public function block(string|array $messages, string $type = null, string $style = null, string $prefix = ' ', bool $padding = false, bool $escape = true): void + public function block(string|array $messages, ?string $type = null, ?string $style = null, string $prefix = ' ', bool $padding = false, bool $escape = true): void { $messages = \is_array($messages) ? array_values($messages) : [$messages]; @@ -209,7 +209,7 @@ public function definitionList(string|array|TableSeparator ...$list): void $this->horizontalTable($headers, [$row]); } - public function ask(string $question, string $default = null, callable $validator = null): mixed + public function ask(string $question, ?string $default = null, ?callable $validator = null): mixed { $question = new Question($question, $default); $question->setValidator($validator); @@ -217,7 +217,7 @@ public function ask(string $question, string $default = null, callable $validato return $this->askQuestion($question); } - public function askHidden(string $question, callable $validator = null): mixed + public function askHidden(string $question, ?callable $validator = null): mixed { $question = new Question($question); @@ -287,7 +287,7 @@ public function createProgressBar(int $max = 0): ProgressBar * * @return iterable */ - public function progressIterate(iterable $iterable, int $max = null): iterable + public function progressIterate(iterable $iterable, ?int $max = null): iterable { yield from $this->createProgressBar()->iterate($iterable, $max); @@ -398,7 +398,7 @@ private function writeBuffer(string $message, bool $newLine, int $type): void $this->bufferedOutput->write($message, $newLine, $type); } - private function createBlock(iterable $messages, string $type = null, string $style = null, string $prefix = ' ', bool $padding = false, bool $escape = false): array + private function createBlock(iterable $messages, ?string $type = null, ?string $style = null, string $prefix = ' ', bool $padding = false, bool $escape = false): array { $indentLength = 0; $prefixLength = Helper::width(Helper::removeDecoration($this->getFormatter(), $prefix)); diff --git a/Tests/Formatter/OutputFormatterTest.php b/Tests/Formatter/OutputFormatterTest.php index 610522a7e..f62fa0889 100644 --- a/Tests/Formatter/OutputFormatterTest.php +++ b/Tests/Formatter/OutputFormatterTest.php @@ -162,7 +162,7 @@ public function testInlineStyle() /** * @dataProvider provideInlineStyleOptionsCases */ - public function testInlineStyleOptions(string $tag, string $expected = null, string $input = null, bool $truecolor = false) + public function testInlineStyleOptions(string $tag, ?string $expected = null, ?string $input = null, bool $truecolor = false) { if ($truecolor && 'truecolor' !== getenv('COLORTERM')) { $this->markTestSkipped('The terminal does not support true colors.'); diff --git a/Tests/Helper/HelperSetTest.php b/Tests/Helper/HelperSetTest.php index 9fbb9afca..389ee0ed3 100644 --- a/Tests/Helper/HelperSetTest.php +++ b/Tests/Helper/HelperSetTest.php @@ -87,7 +87,7 @@ public function testIteration() } } - private function getGenericMockHelper($name, HelperSet $helperset = null) + private function getGenericMockHelper($name, ?HelperSet $helperset = null) { $mock_helper = $this->createMock(HelperInterface::class); $mock_helper->expects($this->any()) diff --git a/Tests/Input/InputOptionTest.php b/Tests/Input/InputOptionTest.php index 74bf69586..e7a007a61 100644 --- a/Tests/Input/InputOptionTest.php +++ b/Tests/Input/InputOptionTest.php @@ -59,6 +59,20 @@ public function testShortcut() $this->assertEquals('f|ff|fff', $option->getShortcut(), '__construct() removes the leading - of the shortcuts'); $option = new InputOption('foo'); $this->assertNull($option->getShortcut(), '__construct() makes the shortcut null by default'); + $option = new InputOption('foo', ''); + $this->assertNull($option->getShortcut(), '__construct() makes the shortcut null when given an empty string'); + $option = new InputOption('foo', []); + $this->assertNull($option->getShortcut(), '__construct() makes the shortcut null when given an empty array'); + $option = new InputOption('foo', ['f', '', 'fff']); + $this->assertEquals('f|fff', $option->getShortcut(), '__construct() removes empty shortcuts'); + $option = new InputOption('foo', 'f||fff'); + $this->assertEquals('f|fff', $option->getShortcut(), '__construct() removes empty shortcuts'); + $option = new InputOption('foo', '0'); + $this->assertEquals('0', $option->getShortcut(), '-0 is an acceptable shortcut value'); + $option = new InputOption('foo', ['0', 'z']); + $this->assertEquals('0|z', $option->getShortcut(), '-0 is an acceptable shortcut value when embedded in an array'); + $option = new InputOption('foo', '0|z'); + $this->assertEquals('0|z', $option->getShortcut(), '-0 is an acceptable shortcut value when embedded in a string-list'); } public function testModes()