1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Restrict type of implode to prevent Array-to-string conversion;

This commit is contained in:
Matt Brown 2021-06-21 19:55:27 -04:00
parent fc88f4f0ee
commit d8f4eb2d6d
5 changed files with 18 additions and 0 deletions

View File

@ -43,6 +43,8 @@ class TypeTokenizer
'class-string' => true,
'callable-string' => true,
'callable-array' => true,
'callable-object' => true,
'stringable-object' => true,
'pure-callable' => true,
'pure-Closure' => true,
'trait-string' => true,

View File

@ -244,6 +244,9 @@ abstract class Atomic implements TypeNode
case 'callable-object':
return new TCallableObject();
case 'stringable-object':
return new Type\Atomic\TObjectWithProperties([], ['__tostring' => 'string']);
case 'class-string':
case 'interface-string':
return new TClassString();

View File

@ -567,6 +567,7 @@ function rtrim(string $str, string $character_mask = " \t\n\r\0\x0B") : string {
* @psalm-pure
*
* @param string|array $glue
* @param array<string|float|int|stringable-object> $pieces
*
* @return (
* $glue is non-empty-string

View File

@ -193,6 +193,16 @@ class ToStringTest extends TestCase
}
'
],
'toStringToImplode' => [
'<?php
class Bar {
public function __toString() {
return "foo";
}
}
echo implode(":", [new Bar()]);',
],
];
}

View File

@ -1531,6 +1531,7 @@ class UnusedVariableTest extends TestCase
takes_ref($a[$e]);
}
/** @param array<string|int> $p */
function takes_ref(array &$p): void {
echo implode(",", $p);
}'
@ -2381,6 +2382,7 @@ class UnusedVariableTest extends TestCase
],
'varDocblockVariableIsUsedByRef' => [
'<?php
/** @param array<string|int> $arr */
function foo(array $arr) : string {
/** @var string $val */
foreach ($arr as &$val) {