Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow method chaining #205

Open
wants to merge 1 commit into
base: 11.3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/HOTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
$htop = new self($secret);
$htop->setCounter(self::DEFAULT_COUNTER);
$htop->setDigest(self::DEFAULT_DIGEST);
$htop->setDigits(self::DEFAULT_DIGITS);

Check warning on line 39 in src/HOTP.php

View workflow job for this annotation

GitHub Actions / 5️⃣ Mutation Testing

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ $htop = new self($secret); $htop->setCounter(self::DEFAULT_COUNTER); $htop->setDigest(self::DEFAULT_DIGEST); - $htop->setDigits(self::DEFAULT_DIGITS); + return $htop; } public static function generate() : self

return $htop;
}
Expand All @@ -49,14 +49,14 @@
public function getCounter(): int
{
$value = $this->getParameter('counter');
(is_int($value) && $value >= 0) || throw new InvalidArgumentException('Invalid "counter" parameter.');

Check warning on line 52 in src/HOTP.php

View workflow job for this annotation

GitHub Actions / 5️⃣ Mutation Testing

Escaped Mutant for Mutator "LogicalAnd": --- Original +++ New @@ @@ public function getCounter() : int { $value = $this->getParameter('counter'); - is_int($value) && $value >= 0 || throw new InvalidArgumentException('Invalid "counter" parameter.'); + is_int($value) || $value >= 0 || throw new InvalidArgumentException('Invalid "counter" parameter.'); return $value; } public function getProvisioningUri() : string

Check warning on line 52 in src/HOTP.php

View workflow job for this annotation

GitHub Actions / 5️⃣ Mutation Testing

Escaped Mutant for Mutator "LogicalOrNegation": --- Original +++ New @@ @@ public function getCounter() : int { $value = $this->getParameter('counter'); - is_int($value) && $value >= 0 || throw new InvalidArgumentException('Invalid "counter" parameter.'); + !(is_int($value) && $value >= 0 || throw new InvalidArgumentException('Invalid "counter" parameter.')); return $value; } public function getProvisioningUri() : string

return $value;
}

public function getProvisioningUri(): string
{
return $this->generateURI('hotp', [

Check warning on line 59 in src/HOTP.php

View workflow job for this annotation

GitHub Actions / 5️⃣ Mutation Testing

Escaped Mutant for Mutator "ArrayItemRemoval": --- Original +++ New @@ @@ } public function getProvisioningUri() : string { - return $this->generateURI('hotp', ['counter' => $this->getCounter()]); + return $this->generateURI('hotp', []); } /** * If the counter is not provided, the OTP is verified at the actual counter.
'counter' => $this->getCounter(),
]);
}
Expand All @@ -66,7 +66,7 @@
*/
public function verify(string $otp, null|int $counter = null, null|int $window = null): bool
{
$counter >= 0 || throw new InvalidArgumentException('The counter must be at least 0.');

Check warning on line 69 in src/HOTP.php

View workflow job for this annotation

GitHub Actions / 5️⃣ Mutation Testing

Escaped Mutant for Mutator "LogicalOrNegation": --- Original +++ New @@ @@ */ public function verify(string $otp, null|int $counter = null, null|int $window = null) : bool { - $counter >= 0 || throw new InvalidArgumentException('The counter must be at least 0.'); + !($counter >= 0 || throw new InvalidArgumentException('The counter must be at least 0.')); if ($counter === null) { $counter = $this->getCounter(); } elseif ($counter < $this->getCounter()) {

if ($counter === null) {
$counter = $this->getCounter();
Expand All @@ -77,9 +77,11 @@
return $this->verifyOtpWithWindow($otp, $counter, $window);
}

public function setCounter(int $counter): void
public function setCounter(int $counter): static
{
$this->setParameter('counter', $counter);

return $this;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/HOTPInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ public static function create(
int $digits = 6
): self;

public function setCounter(int $counter): void;
public function setCounter(int $counter): static;
}
10 changes: 5 additions & 5 deletions src/OTPInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public static function generate(): self;
/**
* @param non-empty-string $secret
*/
public function setSecret(string $secret): void;
public function setSecret(string $secret): static;

public function setDigits(int $digits): void;
public function setDigits(int $digits): static;

/**
* @param non-empty-string $digest
*/
public function setDigest(string $digest): void;
public function setDigest(string $digest): static;

/**
* @return non-empty-string Return the OTP at the specified timestamp
Expand All @@ -57,7 +57,7 @@ public function getSecret(): string;
/**
* @param non-empty-string $label The label of the OTP
*/
public function setLabel(string $label): void;
public function setLabel(string $label): static;

/**
* @return non-empty-string|null The label of the OTP
Expand All @@ -72,7 +72,7 @@ public function getIssuer(): ?string;
/**
* @param non-empty-string $issuer
*/
public function setIssuer(string $issuer): void;
public function setIssuer(string $issuer): static;

/**
* @return bool If true, the issuer will be added as a parameter in the provisioning URI
Expand Down
20 changes: 15 additions & 5 deletions src/ParameterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,23 @@ public function getLabel(): null|string
return $this->label;
}

public function setLabel(string $label): void
public function setLabel(string $label): static
{
$this->setParameter('label', $label);

return $this;
}

public function getIssuer(): null|string
{
return $this->issuer;
}

public function setIssuer(string $issuer): void
public function setIssuer(string $issuer): static
{
$this->setParameter('issuer', $issuer);

return $this;
}

public function isIssuerIncludedAsParameter(): bool
Expand Down Expand Up @@ -128,19 +132,25 @@ public function setParameter(string $parameter, mixed $value): void
}
}

public function setSecret(string $secret): void
public function setSecret(string $secret): static
{
$this->setParameter('secret', $secret);

return $this;
}

public function setDigits(int $digits): void
public function setDigits(int $digits): static
{
$this->setParameter('digits', $digits);

return $this;
}

public function setDigest(string $digest): void
public function setDigest(string $digest): static
{
$this->setParameter('algorithm', $digest);

return $this;
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/TOTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,18 @@ public function getProvisioningUri(): string
return $this->generateURI('totp', $params);
}

public function setPeriod(int $period): void
public function setPeriod(int $period): static
{
$this->setParameter('period', $period);

return $this;
}

public function setEpoch(int $epoch): void
public function setEpoch(int $epoch): static
{
$this->setParameter('epoch', $epoch);

return $this;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/TOTPInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public static function create(
int $digits = self::DEFAULT_DIGITS
): self;

public function setPeriod(int $period): void;
public function setPeriod(int $period): static;

public function setEpoch(int $epoch): void;
public function setEpoch(int $epoch): static;

/**
* Return the TOTP at the current time.
Expand Down
Loading