From 8341ae3d8b3bd3658a08dcbf40fff82542610510 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 10 May 2026 10:45:32 +0200 Subject: [PATCH] Remove requirement of longUrlWasProvided --- .../ShortUrl/Input/ShortUrlDataInput.php | 1 - module/Core/src/ShortUrl/Entity/ShortUrl.php | 2 +- .../src/ShortUrl/Model/ShortUrlEdition.php | 43 +++++++++---------- module/Core/src/ShortUrl/ShortUrlService.php | 2 +- .../test/ShortUrl/ShortUrlServiceTest.php | 1 - .../Action/ShortUrl/EditShortUrlAction.php | 1 - 6 files changed, 23 insertions(+), 27 deletions(-) diff --git a/module/CLI/src/Command/ShortUrl/Input/ShortUrlDataInput.php b/module/CLI/src/Command/ShortUrl/Input/ShortUrlDataInput.php index 2be3f6f4..dc52a3bf 100644 --- a/module/CLI/src/Command/ShortUrl/Input/ShortUrlDataInput.php +++ b/module/CLI/src/Command/ShortUrl/Input/ShortUrlDataInput.php @@ -57,7 +57,6 @@ final class ShortUrlDataInput return [ ...array_filter(get_object_vars($this), static fn (mixed $value) => $value !== null), 'longUrl' => $longUrl, - 'longUrlWasProvided' => $longUrl !== null, 'validSinceWasProvided' => $this->validSince !== null, 'validUntilWasProvided' => $this->validUntil !== null, 'maxVisitsWasProvided' => $this->maxVisits !== null, diff --git a/module/Core/src/ShortUrl/Entity/ShortUrl.php b/module/Core/src/ShortUrl/Entity/ShortUrl.php index 990fb7ed..2ee2e3dd 100644 --- a/module/Core/src/ShortUrl/Entity/ShortUrl.php +++ b/module/Core/src/ShortUrl/Entity/ShortUrl.php @@ -147,7 +147,7 @@ class ShortUrl extends AbstractEntity if ($shortUrlEdit->maxVisitsWasProvided) { $this->maxVisits = $shortUrlEdit->maxVisits; } - if ($shortUrlEdit->longUrlWasProvided()) { + if ($shortUrlEdit->longUrlWasProvided) { $this->longUrl = $shortUrlEdit->longUrl ?? $this->longUrl; } if ($shortUrlEdit->tagsWereProvided) { diff --git a/module/Core/src/ShortUrl/Model/ShortUrlEdition.php b/module/Core/src/ShortUrl/Model/ShortUrlEdition.php index b92c9703..af6bb230 100644 --- a/module/Core/src/ShortUrl/Model/ShortUrlEdition.php +++ b/module/Core/src/ShortUrl/Model/ShortUrlEdition.php @@ -13,35 +13,40 @@ use Shlinkio\Shlink\Core\ShortUrl\Helper\TitleResolutionModelInterface; use function Shlinkio\Shlink\Common\normalizeOptionalDate; -final readonly class ShortUrlEdition implements TitleResolutionModelInterface +final class ShortUrlEdition implements TitleResolutionModelInterface { public Chronos|null $validSince; public Chronos|null $validUntil; + // phpcs:disable PSR2.Classes.PropertyDeclaration.Multiple + public bool $longUrlWasProvided { + // phpcs:disable PSR2.Classes.PropertyDeclaration.ScopeMissing + get => $this->longUrl !== null; + } + /** * @param string[] $tags */ public function __construct( - private bool $longUrlWasProvided = false, #[LooseUriConverter] - public string|null $longUrl = null, - public bool $validSinceWasProvided = false, + readonly public string|null $longUrl = null, + readonly public bool $validSinceWasProvided = false, DateTimeInterface|string|null $validSince = null, - public bool $validUntilWasProvided = false, + readonly public bool $validUntilWasProvided = false, DateTimeInterface|string|null $validUntil = null, - public bool $maxVisitsWasProvided = false, - public int|null $maxVisits = null, - public bool $tagsWereProvided = false, + readonly public bool $maxVisitsWasProvided = false, + readonly public int|null $maxVisits = null, + readonly public bool $tagsWereProvided = false, #[TagsConverter] - public array $tags = [], - public bool $titleWasProvided = false, + readonly public array $tags = [], + readonly public bool $titleWasProvided = false, #[SubstringConverter(512)] - public string|null $title = null, - public bool $titleWasAutoResolved = false, - public bool $crawlableWasProvided = false, - public bool $crawlable = false, - public bool $forwardQueryWasProvided = false, - public bool $forwardQuery = true, + readonly public string|null $title = null, + readonly public bool $titleWasAutoResolved = false, + readonly public bool $crawlableWasProvided = false, + readonly public bool $crawlable = false, + readonly public bool $forwardQueryWasProvided = false, + readonly public bool $forwardQuery = true, ) { $this->validSince = normalizeOptionalDate($validSince); $this->validUntil = normalizeOptionalDate($validUntil); @@ -56,7 +61,6 @@ final readonly class ShortUrlEdition implements TitleResolutionModelInterface // ]); return new self( - longUrlWasProvided: $this->longUrlWasProvided, longUrl: $this->longUrl, validSinceWasProvided: $this->validSinceWasProvided, validSince: $this->validSince, @@ -76,11 +80,6 @@ final readonly class ShortUrlEdition implements TitleResolutionModelInterface ); } - public function longUrlWasProvided(): bool - { - return $this->longUrlWasProvided && $this->longUrl !== null; - } - public function hasTitle(): bool { return $this->titleWasProvided; diff --git a/module/Core/src/ShortUrl/ShortUrlService.php b/module/Core/src/ShortUrl/ShortUrlService.php index b2c7e92f..fcff9513 100644 --- a/module/Core/src/ShortUrl/ShortUrlService.php +++ b/module/Core/src/ShortUrl/ShortUrlService.php @@ -31,7 +31,7 @@ readonly class ShortUrlService implements ShortUrlServiceInterface ShortUrlEdition $shortUrlEdit, ApiKey|null $apiKey = null, ): ShortUrl { - if ($shortUrlEdit->longUrlWasProvided()) { + if ($shortUrlEdit->longUrlWasProvided) { $shortUrlEdit = $this->titleResolutionHelper->processTitle($shortUrlEdit); } diff --git a/module/Core/test/ShortUrl/ShortUrlServiceTest.php b/module/Core/test/ShortUrl/ShortUrlServiceTest.php index eb5d2c31..e187e8db 100644 --- a/module/Core/test/ShortUrl/ShortUrlServiceTest.php +++ b/module/Core/test/ShortUrl/ShortUrlServiceTest.php @@ -85,7 +85,6 @@ class ShortUrlServiceTest extends TestCase maxVisits: 5, ), null]; yield 'long URL and API key' => [new InvokedCount(1), new ShortUrlEdition( - longUrlWasProvided: true, longUrl: 'https://modifiedLongUrl', validSinceWasProvided: true, validSince: Chronos::parse('2017-01-01 00:00:00'), diff --git a/module/Rest/src/Action/ShortUrl/EditShortUrlAction.php b/module/Rest/src/Action/ShortUrl/EditShortUrlAction.php index a7f687c2..3e7b9e45 100644 --- a/module/Rest/src/Action/ShortUrl/EditShortUrlAction.php +++ b/module/Rest/src/Action/ShortUrl/EditShortUrlAction.php @@ -34,7 +34,6 @@ class EditShortUrlAction extends AbstractRestAction $body = (array) $request->getParsedBody(); $shortUrlEdit = $this->treeMapper->map(ShortUrlEdition::class, [ ...$body, - 'longUrlWasProvided' => array_key_exists('longUrl', $body), 'validSinceWasProvided' => array_key_exists('validSince', $body), 'validUntilWasProvided' => array_key_exists('validUntil', $body), 'maxVisitsWasProvided' => array_key_exists('maxVisits', $body),