Psalm: Add Stringable type

Created on 27 Mar 2020  路  8Comments  路  Source: vimeo/psalm

PHP's source now contains the Stringable type. The latest versions of Symfony have started to add this type to their docblocks, which Psalm complains about as an undefined class/interface.

See https://github.com/php/php-src/pull/5083

https://psalm.dev/r/70d9510fe2

Most helpful comment

I imagine stub would be pointless too: if you're targeting PHP7 you still need a polyfill (for runtime support), and if you're targeting PHP8 the interface would be discovered via reflection.

Cross-checking from older to newer version (e.g. Psalm running on PHP7 checking PHP8 code) wasn't a supported scenario for Psalm last time I checked.

All 8 comments

I found these snippets:


https://psalm.dev/r/70d9510fe2

<?php

/**
 * @return string|\Stringable
 */
function getStringish() {
    return '';
}
Psalm output (using commit 031c5be):

ERROR: UndefinedDocblockClass - 4:12 - Docblock-defined class or interface Stringable does not exist

Stringable is feature of php8 .I think you do not use php8. For fix issue in your project add polyfill php8 https://github.com/symfony/polyfill-php80 .

Yeah, Psalm doesn鈥檛 yet support PHP 8, on account of it鈥檚 not released. When PHP 8 is released, I imagine Stringable will automatically work - if it doesn鈥檛, feel free to reopen.

@muglug I don't think it'll work automatically, as it's an implicit interface: any class that has __toString() method is an instance of Stringable, even if it doesn't have it in its implements declaration.

Anyway, it would be a part of PHP8 support effort.

It's possible to create a stub for that. Would anybody do that, if not I can release one.

I personally need it because of https://github.com/symfony/symfony/blob/v4.4.7/src/Symfony/Component/Validator/ConstraintViolationInterface.php#L39

Wait a second - given it's an interface no stubs needed - one may just use one of the polyfills available, eg https://github.com/symfony/polyfill/blob/master/src/Php80/Resources/stubs/Stringable.php

https://packagist.org/packages/symfony/polyfill-php80

I imagine stub would be pointless too: if you're targeting PHP7 you still need a polyfill (for runtime support), and if you're targeting PHP8 the interface would be discovered via reflection.

Cross-checking from older to newer version (e.g. Psalm running on PHP7 checking PHP8 code) wasn't a supported scenario for Psalm last time I checked.

I imagine stub would be pointless too: if you're targeting PHP7 you still need a polyfill (for runtime support), and if you're targeting PHP8 the interface would be discovered via reflection.

interface Stringable
{
    /**
     * @return string
     */
    public function __toString();
}

this is the complete declaration shipped by that package which is compliant with the standardised one.

UPD: now I see the link to polyfill-php80 was also provided above 馃う鈥嶁檪

UPD 2: @weirdan and now I finally understood what you mean. It was a hard day, and a hard week, I can barely think :-S

Was this page helpful?
0 / 5 - 0 ratings