phpstan-wordpress icon indicating copy to clipboard operation
phpstan-wordpress copied to clipboard

Move dynamic return type extensions into wordpress-stubs

Open johnbillion opened this issue 3 years ago • 5 comments

@szepeviktor PHPStan 1.6 added support for conditional return types in docblocks, eg @return ($as_float is true ? float : string).

Some of the dynamic return type extensions (eg. CurrentTimeDynamicFunctionReturnTypeExtension) could be replaced by a conditional return type doc in the wordpress-stubs repo instead. Then we'd only need dynamic return type extensions for functions with more complicated logic.

What do you think? Worth doing?

johnbillion avatar Apr 26 '22 11:04 johnbillion

Example (untested):

/**
 * @phpstan-return ($type is 'timestamp'|'U' ? int : string)
 */
function current_time( $type, $gmt = false) {}

johnbillion avatar Apr 26 '22 11:04 johnbillion

PHPStan 1.6 added support for conditional return types

I go crazy! You have to know I'm old-fashioned guy: keep a Single type in a variable.

szepeviktor avatar Apr 26 '22 11:04 szepeviktor

As for the answer: I have no clue how to manipulate stubs in a sustainable fashion. Keep a big array<Class::method, $returnTypeString>?

szepeviktor avatar Apr 26 '22 11:04 szepeviktor

I would need to check but I think the existing functionMap file could be used.

Example:

'current_time' => '($type is \'timestamp\'|\'U\' ? int : string)',

johnbillion avatar Apr 26 '22 11:04 johnbillion

All right.

szepeviktor avatar Apr 26 '22 12:04 szepeviktor