Skip to content

Fix: Fix PHPStan error by chaining Stringable methods for email+IP #158

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Aluisio-Pires
Copy link

Problem:
$this->string('email') returns a Stringable. In the old code we did:

return Str::transliterate(
    Str::lower($this->string('email')) . '|' . $this->ip()
);

By using the . operator to append |{$this->ip()}, the Stringable was immediately cast to a plain string. PHPStan complained because the concatenation happens without casting the Stringable object to string.
Solution:
We now keep everything as a Stringable:

return $this->string('email')
            ->lower()                       // lower-case while still a Stringable
            ->append('|'.$this->ip())      // append the IP without casting
            ->transliterate()              // transliterate on the Stringable
            ->value();                     // finally cast to string

This chains all transformations on the Stringable object and only converts to a string once, which satisfies PHPStan’s type checks.

@Aluisio-Pires
Copy link
Author

Before:
image
After:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant