1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Fix #4429 - any class with a __toString method in PHP8 assumed to implement Stringable

This commit is contained in:
Matt Brown 2020-10-27 15:41:04 -04:00 committed by Daniil Gentili
parent 7f975045f4
commit 106747487b
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 40 additions and 0 deletions

View File

@ -234,6 +234,16 @@ class ReflectorVisitor extends PhpParser\NodeVisitorAbstract implements FileSour
$this->functionlike_node_scanners[] = $functionlike_node_scanner; $this->functionlike_node_scanners[] = $functionlike_node_scanner;
if ($classlike_storage
&& !$classlike_storage->is_interface
&& $this->codebase->php_major_version >= 8
&& $node instanceof PhpParser\Node\Stmt\ClassMethod
&& strtolower($node->name->name) === '__tostring'
) {
$classlike_storage->class_implements['stringable'] = 'Stringable';
$this->codebase->scanner->queueClassLikeForScanning('Stringable');
}
if (!$this->scan_deep) { if (!$this->scan_deep) {
return PhpParser\NodeTraverser::DONT_TRAVERSE_CHILDREN; return PhpParser\NodeTraverser::DONT_TRAVERSE_CHILDREN;
} }

View File

@ -169,6 +169,21 @@ class ToStringTest extends TestCase
[], [],
'8.0' '8.0'
], ],
'implicitStringable' => [
'<?php
function foo(Stringable $s): void {}
class Bar {
public function __toString() {
return "foo";
}
}
foo(new Bar());',
[],
[],
'8.0',
],
]; ];
} }
@ -361,6 +376,21 @@ class ToStringTest extends TestCase
fora((string) $address);', fora((string) $address);',
'error_message' => 'UndefinedGlobalVariable', 'error_message' => 'UndefinedGlobalVariable',
], ],
'implicitStringableDisallowed' => [
'<?php
function foo(Stringable $s): void {}
class Bar {
public function __toString() {
return "foo";
}
}
foo(new Bar());',
'error_message' => 'InvalidArgument',
[],
'7.4',
],
]; ];
} }
} }