Skip to content

Builder fluent interface is confusing #977

Description

@Slamdunk

Currently the Token Builder is confusing:

public function withClaim(string $name, mixed $value): BuilderInterface
{
if (in_array($name, RegisteredClaims::ALL, true)) {
throw RegisteredClaimGiven::forClaim($name);
}
return $this->setClaim($name, $value);
}
/** @param non-empty-string $name */
private function setClaim(string $name, mixed $value): BuilderInterface
{
$this->claims[$name] = $value;
return $this;
}

The name of the method resemble an immutable object, but under the hood it's a fluent interface.
All my colleagues get confused too by reading a method like:

class User
{
    public function fillJwtToken(JwtBuilder $jwtBuilder): void
    {
        $jwtBuilder->withClaim('user-id', $this->id);
    }
}

Did the developer forget to return the new object?
Or its more a setClaim rather than a withClaim?

Can we take a direction and forget the other one?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions