mirror of
https://github.com/shlinkio/shlink.git
synced 2026-02-28 04:03:12 +08:00
Update to PHP coding standard 2.4.0
This commit is contained in:
@@ -14,11 +14,11 @@ use function array_key_exists;
|
||||
class DomainRedirectsRequest
|
||||
{
|
||||
private string $authority;
|
||||
private ?string $baseUrlRedirect = null;
|
||||
private string|null $baseUrlRedirect = null;
|
||||
private bool $baseUrlRedirectWasProvided = false;
|
||||
private ?string $regular404Redirect = null;
|
||||
private string|null $regular404Redirect = null;
|
||||
private bool $regular404RedirectWasProvided = false;
|
||||
private ?string $invalidShortUrlRedirect = null;
|
||||
private string|null $invalidShortUrlRedirect = null;
|
||||
private bool $invalidShortUrlRedirectWasProvided = false;
|
||||
|
||||
private function __construct()
|
||||
@@ -66,7 +66,7 @@ class DomainRedirectsRequest
|
||||
return $this->authority;
|
||||
}
|
||||
|
||||
public function toNotFoundRedirects(?NotFoundRedirectConfigInterface $defaults = null): NotFoundRedirects
|
||||
public function toNotFoundRedirects(NotFoundRedirectConfigInterface|null $defaults = null): NotFoundRedirects
|
||||
{
|
||||
return NotFoundRedirects::withRedirects(
|
||||
$this->baseUrlRedirectWasProvided ? $this->baseUrlRedirect : $defaults?->baseUrlRedirect(),
|
||||
|
||||
@@ -14,8 +14,8 @@ final class ApiKeyMeta
|
||||
*/
|
||||
private function __construct(
|
||||
public readonly string $key,
|
||||
public readonly ?string $name,
|
||||
public readonly ?Chronos $expirationDate,
|
||||
public readonly string|null $name,
|
||||
public readonly Chronos|null $expirationDate,
|
||||
public readonly iterable $roleDefinitions,
|
||||
) {
|
||||
}
|
||||
@@ -29,9 +29,9 @@ final class ApiKeyMeta
|
||||
* @param iterable<RoleDefinition> $roleDefinitions
|
||||
*/
|
||||
public static function fromParams(
|
||||
?string $key = null,
|
||||
?string $name = null,
|
||||
?Chronos $expirationDate = null,
|
||||
string|null $key = null,
|
||||
string|null $name = null,
|
||||
Chronos|null $expirationDate = null,
|
||||
iterable $roleDefinitions = [],
|
||||
): self {
|
||||
return new self(
|
||||
|
||||
@@ -17,10 +17,10 @@ class ApiKeyRepository extends EntitySpecificationRepository implements ApiKeyRe
|
||||
/**
|
||||
* Will create provided API key with admin permissions, only if there's no other API keys yet
|
||||
*/
|
||||
public function createInitialApiKey(string $apiKey): ?ApiKey
|
||||
public function createInitialApiKey(string $apiKey): ApiKey|null
|
||||
{
|
||||
$em = $this->getEntityManager();
|
||||
return $em->wrapInTransaction(function () use ($apiKey, $em): ?ApiKey {
|
||||
return $em->wrapInTransaction(function () use ($apiKey, $em): ApiKey|null {
|
||||
// Ideally this would be a SELECT COUNT(...), but MsSQL and Postgres do not allow locking on aggregates
|
||||
// Because of that we check if at least one result exists
|
||||
$firstResult = $em->createQueryBuilder()->select('a.id')
|
||||
|
||||
@@ -16,5 +16,5 @@ interface ApiKeyRepositoryInterface extends ObjectRepository, EntitySpecificatio
|
||||
/**
|
||||
* Will create provided API key only if there's no API keys yet
|
||||
*/
|
||||
public function createInitialApiKey(string $apiKey): ?ApiKey;
|
||||
public function createInitialApiKey(string $apiKey): ApiKey|null;
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ enum Role: string
|
||||
};
|
||||
}
|
||||
|
||||
public static function toSpec(ApiKeyRole $role, ?string $context = null): Specification
|
||||
public static function toSpec(ApiKeyRole $role, string|null $context = null): Specification
|
||||
{
|
||||
return match ($role->role) {
|
||||
self::AUTHORED_SHORT_URLS => new BelongsToApiKey($role->apiKey, $context),
|
||||
|
||||
@@ -11,8 +11,10 @@ use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||
|
||||
class WithApiKeySpecsEnsuringJoin extends BaseSpecification
|
||||
{
|
||||
public function __construct(private readonly ?ApiKey $apiKey, private readonly string $fieldToJoin = 'shortUrls')
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ApiKey|null $apiKey,
|
||||
private readonly string $fieldToJoin = 'shortUrls',
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ class ConfigProvider
|
||||
return $healthRoute !== null ? [...$prefixedRoutes, $healthRoute] : $prefixedRoutes;
|
||||
}
|
||||
|
||||
private static function buildUnversionedHealthRouteFromExistingRoutes(array $routes): ?array
|
||||
private static function buildUnversionedHealthRouteFromExistingRoutes(array $routes): array|null
|
||||
{
|
||||
$healthRoutes = array_filter($routes, fn (array $route) => $route['path'] === '/health');
|
||||
$healthRoute = reset($healthRoutes);
|
||||
|
||||
@@ -23,8 +23,8 @@ class ApiKey extends AbstractEntity
|
||||
*/
|
||||
private function __construct(
|
||||
private string $key,
|
||||
public readonly ?string $name = null,
|
||||
public readonly ?Chronos $expirationDate = null,
|
||||
public readonly string|null $name = null,
|
||||
public readonly Chronos|null $expirationDate = null,
|
||||
private bool $enabled = true,
|
||||
private Collection $roles = new ArrayCollection(),
|
||||
) {
|
||||
@@ -85,7 +85,7 @@ class ApiKey extends AbstractEntity
|
||||
return $this->key;
|
||||
}
|
||||
|
||||
public function spec(?string $context = null): Specification
|
||||
public function spec(string|null $context = null): Specification
|
||||
{
|
||||
$specs = $this->roles->map(fn (ApiKeyRole $role) => Role::toSpec($role, $context))->getValues();
|
||||
return Spec::andX(...$specs);
|
||||
@@ -100,7 +100,7 @@ class ApiKey extends AbstractEntity
|
||||
/**
|
||||
* @return ($apiKey is null ? true : boolean)
|
||||
*/
|
||||
public static function isAdmin(?ApiKey $apiKey): bool
|
||||
public static function isAdmin(ApiKey|null $apiKey): bool
|
||||
{
|
||||
return $apiKey === null || $apiKey->roles->isEmpty();
|
||||
}
|
||||
@@ -108,7 +108,7 @@ class ApiKey extends AbstractEntity
|
||||
/**
|
||||
* Tells if provided API key has any of the roles restricting at the short URL level
|
||||
*/
|
||||
public static function isShortUrlRestricted(?ApiKey $apiKey): bool
|
||||
public static function isShortUrlRestricted(ApiKey|null $apiKey): bool
|
||||
{
|
||||
if ($apiKey === null) {
|
||||
return false;
|
||||
|
||||
@@ -8,7 +8,7 @@ use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||
|
||||
final class ApiKeyCheckResult
|
||||
{
|
||||
public function __construct(public readonly ?ApiKey $apiKey = null)
|
||||
public function __construct(public readonly ApiKey|null $apiKey = null)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ class ApiKeyService implements ApiKeyServiceInterface
|
||||
return $apiKey;
|
||||
}
|
||||
|
||||
public function createInitial(string $key): ?ApiKey
|
||||
public function createInitial(string $key): ApiKey|null
|
||||
{
|
||||
/** @var ApiKeyRepositoryInterface $repo */
|
||||
$repo = $this->em->getRepository(ApiKey::class);
|
||||
@@ -67,7 +67,7 @@ class ApiKeyService implements ApiKeyServiceInterface
|
||||
return $apiKeys;
|
||||
}
|
||||
|
||||
private function getByKey(string $key): ?ApiKey
|
||||
private function getByKey(string $key): ApiKey|null
|
||||
{
|
||||
/** @var ApiKey|null $apiKey */
|
||||
$apiKey = $this->em->getRepository(ApiKey::class)->findOneBy([
|
||||
|
||||
@@ -12,7 +12,7 @@ interface ApiKeyServiceInterface
|
||||
{
|
||||
public function create(ApiKeyMeta $apiKeyMeta): ApiKey;
|
||||
|
||||
public function createInitial(string $key): ?ApiKey;
|
||||
public function createInitial(string $key): ApiKey|null;
|
||||
|
||||
public function check(string $key): ApiKeyCheckResult;
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ class CreateShortUrlTest extends ApiTestCase
|
||||
}
|
||||
|
||||
#[Test, DataProvider('provideConflictingSlugs')]
|
||||
public function failsToCreateShortUrlWithDuplicatedSlug(string $slug, ?string $domain): void
|
||||
public function failsToCreateShortUrlWithDuplicatedSlug(string $slug, string|null $domain): void
|
||||
{
|
||||
$suffix = $domain === null ? '' : sprintf(' for domain "%s"', $domain);
|
||||
$detail = sprintf('Provided slug "%s" is already in use%s.', $slug, $suffix);
|
||||
@@ -171,8 +171,10 @@ class CreateShortUrlTest extends ApiTestCase
|
||||
}
|
||||
|
||||
#[Test, DataProvider('provideConflictingSlugs')]
|
||||
public function returnsErrorWhenRequestingReturnExistingButCustomSlugIsInUse(string $slug, ?string $domain): void
|
||||
{
|
||||
public function returnsErrorWhenRequestingReturnExistingButCustomSlugIsInUse(
|
||||
string $slug,
|
||||
string|null $domain,
|
||||
): void {
|
||||
$longUrl = 'https://www.alejandrocelaya.com';
|
||||
|
||||
[$firstStatusCode] = $this->createShortUrl(['longUrl' => $longUrl]);
|
||||
@@ -269,7 +271,7 @@ class CreateShortUrlTest extends ApiTestCase
|
||||
}
|
||||
|
||||
#[Test, DataProvider('provideDomains')]
|
||||
public function apiKeyDomainIsEnforced(?string $providedDomain): void
|
||||
public function apiKeyDomainIsEnforced(string|null $providedDomain): void
|
||||
{
|
||||
[$statusCode, ['domain' => $returnedDomain]] = $this->createShortUrl(
|
||||
['domain' => $providedDomain],
|
||||
@@ -315,7 +317,7 @@ class CreateShortUrlTest extends ApiTestCase
|
||||
#[Test]
|
||||
#[TestWith([null])]
|
||||
#[TestWith(['my-custom-slug'])]
|
||||
public function prefixCanBeSet(?string $customSlug): void
|
||||
public function prefixCanBeSet(string|null $customSlug): void
|
||||
{
|
||||
[$statusCode, $payload] = $this->createShortUrl([
|
||||
'longUrl' => 'https://github.com/shlinkio/shlink/issues/1557',
|
||||
|
||||
@@ -18,7 +18,7 @@ class DeleteShortUrlTest extends ApiTestCase
|
||||
#[Test, DataProviderExternal(ApiTestDataProviders::class, 'invalidUrlsProvider')]
|
||||
public function notFoundErrorIsReturnWhenDeletingInvalidUrl(
|
||||
string $shortCode,
|
||||
?string $domain,
|
||||
string|null $domain,
|
||||
string $expectedDetail,
|
||||
string $apiKey,
|
||||
): void {
|
||||
|
||||
@@ -90,7 +90,7 @@ class EditShortUrlTest extends ApiTestCase
|
||||
#[Test, DataProviderExternal(ApiTestDataProviders::class, 'invalidUrlsProvider')]
|
||||
public function tryingToEditInvalidUrlReturnsNotFoundError(
|
||||
string $shortCode,
|
||||
?string $domain,
|
||||
string|null $domain,
|
||||
string $expectedDetail,
|
||||
string $apiKey,
|
||||
): void {
|
||||
@@ -125,7 +125,7 @@ class EditShortUrlTest extends ApiTestCase
|
||||
}
|
||||
|
||||
#[Test, DataProvider('provideDomains')]
|
||||
public function metadataIsEditedOnProperShortUrlBasedOnDomain(?string $domain, string $expectedUrl): void
|
||||
public function metadataIsEditedOnProperShortUrlBasedOnDomain(string|null $domain, string $expectedUrl): void
|
||||
{
|
||||
$shortCode = 'ghi789';
|
||||
$url = new Uri(sprintf('/short-urls/%s', $shortCode));
|
||||
|
||||
@@ -45,7 +45,7 @@ class ResolveShortUrlTest extends ApiTestCase
|
||||
#[Test, DataProviderExternal(ApiTestDataProviders::class, 'invalidUrlsProvider')]
|
||||
public function tryingToResolveInvalidUrlReturnsNotFoundError(
|
||||
string $shortCode,
|
||||
?string $domain,
|
||||
string|null $domain,
|
||||
string $expectedDetail,
|
||||
string $apiKey,
|
||||
): void {
|
||||
|
||||
@@ -21,7 +21,7 @@ class ShortUrlVisitsTest extends ApiTestCase
|
||||
#[Test, DataProviderExternal(ApiTestDataProviders::class, 'invalidUrlsProvider')]
|
||||
public function tryingToGetVisitsForInvalidUrlReturnsNotFoundError(
|
||||
string $shortCode,
|
||||
?string $domain,
|
||||
string|null $domain,
|
||||
string $expectedDetail,
|
||||
string $apiKey,
|
||||
): void {
|
||||
@@ -42,7 +42,7 @@ class ShortUrlVisitsTest extends ApiTestCase
|
||||
}
|
||||
|
||||
#[Test, DataProvider('provideDomains')]
|
||||
public function properVisitsAreReturnedWhenDomainIsProvided(?string $domain, int $expectedAmountOfVisits): void
|
||||
public function properVisitsAreReturnedWhenDomainIsProvided(string|null $domain, int $expectedAmountOfVisits): void
|
||||
{
|
||||
$shortCode = 'ghi789';
|
||||
$url = new Uri(sprintf('/short-urls/%s/visits', $shortCode));
|
||||
|
||||
@@ -13,7 +13,7 @@ use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
|
||||
class SingleStepCreateShortUrlTest extends ApiTestCase
|
||||
{
|
||||
#[Test, DataProvider('provideFormats')]
|
||||
public function createsNewShortUrlWithExpectedResponse(?string $format, string $expectedContentType): void
|
||||
public function createsNewShortUrlWithExpectedResponse(string|null $format, string $expectedContentType): void
|
||||
{
|
||||
$resp = $this->createShortUrl($format, 'valid_api_key');
|
||||
|
||||
@@ -43,7 +43,7 @@ class SingleStepCreateShortUrlTest extends ApiTestCase
|
||||
self::assertEquals('Invalid authorization', $payload['title']);
|
||||
}
|
||||
|
||||
private function createShortUrl(?string $format = 'json', ?string $apiKey = null): ResponseInterface
|
||||
private function createShortUrl(string|null $format = 'json', string|null $apiKey = null): ResponseInterface
|
||||
{
|
||||
$query = [
|
||||
'longUrl' => 'https://app.shlink.io',
|
||||
|
||||
@@ -49,7 +49,7 @@ class ApiKeyFixture extends AbstractFixture implements DependentFixtureInterface
|
||||
$manager->flush();
|
||||
}
|
||||
|
||||
private function buildApiKey(string $key, bool $enabled, ?Chronos $expiresAt = null): ApiKey
|
||||
private function buildApiKey(string $key, bool $enabled, Chronos|null $expiresAt = null): ApiKey
|
||||
{
|
||||
$apiKey = ApiKey::fromMeta(ApiKeyMeta::fromParams(expirationDate: $expiresAt));
|
||||
$ref = new ReflectionObject($apiKey);
|
||||
|
||||
@@ -11,7 +11,7 @@ use function sprintf;
|
||||
|
||||
class UrlBuilder
|
||||
{
|
||||
public static function buildShortUrlPath(string $shortCode, ?string $domain, string $suffix = ''): string
|
||||
public static function buildShortUrlPath(string $shortCode, string|null $domain, string $suffix = ''): string
|
||||
{
|
||||
$url = new Uri(sprintf('/short-urls/%s%s', $shortCode, $suffix));
|
||||
if ($domain !== null) {
|
||||
|
||||
@@ -30,11 +30,11 @@ class DomainRedirectsRequestTest extends TestCase
|
||||
#[Test, DataProvider('provideValidData')]
|
||||
public function isProperlyCastToNotFoundRedirects(
|
||||
array $data,
|
||||
?NotFoundRedirectConfigInterface $defaults,
|
||||
NotFoundRedirectConfigInterface|null $defaults,
|
||||
string $expectedAuthority,
|
||||
?string $expectedBaseUrlRedirect,
|
||||
?string $expectedRegular404Redirect,
|
||||
?string $expectedInvalidShortUrlRedirect,
|
||||
string|null $expectedBaseUrlRedirect,
|
||||
string|null $expectedRegular404Redirect,
|
||||
string|null $expectedInvalidShortUrlRedirect,
|
||||
): void {
|
||||
$request = DomainRedirectsRequest::fromRawData($data);
|
||||
$notFound = $request->toNotFoundRedirects($defaults);
|
||||
|
||||
@@ -49,7 +49,7 @@ class MercureInfoActionTest extends TestCase
|
||||
}
|
||||
|
||||
#[Test, DataProvider('provideDays')]
|
||||
public function returnsExpectedInfoWhenEverythingIsOk(?int $days): void
|
||||
public function returnsExpectedInfoWhenEverythingIsOk(int|null $days): void
|
||||
{
|
||||
$this->provider->expects($this->once())->method('buildSubscriptionToken')->willReturn('abc.123');
|
||||
|
||||
|
||||
@@ -39,11 +39,11 @@ class ListShortUrlsActionTest extends TestCase
|
||||
public function properListReturnsSuccessResponse(
|
||||
array $query,
|
||||
int $expectedPage,
|
||||
?string $expectedSearchTerm,
|
||||
string|null $expectedSearchTerm,
|
||||
array $expectedTags,
|
||||
?string $expectedOrderBy,
|
||||
?string $startDate = null,
|
||||
?string $endDate = null,
|
||||
string|null $expectedOrderBy,
|
||||
string|null $startDate = null,
|
||||
string|null $endDate = null,
|
||||
): void {
|
||||
$apiKey = ApiKey::create();
|
||||
$request = ServerRequestFactory::fromGlobals()->withQueryParams($query)
|
||||
|
||||
@@ -25,7 +25,7 @@ class DeleteTagsActionTest extends TestCase
|
||||
}
|
||||
|
||||
#[Test, DataProvider('provideTags')]
|
||||
public function processDelegatesIntoService(?array $tags): void
|
||||
public function processDelegatesIntoService(array|null $tags): void
|
||||
{
|
||||
$request = (new ServerRequest())
|
||||
->withQueryParams(['tags' => $tags])
|
||||
|
||||
@@ -82,7 +82,7 @@ class CrossDomainMiddlewareTest extends TestCase
|
||||
|
||||
#[Test, DataProvider('provideRouteResults')]
|
||||
public function optionsRequestParsesRouteMatchToDetermineAllowedMethods(
|
||||
?string $allowHeader,
|
||||
string|null $allowHeader,
|
||||
string $expectedAllowedMethods,
|
||||
): void {
|
||||
$originalResponse = new Response();
|
||||
|
||||
@@ -40,7 +40,7 @@ class CreateShortUrlContentNegotiationMiddlewareTest extends TestCase
|
||||
}
|
||||
|
||||
#[Test, DataProvider('provideData')]
|
||||
public function properResponseIsReturned(?string $accept, array $query, string $expectedContentType): void
|
||||
public function properResponseIsReturned(string|null $accept, array $query, string $expectedContentType): void
|
||||
{
|
||||
$request = (new ServerRequest())->withQueryParams($query);
|
||||
if ($accept !== null) {
|
||||
|
||||
@@ -35,7 +35,7 @@ class ApiKeyServiceTest extends TestCase
|
||||
* @param RoleDefinition[] $roles
|
||||
*/
|
||||
#[Test, DataProvider('provideCreationDate')]
|
||||
public function apiKeyIsProperlyCreated(?Chronos $date, ?string $name, array $roles): void
|
||||
public function apiKeyIsProperlyCreated(Chronos|null $date, string|null $name, array $roles): void
|
||||
{
|
||||
$this->em->expects($this->once())->method('flush');
|
||||
$this->em->expects($this->once())->method('persist')->with($this->isInstanceOf(ApiKey::class));
|
||||
@@ -68,7 +68,7 @@ class ApiKeyServiceTest extends TestCase
|
||||
}
|
||||
|
||||
#[Test, DataProvider('provideInvalidApiKeys')]
|
||||
public function checkReturnsFalseForInvalidApiKeys(?ApiKey $invalidKey): void
|
||||
public function checkReturnsFalseForInvalidApiKeys(ApiKey|null $invalidKey): void
|
||||
{
|
||||
$this->repo->expects($this->once())->method('findOneBy')->with(['key' => '12345'])->willReturn($invalidKey);
|
||||
$this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo);
|
||||
@@ -154,7 +154,7 @@ class ApiKeyServiceTest extends TestCase
|
||||
}
|
||||
|
||||
#[Test, DataProvider('provideInitialApiKeys')]
|
||||
public function createInitialDelegatesToRepository(?ApiKey $apiKey): void
|
||||
public function createInitialDelegatesToRepository(ApiKey|null $apiKey): void
|
||||
{
|
||||
$this->repo->expects($this->once())->method('createInitialApiKey')->with('the_key')->willReturn($apiKey);
|
||||
$this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo);
|
||||
|
||||
Reference in New Issue
Block a user