Skip to content

Conversation

@IanDelMar
Copy link
Contributor

Adds a dynamic function return type extension for handling:

  1. untrailingslashit() — removes trailing forward slashes and backslashes,
  2. trailingslashit() — adds a trailing slash after removing any trailing slashes,
  3. backslashit() — adds backslashes before letters and before a number at the start of a string.

For (1) and (2), the extension uses the types PHPStan provides for rtrim() and string concatenation.
For (3), the extension traverses types, generalising constant strings to strings with accessory types, and replaces numeric-string (backslashes will be added) and non-empty-string (if it were '0', backslashes would be added) with non-falsy-string.

Fixes the StripslashesFromStringsOnlyDynamicFunctionReturnTypeExtension by letting PHPStan handle the type for non-constant string arguments (e.g. a non-falsy string might be '\\', which becomes ''). Currently, PHPStan returns string for calls to stripslashes(). If PHPStan ever decides that stripslashes() should be, or is worth being, handled more granularly, this extension will align with that.

@szepeviktor
Copy link
Owner

szepeviktor commented Sep 11, 2025

It fails with Locking phpstan/phpstan (2.0.0)
How high should I raise it?

"phpstan/phpstan": "^2.0"

@IanDelMar
Copy link
Contributor Author

Since 2.1.18, there are no errors; the "errors" (nothing is incorrect--prior to 2.1.18 the types were simply wider) occurred because PHPStan improved its inference of rtrim()’s return type. It’s unfortunate that we have to bump the version to overcome this.

@szepeviktor
Copy link
Owner

Done.

@szepeviktor szepeviktor merged commit 4143d06 into szepeviktor:2.x Sep 11, 2025
1 of 2 checks passed
@IanDelMar IanDelMar deleted the slashit branch September 11, 2025 22:15
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.

2 participants