From b0ad41f07dad2fff9b04653a5f12b7c7f43d8fd6 Mon Sep 17 00:00:00 2001 From: Anatoly Pashin Date: Mon, 4 Nov 2024 16:41:32 +0700 Subject: [PATCH 1/2] Mark Builder methods as pure --- src/Builder.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Builder.php b/src/Builder.php index 4295e3f0..da713b61 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -18,11 +18,15 @@ interface Builder * Appends new items to audience * * @param non-empty-string ...$audiences + * + * @pure */ public function permittedFor(string ...$audiences): Builder; /** * Configures the expiration time + * + * @pure */ public function expiresAt(DateTimeImmutable $expiration): Builder; @@ -30,11 +34,15 @@ public function expiresAt(DateTimeImmutable $expiration): Builder; * Configures the token id * * @param non-empty-string $id + * + * @pure */ public function identifiedBy(string $id): Builder; /** * Configures the time that the token was issued + * + * @pure */ public function issuedAt(DateTimeImmutable $issuedAt): Builder; @@ -42,11 +50,15 @@ public function issuedAt(DateTimeImmutable $issuedAt): Builder; * Configures the issuer * * @param non-empty-string $issuer + * + * @pure */ public function issuedBy(string $issuer): Builder; /** * Configures the time before which the token cannot be accepted + * + * @pure */ public function canOnlyBeUsedAfter(DateTimeImmutable $notBefore): Builder; @@ -54,6 +66,8 @@ public function canOnlyBeUsedAfter(DateTimeImmutable $notBefore): Builder; * Configures the subject * * @param non-empty-string $subject + * + * @pure */ public function relatedTo(string $subject): Builder; @@ -61,6 +75,8 @@ public function relatedTo(string $subject): Builder; * Configures a header item * * @param non-empty-string $name + * + * @pure */ public function withHeader(string $name, mixed $value): Builder; @@ -70,6 +86,8 @@ public function withHeader(string $name, mixed $value): Builder; * @param non-empty-string $name * * @throws RegisteredClaimGiven When trying to set a registered claim. + * + * @pure */ public function withClaim(string $name, mixed $value): Builder; From 5012bc481cb39fe13be5219ab5bff7c9fd282592 Mon Sep 17 00:00:00 2001 From: Anatoly Pashin Date: Mon, 4 Nov 2024 21:50:21 +0700 Subject: [PATCH 2/2] Revert interface changes, mark implementation methods --- src/Builder.php | 18 ------------------ src/Token/Builder.php | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/Builder.php b/src/Builder.php index da713b61..4295e3f0 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -18,15 +18,11 @@ interface Builder * Appends new items to audience * * @param non-empty-string ...$audiences - * - * @pure */ public function permittedFor(string ...$audiences): Builder; /** * Configures the expiration time - * - * @pure */ public function expiresAt(DateTimeImmutable $expiration): Builder; @@ -34,15 +30,11 @@ public function expiresAt(DateTimeImmutable $expiration): Builder; * Configures the token id * * @param non-empty-string $id - * - * @pure */ public function identifiedBy(string $id): Builder; /** * Configures the time that the token was issued - * - * @pure */ public function issuedAt(DateTimeImmutable $issuedAt): Builder; @@ -50,15 +42,11 @@ public function issuedAt(DateTimeImmutable $issuedAt): Builder; * Configures the issuer * * @param non-empty-string $issuer - * - * @pure */ public function issuedBy(string $issuer): Builder; /** * Configures the time before which the token cannot be accepted - * - * @pure */ public function canOnlyBeUsedAfter(DateTimeImmutable $notBefore): Builder; @@ -66,8 +54,6 @@ public function canOnlyBeUsedAfter(DateTimeImmutable $notBefore): Builder; * Configures the subject * * @param non-empty-string $subject - * - * @pure */ public function relatedTo(string $subject): Builder; @@ -75,8 +61,6 @@ public function relatedTo(string $subject): Builder; * Configures a header item * * @param non-empty-string $name - * - * @pure */ public function withHeader(string $name, mixed $value): Builder; @@ -86,8 +70,6 @@ public function withHeader(string $name, mixed $value): Builder; * @param non-empty-string $name * * @throws RegisteredClaimGiven When trying to set a registered claim. - * - * @pure */ public function withClaim(string $name, mixed $value): Builder; diff --git a/src/Token/Builder.php b/src/Token/Builder.php index d92d41e1..355766b7 100644 --- a/src/Token/Builder.php +++ b/src/Token/Builder.php @@ -29,6 +29,10 @@ public function __construct(private readonly Encoder $encoder, private readonly { } + /** + * @inheritDoc + * @pure + */ public function permittedFor(string ...$audiences): BuilderInterface { $configured = $this->claims[RegisteredClaims::AUDIENCE] ?? []; @@ -37,36 +41,64 @@ public function permittedFor(string ...$audiences): BuilderInterface return $this->setClaim(RegisteredClaims::AUDIENCE, array_merge($configured, $toAppend)); } + /** + * @inheritDoc + * @pure + */ public function expiresAt(DateTimeImmutable $expiration): BuilderInterface { return $this->setClaim(RegisteredClaims::EXPIRATION_TIME, $expiration); } + /** + * @inheritDoc + * @pure + */ public function identifiedBy(string $id): BuilderInterface { return $this->setClaim(RegisteredClaims::ID, $id); } + /** + * @inheritDoc + * @pure + */ public function issuedAt(DateTimeImmutable $issuedAt): BuilderInterface { return $this->setClaim(RegisteredClaims::ISSUED_AT, $issuedAt); } + /** + * @inheritDoc + * @pure + */ public function issuedBy(string $issuer): BuilderInterface { return $this->setClaim(RegisteredClaims::ISSUER, $issuer); } + /** + * @inheritDoc + * @pure + */ public function canOnlyBeUsedAfter(DateTimeImmutable $notBefore): BuilderInterface { return $this->setClaim(RegisteredClaims::NOT_BEFORE, $notBefore); } + /** + * @inheritDoc + * @pure + */ public function relatedTo(string $subject): BuilderInterface { return $this->setClaim(RegisteredClaims::SUBJECT, $subject); } + /** + * @inheritDoc + * @pure + */ public function withHeader(string $name, mixed $value): BuilderInterface { $new = clone $this; @@ -75,6 +107,10 @@ public function withHeader(string $name, mixed $value): BuilderInterface return $new; } + /** + * @inheritDoc + * @pure + */ public function withClaim(string $name, mixed $value): BuilderInterface { if (in_array($name, RegisteredClaims::ALL, true)) {