Skip to content

PHPStan error when using authenticate with ClientCredentialsGrant #521

@treyssatvincent

Description

@treyssatvincent

Hello, with the following code:

// $pendingRequest is a \Saloon\Http\PendingRequest
// $connector is an implementation of \Saloon\Http\Connector that uses \Saloon\Traits\OAuth2\ClientCredentialsGrant
$pendingRequest->authenticate($connector->getAccessToken());

PHPStan will detect it as a possible bug:

Parameter #1 $authenticator of method Saloon\Http\PendingRequest::authenticate() expects Saloon\Contracts\Authenticator,
Saloon\Contracts\OAuthAuthenticator|Saloon\Http\Response given.

And indeed if getAccessToken returns a Response, authenticate would crash.

By using conditional return types in the PHPDoc we can tell that the return type is either a OAuthAuthenticator or a Response:

/**
 * Get the access token
 *
 * @template TRequest of \Saloon\Http\Request
 *
 * @param array<string> $scopes
 * @param callable(TRequest): (void)|null $requestModifier
 * @return ($returnResponse is true ? Response : OAuthAuthenticator)
 */
public function getAccessToken(array $scopes = [], string $scopeSeparator = ' ', bool $returnResponse = false, ?callable $requestModifier = null): OAuthAuthenticator|Response

(here's a commit of this change: treyssatvincent@724ad17)

I can open a pull request if that's something you'll want.

I wasn't sure that the sentence "please first discuss the change you wish to make via issue, email, or any other method" from CONTRIBUTING.md allowed me to create the pull request with the text I included here as description, if it was then I'm sorry for the extra step!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions