Skip to content

Commit

Permalink
Add types to all the things!
Browse files Browse the repository at this point in the history
  • Loading branch information
ramsey committed Dec 19, 2024
1 parent 84e9a7c commit 388acd9
Show file tree
Hide file tree
Showing 33 changed files with 183 additions and 424 deletions.
12 changes: 4 additions & 8 deletions src/Grant/AbstractGrant.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,21 @@ abstract class AbstractGrant
/**
* Returns the name of this grant, eg. 'grant_name', which is used as the
* grant type when encoding URL query parameters.
*
* @return string
*/
abstract protected function getName();
abstract protected function getName(): string;

/**
* Returns a list of all required request parameters.
*
* @return list<string>
*/
abstract protected function getRequiredRequestParameters();
abstract protected function getRequiredRequestParameters(): array;

/**
* Returns this grant's name as its string representation. This allows for
* string interpolation when building URL query parameters.
*
* @return string
*/
public function __toString()
public function __toString(): string
{
return $this->getName();
}
Expand All @@ -72,7 +68,7 @@ public function __toString()
*
* @return array<string, mixed>
*/
public function prepareRequestParameters(array $defaults, array $options)
public function prepareRequestParameters(array $defaults, array $options): array
{
$defaults['grant_type'] = $this->getName();

Expand Down
7 changes: 2 additions & 5 deletions src/Grant/AuthorizationCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,15 @@
*/
class AuthorizationCode extends AbstractGrant
{
/**
* @inheritdoc
*/
protected function getName()
protected function getName(): string
{
return 'authorization_code';
}

/**
* @inheritdoc
*/
protected function getRequiredRequestParameters()
protected function getRequiredRequestParameters(): array
{
return [
'code',
Expand Down
7 changes: 2 additions & 5 deletions src/Grant/ClientCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,15 @@
*/
class ClientCredentials extends AbstractGrant
{
/**
* @inheritdoc
*/
protected function getName()
protected function getName(): string
{
return 'client_credentials';
}

/**
* @inheritdoc
*/
protected function getRequiredRequestParameters()
protected function getRequiredRequestParameters(): array
{
return [];
}
Expand Down
32 changes: 10 additions & 22 deletions src/Grant/GrantFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ class GrantFactory

/**
* Defines a grant singleton in the registry.
*
* @return self
*/
public function setGrant(string $name, AbstractGrant $grant)
public function setGrant(string $name, AbstractGrant $grant): static
{
$this->registry[$name] = $grant;

Expand All @@ -55,10 +53,8 @@ public function setGrant(string $name, AbstractGrant $grant)
* Returns a grant singleton by name.
*
* If the grant has not be registered, a default grant will be loaded.
*
* @return AbstractGrant
*/
public function getGrant(string $name)
public function getGrant(string $name): AbstractGrant
{
if (!isset($this->registry[$name])) {
$this->registerDefaultGrant($name);
Expand All @@ -70,10 +66,8 @@ public function getGrant(string $name)

/**
* Registers a default grant singleton by name.
*
* @return self
*/
protected function registerDefaultGrant(string $name)
protected function registerDefaultGrant(string $name): static
{
// PascalCase the grant. E.g: 'authorization_code' becomes 'AuthorizationCode'
$class = str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $name)));
Expand All @@ -87,11 +81,9 @@ protected function registerDefaultGrant(string $name)
/**
* Determines if a variable is a valid grant.
*
* @return bool
*
* @phpstan-assert-if-true class-string<AbstractGrant> | AbstractGrant $class
*/
public function isGrant(mixed $class)
public function isGrant(mixed $class): bool
{
if (!is_string($class) && !is_object($class)) {
return false;
Expand All @@ -103,22 +95,18 @@ public function isGrant(mixed $class)
/**
* Checks if a variable is a valid grant.
*
* @return void
*
* @throws InvalidGrantException
*
* @phpstan-assert class-string<AbstractGrant> | AbstractGrant $class
*/
public function checkGrant(mixed $class)
public function checkGrant(mixed $class): void
{
if (!$this->isGrant($class)) {
if (is_object($class)) {
$type = $class::class;
} elseif (is_scalar($class)) {
$type = $class;
} else {
$type = gettype($class);
}
$type = match (true) {
is_object($class) => $class::class,
is_scalar($class) => $class,
default => gettype($class),
};

throw new InvalidGrantException(sprintf('Grant "%s" must extend AbstractGrant', $type));
}
Expand Down
7 changes: 2 additions & 5 deletions src/Grant/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,15 @@
*/
class Password extends AbstractGrant
{
/**
* @inheritdoc
*/
protected function getName()
protected function getName(): string
{
return 'password';
}

/**
* @inheritdoc
*/
protected function getRequiredRequestParameters()
protected function getRequiredRequestParameters(): array
{
return [
'username',
Expand Down
7 changes: 2 additions & 5 deletions src/Grant/RefreshToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,15 @@
*/
class RefreshToken extends AbstractGrant
{
/**
* @inheritdoc
*/
protected function getName()
protected function getName(): string
{
return 'refresh_token';
}

/**
* @inheritdoc
*/
protected function getRequiredRequestParameters()
protected function getRequiredRequestParameters(): array
{
return [
'refresh_token',
Expand Down
2 changes: 1 addition & 1 deletion src/OptionProvider/OptionProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ interface OptionProviderInterface
*
* @return array<string, mixed>
*/
public function getAccessTokenOptions(string $method, array $params);
public function getAccessTokenOptions(string $method, array $params): array;
}
4 changes: 1 addition & 3 deletions src/OptionProvider/PostAuthOptionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ public function getAccessTokenOptions(string $method, array $params): array
* Returns the request body for requesting an access token.
*
* @param array<string, mixed> $params
*
* @return string
*/
protected function getAccessTokenBody(array $params)
protected function getAccessTokenBody(array $params): string
{
return $this->buildQueryString($params);
}
Expand Down
Loading

0 comments on commit 388acd9

Please sign in to comment.