Skip to content

Comments

[12.x] Improve types in \Illuminate\Support\Str helper#58356

Merged
taylorotwell merged 1 commit intolaravel:12.xfrom
shaedrich:improve-str-types
Jan 13, 2026
Merged

[12.x] Improve types in \Illuminate\Support\Str helper#58356
taylorotwell merged 1 commit intolaravel:12.xfrom
shaedrich:improve-str-types

Conversation

@shaedrich
Copy link
Contributor

Benefits

✅ Improved static analysis
✅ Improved type inference
✅ Increased type coverage

Highlights

  • Closure/callable parameter and return types
  • array type narrowing
  • number narrowing
  • more precise return type when input strings are empty
  • non-empty-string assertions
  • string narrowing

@shaedrich
Copy link
Contributor Author

Test failure doesn't seem to be related to changes

@donnysim
Copy link
Contributor

I really wish there was just @phpstan-return for returns like ($needles is array{} ? false : ($haystack is non-empty-string ? bool : false)). It makes IDE support worse and it's such an abomination of a syntax.

@shaedrich
Copy link
Contributor Author

Fair point 🤔 I used what was already used elsewhere. Let's see what Taylor has to say

@donnysim
Copy link
Contributor

I don't really know if phpstan even has a custom return tag, but others psalm etc. do for the same reason as mentioned. I find it kind of a weird phpstan choice to use custom syntax on an already established tag. Currently have problems with other functions that have this and no explicit return type as it just ends up being mixed in IDE.

@shaedrich
Copy link
Contributor Author

I don't really know if phpstan even has a custom return tag, but others psalm etc. do for the same reason as mentioned. I find it kind of a weird phpstan choice to use custom syntax on an already established tag. Currently have problems with other functions that have this and no explicit return type as it just ends up being mixed in IDE.

It does:

Prefixed tags

Supported tags (@var, @param, @return, and all generics-related ones) can be prefixed with @phpstan-:

/**
 * @phpstan-param Foo $param
 * @phpstan-return Bar
 */
function foo ($param) { ... }

This is useful in the context of advanced types and generics. IDEs and other PHP tools might not understand the advanced types that PHPStan takes advantage of. So you can leave the ordinary @param in the PHPDoc and add a @phpstan-param with an advanced type syntax.

others psalm etc. do for the same reason as mentioned. I find it kind of a weird phpstan choice to use custom syntax on an already established tag. Currently have problems with other functions that have this and no explicit return type as it just ends up being mixed in IDE.

I didn't know that 😲 That really isn't ideal 😕 Maybe, we should prefix all conditional returns in Laravel 🤔

@donnysim
Copy link
Contributor

I must be bad at searching then 😅 tried to skim through with search and found nothing. As the framework already is embracing phpstan as default it's probably not in interest to yet again get a flood of changes so it is what it is. Saw that PHPStorm tries to support it best it can at the type level but ain't always on point, especially on the nested conditions where it just drops half of the condition types so maybe it will get better and won't matter in the long run. At least from the JetBrains side 😬

@shaedrich
Copy link
Contributor Author

I must be bad at searching then 😅

Good to know that I'm not the only one who gets a little lost in the PHPStan docs 😅

@taylorotwell taylorotwell merged commit 3a3c9d6 into laravel:12.x Jan 13, 2026
36 of 72 checks passed
@shaedrich shaedrich deleted the improve-str-types branch January 13, 2026 14:47
Copy link

@wwright216 wwright216 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this has already been merged (and now released) but....

* @param array|string $subject
* @param string|string[] $pattern
* @param (\Closure(array): string)|string[]|string $replace
* @param array|string[] $subject

This comment was marked as resolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestions, there's already a PR for this: #58364

* @param array|string[] $subject
* @param int $limit
* @return string|string[]|null
* @return ($subject is array ? string|null : string[]|null)

This comment was marked as resolved.

sumaiazaman added a commit to sumaiazaman/framework that referenced this pull request Jan 24, 2026
Add explicit array|string parameter type hint and string return type
to the toCssClasses method, following the pattern established in recent
type improvement PRs like laravel#58356.

This improves type safety and IDE support without changing functionality.
sumaiazaman added a commit to sumaiazaman/framework that referenced this pull request Jan 24, 2026
Add explicit array|string parameter type hint and string return type
to the toCssStyles method, following the pattern established in recent
type improvement PRs like laravel#58356.

This improves type safety and IDE support without changing functionality.
sumaiazaman added a commit to sumaiazaman/framework that referenced this pull request Jan 24, 2026
Add explicit mixed parameter type hint and bool return type to the
accessible method, following the pattern established in recent type
improvement PRs like laravel#58356.

This improves type safety and IDE support without changing functionality.
sumaiazaman added a commit to sumaiazaman/framework that referenced this pull request Jan 24, 2026
Add explicit mixed parameter type hint and bool return type to the
arrayable method, following the pattern established in recent type
improvement PRs like laravel#58356.

This improves type safety and IDE support without changing functionality.
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.

4 participants