Update to PHP coding standard 2.4.0

This commit is contained in:
Alejandro Celaya
2024-10-28 22:27:30 +01:00
parent 93a277a94d
commit 3f1d61e01e
192 changed files with 465 additions and 432 deletions

View File

@@ -26,7 +26,7 @@ class ReadEnvVarCommand extends Command
/** @var Closure(string $envVar): mixed */
private readonly Closure $loadEnvVar;
public function __construct(?Closure $loadEnvVar = null)
public function __construct(Closure|null $loadEnvVar = null)
{
$this->loadEnvVar = $loadEnvVar ?? static fn (string $envVar) => EnvVars::from($envVar)->loadFromEnv();
parent::__construct();

View File

@@ -74,7 +74,7 @@ class DomainRedirectsCommand extends Command
$domainAuthority = $input->getArgument('domain');
$domain = $this->domainService->findByAuthority($domainAuthority);
$ask = static function (string $message, ?string $current) use ($io): ?string {
$ask = static function (string $message, string|null $current) use ($io): string|null {
if ($current === null) {
return $io->ask(sprintf('%s (Leave empty for no redirect)', $message));
}

View File

@@ -22,7 +22,7 @@ class CreateShortUrlCommand extends Command
{
public const NAME = 'short-url:create';
private ?SymfonyStyle $io;
private SymfonyStyle|null $io;
private readonly ShortUrlDataInput $shortUrlDataInput;
public function __construct(

View File

@@ -210,7 +210,7 @@ class ListShortUrlsCommand extends Command
return $shortUrls;
}
private function processOrderBy(InputInterface $input): ?string
private function processOrderBy(InputInterface $input): string|null
{
$orderBy = $input->getOption('order-by');
if (empty($orderBy)) {
@@ -247,7 +247,7 @@ class ListShortUrlsCommand extends Command
$shortUrl->authorApiKey?->__toString() ?? '';
}
if ($input->getOption('show-api-key-name')) {
$columnsMap['API Key Name'] = static fn (array $_, ShortUrl $shortUrl): ?string =>
$columnsMap['API Key Name'] = static fn (array $_, ShortUrl $shortUrl): string|null =>
$shortUrl->authorApiKey?->name;
}

View File

@@ -20,7 +20,7 @@ class DownloadGeoLiteDbCommand extends Command
{
public const NAME = 'visit:download-db';
private ?ProgressBar $progressBar = null;
private ProgressBar|null $progressBar = null;
public function __construct(private GeolocationDbUpdaterInterface $dbUpdater)
{

View File

@@ -13,12 +13,12 @@ class GeolocationDbUpdateFailedException extends RuntimeException implements Exc
{
private bool $olderDbExists;
private function __construct(string $message, ?Throwable $previous = null)
private function __construct(string $message, Throwable|null $previous = null)
{
parent::__construct($message, previous: $previous);
}
public static function withOlderDb(?Throwable $prev = null): self
public static function withOlderDb(Throwable|null $prev = null): self
{
$e = new self(
'An error occurred while updating geolocation database, but an older DB is already present.',
@@ -29,7 +29,7 @@ class GeolocationDbUpdateFailedException extends RuntimeException implements Exc
return $e;
}
public static function withoutOlderDb(?Throwable $prev = null): self
public static function withoutOlderDb(Throwable|null $prev = null): self
{
$e = new self(
'An error occurred while updating geolocation database, and an older version could not be found.',

View File

@@ -40,8 +40,10 @@ class GeolocationDbUpdater implements GeolocationDbUpdaterInterface
/**
* @throws GeolocationDbUpdateFailedException
*/
public function checkDbUpdate(?callable $beforeDownload = null, ?callable $handleProgress = null): GeolocationResult
{
public function checkDbUpdate(
callable|null $beforeDownload = null,
callable|null $handleProgress = null,
): GeolocationResult {
if ($this->trackingOptions->disableTracking || $this->trackingOptions->disableIpTracking) {
return GeolocationResult::CHECK_SKIPPED;
}
@@ -59,7 +61,7 @@ class GeolocationDbUpdater implements GeolocationDbUpdaterInterface
/**
* @throws GeolocationDbUpdateFailedException
*/
private function downloadIfNeeded(?callable $beforeDownload, ?callable $handleProgress): GeolocationResult
private function downloadIfNeeded(callable|null $beforeDownload, callable|null $handleProgress): GeolocationResult
{
if (! $this->dbUpdater->databaseFileExists()) {
return $this->downloadNewDb(false, $beforeDownload, $handleProgress);
@@ -105,8 +107,8 @@ class GeolocationDbUpdater implements GeolocationDbUpdaterInterface
*/
private function downloadNewDb(
bool $olderDbExists,
?callable $beforeDownload,
?callable $handleProgress,
callable|null $beforeDownload,
callable|null $handleProgress,
): GeolocationResult {
if ($beforeDownload !== null) {
$beforeDownload($olderDbExists);
@@ -124,7 +126,7 @@ class GeolocationDbUpdater implements GeolocationDbUpdaterInterface
}
}
private function wrapHandleProgressCallback(?callable $handleProgress, bool $olderDbExists): ?callable
private function wrapHandleProgressCallback(callable|null $handleProgress, bool $olderDbExists): callable|null
{
if ($handleProgress === null) {
return null;

View File

@@ -12,7 +12,7 @@ interface GeolocationDbUpdaterInterface
* @throws GeolocationDbUpdateFailedException
*/
public function checkDbUpdate(
?callable $beforeDownload = null,
?callable $handleProgress = null,
callable|null $beforeDownload = null,
callable|null $handleProgress = null,
): GeolocationResult;
}

View File

@@ -21,7 +21,7 @@ readonly class DateOption
$command->addOption($name, $shortcut, InputOption::VALUE_REQUIRED, $description);
}
public function get(InputInterface $input, OutputInterface $output): ?Chronos
public function get(InputInterface $input, OutputInterface $output): Chronos|null
{
$value = $input->getOption($this->name);
if (empty($value) || ! is_string($value)) {

View File

@@ -23,7 +23,7 @@ readonly final class EndDateOption
));
}
public function get(InputInterface $input, OutputInterface $output): ?Chronos
public function get(InputInterface $input, OutputInterface $output): Chronos|null
{
return $this->dateOption->get($input, $output);
}

View File

@@ -18,7 +18,7 @@ enum ShortUrlDataOption: string
case CRAWLABLE = 'crawlable';
case NO_FORWARD_QUERY = 'no-forward-query';
public function shortcut(): ?string
public function shortcut(): string|null
{
return match ($this) {
self::TAGS => 't',

View File

@@ -19,7 +19,7 @@ readonly final class ShortUrlIdentifierInput
->addOption('domain', 'd', InputOption::VALUE_REQUIRED, $domainDesc);
}
public function shortCode(InputInterface $input): ?string
public function shortCode(InputInterface $input): string|null
{
return $input->getArgument('shortCode');
}

View File

@@ -23,7 +23,7 @@ readonly final class StartDateOption
));
}
public function get(InputInterface $input, OutputInterface $output): ?Chronos
public function get(InputInterface $input, OutputInterface $output): Chronos|null
{
return $this->dateOption->get($input, $output);
}

View File

@@ -33,7 +33,7 @@ use const STR_PAD_LEFT;
class RedirectRuleHandler implements RedirectRuleHandlerInterface
{
public function manageRules(StyleInterface $io, ShortUrl $shortUrl, array $rules): ?array
public function manageRules(StyleInterface $io, ShortUrl $shortUrl, array $rules): array|null
{
$amountOfRules = count($rules);
@@ -213,7 +213,7 @@ class RedirectRuleHandler implements RedirectRuleHandlerInterface
private function askMandatory(string $message, StyleInterface $io): string
{
return $io->ask($message, validator: function (?string $answer): string {
return $io->ask($message, validator: function (string|null $answer): string {
if ($answer === null) {
throw new InvalidArgumentException('The value is mandatory');
}
@@ -223,6 +223,6 @@ class RedirectRuleHandler implements RedirectRuleHandlerInterface
private function askOptional(string $message, StyleInterface $io): string
{
return $io->ask($message, validator: fn (?string $answer) => $answer === null ? '' : trim($answer));
return $io->ask($message, validator: fn (string|null $answer) => $answer === null ? '' : trim($answer));
}
}

View File

@@ -16,5 +16,5 @@ interface RedirectRuleHandlerInterface
* @param ShortUrlRedirectRule[] $rules
* @return ShortUrlRedirectRule[]|null - A new list of rules to save, or null if no changes should be saved
*/
public function manageRules(StyleInterface $io, ShortUrl $shortUrl, array $rules): ?array;
public function manageRules(StyleInterface $io, ShortUrl $shortUrl, array $rules): array|null;
}

View File

@@ -20,7 +20,7 @@ class ProcessRunner implements ProcessRunnerInterface
{
private Closure $createProcess;
public function __construct(private ProcessHelper $helper, ?callable $createProcess = null)
public function __construct(private ProcessHelper $helper, callable|null $createProcess = null)
{
$this->createProcess = $createProcess !== null
? $createProcess(...)

View File

@@ -34,8 +34,12 @@ final class ShlinkTable
return new self($baseTable);
}
public function render(array $headers, array $rows, ?string $footerTitle = null, ?string $headerTitle = null): void
{
public function render(
array $headers,
array $rows,
string|null $footerTitle = null,
string|null $headerTitle = null,
): void {
$style = Table::getStyleDefinition(self::DEFAULT_STYLE_NAME);
$style->setFooterTitleFormat(self::TABLE_TITLE_STYLE)
->setHeaderTitleFormat(self::TABLE_TITLE_STYLE);

View File

@@ -27,8 +27,11 @@ class InitialApiKeyCommandTest extends TestCase
}
#[Test, DataProvider('provideParams')]
public function initialKeyIsCreatedWithProvidedValue(?ApiKey $result, bool $verbose, string $expectedOutput): void
{
public function initialKeyIsCreatedWithProvidedValue(
ApiKey|null $result,
bool $verbose,
string $expectedOutput,
): void {
$this->apiKeyService->expects($this->once())->method('createInitial')->with('the_key')->willReturn($result);
$this->commandTester->execute(

View File

@@ -31,7 +31,7 @@ class DomainRedirectsCommandTest extends TestCase
}
#[Test, DataProvider('provideDomains')]
public function onlyPlainQuestionsAreAskedForNewDomainsAndDomainsWithNoRedirects(?Domain $domain): void
public function onlyPlainQuestionsAreAskedForNewDomainsAndDomainsWithNoRedirects(Domain|null $domain): void
{
$domainAuthority = 'my-domain.com';
$this->domainService->expects($this->once())->method('findByAuthority')->with($domainAuthority)->willReturn(

View File

@@ -104,7 +104,7 @@ class CreateShortUrlCommandTest extends TestCase
}
#[Test, DataProvider('provideDomains')]
public function properlyProcessesProvidedDomain(array $input, ?string $expectedDomain): void
public function properlyProcessesProvidedDomain(array $input, string|null $expectedDomain): void
{
$this->urlShortener->expects($this->once())->method('shorten')->with(
$this->callback(function (ShortUrlCreation $meta) use ($expectedDomain) {
@@ -128,8 +128,10 @@ class CreateShortUrlCommandTest extends TestCase
}
#[Test, DataProvider('provideFlags')]
public function urlValidationHasExpectedValueBasedOnProvidedFlags(array $options, ?bool $expectedCrawlable): void
{
public function urlValidationHasExpectedValueBasedOnProvidedFlags(
array $options,
bool|null $expectedCrawlable,
): void {
$shortUrl = ShortUrl::createFake();
$this->urlShortener->expects($this->once())->method('shorten')->with(
$this->callback(function (ShortUrlCreation $meta) use ($expectedCrawlable) {

View File

@@ -198,12 +198,12 @@ class ListShortUrlsCommandTest extends TestCase
#[Test, DataProvider('provideArgs')]
public function serviceIsInvokedWithProvidedArgs(
array $commandArgs,
?int $page,
?string $searchTerm,
int|null $page,
string|null $searchTerm,
array $tags,
string $tagsMode,
?string $startDate = null,
?string $endDate = null,
string|null $startDate = null,
string|null $endDate = null,
): void {
$this->shortUrlService->expects($this->once())->method('listShortUrls')->with(ShortUrlsParams::fromRawData([
'page' => $page,
@@ -260,7 +260,7 @@ class ListShortUrlsCommandTest extends TestCase
}
#[Test, DataProvider('provideOrderBy')]
public function orderByIsProperlyComputed(array $commandArgs, ?string $expectedOrderBy): void
public function orderByIsProperlyComputed(array $commandArgs, string|null $expectedOrderBy): void
{
$this->shortUrlService->expects($this->once())->method('listShortUrls')->with(ShortUrlsParams::fromRawData([
'orderBy' => $expectedOrderBy,

View File

@@ -15,7 +15,7 @@ use Throwable;
class GeolocationDbUpdateFailedExceptionTest extends TestCase
{
#[Test, DataProvider('providePrev')]
public function withOlderDbBuildsException(?Throwable $prev): void
public function withOlderDbBuildsException(Throwable|null $prev): void
{
$e = GeolocationDbUpdateFailedException::withOlderDb($prev);
@@ -29,7 +29,7 @@ class GeolocationDbUpdateFailedExceptionTest extends TestCase
}
#[Test, DataProvider('providePrev')]
public function withoutOlderDbBuildsException(?Throwable $prev): void
public function withoutOlderDbBuildsException(Throwable|null $prev): void
{
$e = GeolocationDbUpdateFailedException::withoutOlderDb($prev);

View File

@@ -180,7 +180,7 @@ class GeolocationDbUpdaterTest extends TestCase
yield 'both' => [new TrackingOptions(disableTracking: true, disableIpTracking: true)];
}
private function geolocationDbUpdater(?TrackingOptions $options = null): GeolocationDbUpdater
private function geolocationDbUpdater(TrackingOptions|null $options = null): GeolocationDbUpdater
{
$locker = $this->createMock(Lock\LockFactory::class);
$locker->method('createLock')->with($this->isType('string'))->willReturn($this->lock);

View File

@@ -56,7 +56,7 @@ class RedirectRuleHandlerTest extends TestCase
#[Test, DataProvider('provideExitActions')]
public function commentIsDisplayedWhenRulesListIsEmpty(
RedirectRuleHandlerAction $action,
?array $expectedResult,
array|null $expectedResult,
): void {
$this->io->expects($this->once())->method('choice')->willReturn($action->value);
$this->io->expects($this->once())->method('newLine');