Make WordPress Core


Ignore:
Timestamp:
07/09/2026 05:56:05 AM (7 weeks ago)
Author:
westonruter
Message:

Code Quality: Add PHPStan conditional return types to the slashing functions.

Adds @phpstan- prefixed generics and conditional return types to map_deep(), stripslashes_from_strings_only(), stripslashes_deep(), wp_slash(), wp_unslash(), and add_magic_quotes(). Static analysis now knows that a string passed to the slashing functions yields a string, and an array yields an array; this is something the plain @return tags could not express, and which caused wp_unslash() to widen its callers' types to array|string even after an is_array() guard.

The returns are conditional rather than a bare T for two reasons: map_deep()'s callback may change the type of every leaf, and the slashing functions rewrite string contents, so a literal-string type cannot survive.

Developed in https://github.com/WordPress/wordpress-develop/pull/12455.

See #64898.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/formatting.php

    r62482 r62672  
    28492849 * @param mixed $value The value to be stripped.
    28502850 * @return mixed Stripped value.
     2851 *
     2852 * @phpstan-template T
     2853 * @phpstan-param T $value
     2854 * @phpstan-return (
     2855 *     T is string ? string : (
     2856 *         T is array ? array<key-of<T>, ( value-of<T> is string ? string : value-of<T> )> : T
     2857 *     )
     2858 * )
    28512859 */
    28522860function stripslashes_deep( $value ) {
     
    28612869 * @param mixed $value The array or string to be stripped.
    28622870 * @return mixed The stripped value.
     2871 *
     2872 * @phpstan-template T
     2873 * @phpstan-param T $value
     2874 * @phpstan-return (T is string ? string : T)
    28632875 */
    28642876function stripslashes_from_strings_only( $value ) {
     
    51715183 * @param callable $callback The function to map onto $value.
    51725184 * @return mixed The value with the callback applied to all non-arrays and non-objects inside it.
     5185 *
     5186 * @phpstan-template T
     5187 * @phpstan-param T $value
     5188 * @phpstan-return (T is array ? array<key-of<T>, mixed> : (T is object ? T : mixed))
    51735189 */
    51745190function map_deep( $value, $callback ) {
     
    58125828 * @param string|array $value String or array of data to slash.
    58135829 * @return string|array Slashed `$value`, in the same type as supplied.
     5830 *
     5831 * @phpstan-template T
     5832 * @phpstan-param T $value
     5833 * @phpstan-return (
     5834 *     T is string ? string : (
     5835 *         T is array ? array<key-of<T>, ( value-of<T> is string ? string : value-of<T> )> : T
     5836 *     )
     5837 * )
    58145838 */
    58155839function wp_slash( $value ) {
     
    58355859 * @param string|array $value String or array of data to unslash.
    58365860 * @return string|array Unslashed `$value`, in the same type as supplied.
     5861 *
     5862 * @phpstan-template T
     5863 * @phpstan-param T $value
     5864 * @phpstan-return (
     5865 *     T is string ? string : (
     5866 *         T is array ? array<key-of<T>, ( value-of<T> is string ? string : value-of<T> )> : T
     5867 *     )
     5868 * )
    58375869 */
    58385870function wp_unslash( $value ) {
Note: See TracChangeset for help on using the changeset viewer.