2016-12-09 18:48:02 +01:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2016-12-09 18:48:02 +01:00
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2021-12-04 21:55:53 +01:00
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class ToStringTest extends TestCase
|
2016-12-09 18:48:02 +01:00
|
|
|
{
|
2021-12-04 21:55:53 +01:00
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
2017-01-02 21:31:18 +01:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2016-12-09 18:48:02 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'validToString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {
|
|
|
|
function __toString() {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
echo (new A);',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2018-02-12 02:56:34 +01:00
|
|
|
'inheritedToString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-02-12 02:56:34 +01:00
|
|
|
class A {
|
|
|
|
function __toString() {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class B {
|
|
|
|
function __toString() {
|
|
|
|
return "goodbye";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class C extends B {}
|
|
|
|
|
|
|
|
$c = new C();
|
2018-09-17 18:15:45 +02:00
|
|
|
echo (string) $c;',
|
2018-02-12 02:56:34 +01:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'goodCast' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function __toString(): string
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
2017-06-12 01:20:07 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @param string|A $b */
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo($b): void {}
|
2017-06-12 01:20:07 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @param A|string $b */
|
2018-01-11 21:50:45 +01:00
|
|
|
function barBar($b): void {}
|
2017-06-12 01:20:07 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
fooFoo(new A());
|
2017-05-27 02:05:57 +02:00
|
|
|
barBar(new A());',
|
|
|
|
],
|
2018-04-30 06:19:35 +02:00
|
|
|
'resourceToString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-04-30 06:19:35 +02:00
|
|
|
$a = fopen("php://memory", "r");
|
|
|
|
if ($a === false) exit;
|
|
|
|
$b = (string) $a;',
|
|
|
|
],
|
2018-10-04 21:29:01 +02:00
|
|
|
'canBeObject' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-10-04 21:29:01 +02:00
|
|
|
class A {
|
|
|
|
public function __toString() {
|
|
|
|
return "A";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param string|object $s */
|
|
|
|
function foo($s) : void {}
|
|
|
|
|
|
|
|
foo(new A);',
|
|
|
|
],
|
2019-01-05 06:15:53 +01:00
|
|
|
'castArrayKey' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-05 06:15:53 +01:00
|
|
|
/**
|
|
|
|
* @param string[] $arr
|
|
|
|
*/
|
|
|
|
function foo(array $arr) : void {
|
|
|
|
if (!$arr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($arr as $i => $_) {}
|
|
|
|
|
|
|
|
echo (string) $i;
|
|
|
|
}',
|
|
|
|
],
|
2019-08-16 17:33:58 +02:00
|
|
|
'allowToStringAfterMethodExistsCheck' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-08-16 17:33:58 +02:00
|
|
|
function getString(object $value) : ?string {
|
|
|
|
if (method_exists($value, "__toString")) {
|
|
|
|
return (string) $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-08-16 17:33:58 +02:00
|
|
|
],
|
2020-01-30 04:28:40 +01:00
|
|
|
'refineToStringType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-01-30 04:28:40 +01:00
|
|
|
/** @psalm-return non-empty-string */
|
|
|
|
function doesCast() : string {
|
|
|
|
return (string) (new A());
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-return non-empty-string */
|
|
|
|
function callsToString() : string {
|
|
|
|
return (new A())->__toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
/** @psalm-return non-empty-string */
|
|
|
|
function __toString(): string {
|
|
|
|
return "ha";
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-01-30 04:28:40 +01:00
|
|
|
],
|
2020-04-27 02:04:34 +02:00
|
|
|
'intersectionCanBeString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-04-27 02:04:34 +02:00
|
|
|
interface EmptyInterface {}
|
|
|
|
|
|
|
|
class StringCastable implements EmptyInterface
|
|
|
|
{
|
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
return \'I am castable\';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function factory(): EmptyInterface
|
|
|
|
{
|
|
|
|
return new StringCastable();
|
|
|
|
}
|
|
|
|
|
|
|
|
$object = factory();
|
|
|
|
if (method_exists($object, \'__toString\')) {
|
|
|
|
$a = (string) $object;
|
|
|
|
echo $a;
|
2020-05-16 14:50:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_callable([$object, \'__toString\'])) {
|
|
|
|
$a = (string) $object;
|
|
|
|
echo $a;
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-10-04 06:17:16 +02:00
|
|
|
],
|
2020-10-12 17:35:14 +02:00
|
|
|
'PHP80-stringableInterface' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-10-04 06:17:16 +02:00
|
|
|
interface Foo extends Stringable {}
|
|
|
|
|
|
|
|
function takesString(string $s) : void {}
|
|
|
|
|
|
|
|
function takesFoo(Foo $foo) : void {
|
|
|
|
/** @psalm-suppress ImplicitToStringCast */
|
|
|
|
takesString($foo);
|
2020-05-16 14:50:43 +02:00
|
|
|
}
|
2020-10-04 06:17:16 +02:00
|
|
|
|
|
|
|
class FooImplementer implements Foo {
|
|
|
|
public function __toString() : string {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
takesFoo(new FooImplementer());',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
2022-12-18 17:15:15 +01:00
|
|
|
'php_version' => '8.0',
|
2020-04-27 02:04:34 +02:00
|
|
|
],
|
2020-10-27 20:41:04 +01:00
|
|
|
'implicitStringable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-10-27 20:41:04 +01:00
|
|
|
function foo(Stringable $s): void {}
|
|
|
|
|
|
|
|
class Bar {
|
|
|
|
public function __toString() {
|
|
|
|
return "foo";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foo(new Bar());',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0',
|
2020-10-27 20:41:04 +01:00
|
|
|
],
|
2021-05-05 07:22:29 +02:00
|
|
|
'toStringNever' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-05-05 07:22:29 +02:00
|
|
|
class B{
|
|
|
|
public function __toString() {
|
|
|
|
throw new BadMethodCallException("bad");
|
|
|
|
}
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
',
|
2021-05-05 07:22:29 +02:00
|
|
|
],
|
2021-06-22 01:55:27 +02:00
|
|
|
'toStringToImplode' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-06-22 01:55:27 +02:00
|
|
|
class Bar {
|
|
|
|
public function __toString() {
|
|
|
|
return "foo";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo implode(":", [new Bar()]);',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2016-12-09 18:48:02 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2016-12-09 18:48:02 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'echoClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {}
|
|
|
|
echo (new A);',
|
2020-06-29 21:06:11 +02:00
|
|
|
'error_message' => 'InvalidArgument',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-10-07 17:27:54 +02:00
|
|
|
'echoCastClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-10-07 17:27:54 +02:00
|
|
|
class A {}
|
|
|
|
echo (string)(new A);',
|
|
|
|
'error_message' => 'InvalidCast',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'invalidToStringReturnType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
function __toString(): void { }
|
2017-04-25 05:45:02 +02:00
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidToString',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'invalidInferredToStringReturnType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {
|
|
|
|
function __toString() { }
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidToString',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2020-10-28 03:29:49 +01:00
|
|
|
'invalidInferredToStringReturnTypeWithTruePhp8' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-10-28 03:29:49 +01:00
|
|
|
class A {
|
|
|
|
function __toString() {
|
|
|
|
/** @psalm-suppress InvalidReturnStatement */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidToString',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
2022-12-18 17:15:15 +01:00
|
|
|
'php_version' => '8.0',
|
2020-10-28 03:29:49 +01:00
|
|
|
],
|
2018-08-28 23:42:39 +02:00
|
|
|
'implicitCastWithStrictTypes' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php declare(strict_types=1);
|
2018-08-28 23:42:39 +02:00
|
|
|
class A {
|
|
|
|
public function __toString(): string
|
|
|
|
{
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function fooFoo(string $b): void {}
|
|
|
|
fooFoo(new A());',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
2019-05-29 20:22:15 +02:00
|
|
|
'implicitCastWithStrictTypesToEchoOrSprintf' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php declare(strict_types=1);
|
2019-05-29 20:22:15 +02:00
|
|
|
class A {
|
|
|
|
public function __toString(): string
|
|
|
|
{
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo(new A());
|
|
|
|
sprintf("hello *", new A());',
|
|
|
|
'error_message' => 'ImplicitToStringCast',
|
|
|
|
],
|
2018-08-28 23:42:39 +02:00
|
|
|
'implicitCast' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function __toString(): string
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
2017-06-12 01:20:07 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(string $b): void {}
|
2017-04-25 05:45:02 +02:00
|
|
|
fooFoo(new A());',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'ImplicitToStringCast',
|
2019-05-29 20:22:15 +02:00
|
|
|
],
|
|
|
|
'implicitCastToUnion' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-05-29 20:22:15 +02:00
|
|
|
class A {
|
|
|
|
public function __toString(): string
|
|
|
|
{
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param string|int $b */
|
|
|
|
function fooFoo($b): void {}
|
|
|
|
fooFoo(new A());',
|
|
|
|
'error_message' => 'ImplicitToStringCast',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-06-12 01:20:07 +02:00
|
|
|
'implicitCastFromInterface' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-06-12 01:20:07 +02:00
|
|
|
interface I {
|
|
|
|
public function __toString();
|
|
|
|
}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function takesString(string $str): void { }
|
2017-06-12 01:20:07 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function takesI(I $i): void
|
2017-06-12 01:20:07 +02:00
|
|
|
{
|
|
|
|
takesString($i);
|
|
|
|
}',
|
|
|
|
'error_message' => 'ImplicitToStringCast',
|
2017-06-20 20:38:13 +02:00
|
|
|
],
|
2018-04-30 06:19:35 +02:00
|
|
|
'resourceCannotBeCoercedToString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-04-30 06:19:35 +02:00
|
|
|
function takesString(string $s) : void {}
|
|
|
|
$a = fopen("php://memory", "r");
|
|
|
|
takesString($a);',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
2019-03-01 06:50:22 +01:00
|
|
|
'resourceOrFalseToString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-03-01 06:50:22 +01:00
|
|
|
$a = fopen("php://memory", "r");
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = [];
|
|
|
|
}
|
|
|
|
$b = (string) $a;',
|
2019-03-17 22:10:51 +01:00
|
|
|
'error_message' => 'PossiblyInvalidCast',
|
2019-03-01 06:50:22 +01:00
|
|
|
],
|
2019-03-17 21:31:56 +01:00
|
|
|
'cannotCastInsideString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-03-17 21:31:56 +01:00
|
|
|
class NotStringCastable {}
|
|
|
|
$object = new NotStringCastable();
|
|
|
|
echo "$object";',
|
|
|
|
'error_message' => 'InvalidCast',
|
|
|
|
],
|
2019-03-17 21:41:34 +01:00
|
|
|
'warnAboutNullableCast' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-03-17 21:41:34 +01:00
|
|
|
class ClassWithToString {
|
|
|
|
public function __toString(): string {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function maybeShow(?string $message): void {
|
|
|
|
if ($message !== null) {
|
|
|
|
echo $message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
maybeShow(new ClassWithToString());',
|
|
|
|
'error_message' => 'ImplicitToStringCast',
|
|
|
|
],
|
2019-05-06 16:06:05 +02:00
|
|
|
'possiblyInvalidCastOnIsSubclassOf' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-05-06 16:06:05 +02:00
|
|
|
class Foo {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $a
|
|
|
|
*/
|
|
|
|
function bar($a) : ?string {
|
|
|
|
/**
|
|
|
|
* @psalm-suppress MixedArgument
|
|
|
|
*/
|
|
|
|
if (is_subclass_of($a, Foo::class)) {
|
|
|
|
return "hello" . $a;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}',
|
|
|
|
'error_message' => 'PossiblyInvalidOperand',
|
|
|
|
],
|
2019-08-16 17:33:58 +02:00
|
|
|
'allowToStringAfterMethodExistsCheckWithTypo' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-08-16 17:33:58 +02:00
|
|
|
function getString(object $value) : ?string {
|
|
|
|
if (method_exists($value, "__toStrong")) {
|
|
|
|
return (string) $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidCast',
|
|
|
|
],
|
2020-06-12 23:25:46 +02:00
|
|
|
'alwaysEvaluateToStringVar' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-06-12 23:25:46 +02:00
|
|
|
/** @psalm-suppress UndefinedFunction */
|
|
|
|
fora((string) $address);',
|
|
|
|
'error_message' => 'UndefinedGlobalVariable',
|
|
|
|
],
|
2020-10-27 20:41:04 +01:00
|
|
|
'implicitStringableDisallowed' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-10-30 00:41:10 +01:00
|
|
|
interface Stringable {
|
|
|
|
function __toString() {}
|
|
|
|
}
|
2020-10-27 20:41:04 +01:00
|
|
|
function foo(Stringable $s): void {}
|
|
|
|
|
|
|
|
class Bar {
|
|
|
|
public function __toString() {
|
|
|
|
return "foo";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foo(new Bar());',
|
|
|
|
'error_message' => 'InvalidArgument',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '7.4',
|
2020-10-27 20:41:04 +01:00
|
|
|
],
|
2021-03-11 06:07:39 +01:00
|
|
|
'implicitCastInArray' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-03-11 06:07:39 +01:00
|
|
|
interface S {
|
|
|
|
public function __toString(): string;
|
|
|
|
}
|
|
|
|
/** @return array<array-key, string> */
|
|
|
|
function f(S $s): array {
|
|
|
|
return [$s];
|
|
|
|
}
|
|
|
|
',
|
2022-12-18 17:15:15 +01:00
|
|
|
'error_message' => 'ImplicitToStringCast',
|
2021-03-11 06:07:39 +01:00
|
|
|
],
|
|
|
|
'implicitCastInList' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-03-11 06:07:39 +01:00
|
|
|
interface S {
|
|
|
|
public function __toString(): string;
|
|
|
|
}
|
|
|
|
/** @return list<string> */
|
|
|
|
function f(S $s): array {
|
|
|
|
return [$s];
|
|
|
|
}
|
|
|
|
',
|
2022-12-18 17:15:15 +01:00
|
|
|
'error_message' => 'ImplicitToStringCast',
|
2021-03-11 06:07:39 +01:00
|
|
|
],
|
|
|
|
'implicitCastInTuple' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-03-11 06:07:39 +01:00
|
|
|
interface S {
|
|
|
|
public function __toString(): string;
|
|
|
|
}
|
2022-11-12 02:14:21 +01:00
|
|
|
/** @return array{string} */
|
2021-03-11 06:07:39 +01:00
|
|
|
function f(S $s): array {
|
|
|
|
return [$s];
|
|
|
|
}
|
|
|
|
',
|
2022-12-18 17:15:15 +01:00
|
|
|
'error_message' => 'ImplicitToStringCast',
|
2021-03-11 06:07:39 +01:00
|
|
|
],
|
|
|
|
'implicitCastInShape' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-03-11 06:07:39 +01:00
|
|
|
interface S {
|
|
|
|
public function __toString(): string;
|
|
|
|
}
|
2022-11-12 02:14:21 +01:00
|
|
|
/** @return array{0:string} */
|
2021-03-11 06:07:39 +01:00
|
|
|
function f(S $s): array {
|
|
|
|
return [$s];
|
|
|
|
}
|
|
|
|
',
|
2022-12-18 17:15:15 +01:00
|
|
|
'error_message' => 'ImplicitToStringCast',
|
2021-03-11 06:07:39 +01:00
|
|
|
],
|
|
|
|
'implicitCastInIterable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-03-11 06:07:39 +01:00
|
|
|
interface S {
|
|
|
|
public function __toString(): string;
|
|
|
|
}
|
|
|
|
/** @return iterable<int, string> */
|
|
|
|
function f(S $s) {
|
|
|
|
return [$s];
|
|
|
|
}
|
|
|
|
',
|
2022-12-18 17:15:15 +01:00
|
|
|
'error_message' => 'ImplicitToStringCast',
|
2021-03-11 06:07:39 +01:00
|
|
|
],
|
2021-10-20 19:54:32 +02:00
|
|
|
'implicitCastInToString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-10-20 19:54:32 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
final class A
|
|
|
|
{
|
|
|
|
public function __toString(): string
|
|
|
|
{
|
|
|
|
return new SplFileInfo("a");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
2022-12-18 17:15:15 +01:00
|
|
|
'error_message' => 'ImplicitToStringCast',
|
2021-10-20 19:54:32 +02:00
|
|
|
],
|
2022-09-16 14:19:26 +02:00
|
|
|
'toStringTypecastNonString' => [
|
2022-10-16 13:59:15 +02:00
|
|
|
'code' => '<?php
|
2022-09-16 14:19:26 +02:00
|
|
|
class A {
|
|
|
|
function __toString(): string {
|
|
|
|
return "ha";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$foo = new A();
|
|
|
|
echo (int) $foo;',
|
|
|
|
'error_message' => 'InvalidCast',
|
|
|
|
],
|
2022-09-20 10:59:46 +02:00
|
|
|
'riskyArrayToIntCast' => [
|
2022-10-16 13:59:15 +02:00
|
|
|
'code' => '<?php
|
2022-09-20 10:59:46 +02:00
|
|
|
echo (int) array();',
|
|
|
|
'error_message' => 'RiskyCast',
|
|
|
|
],
|
|
|
|
'riskyArrayToFloatCast' => [
|
2022-10-16 13:59:15 +02:00
|
|
|
'code' => '<?php
|
2022-09-20 10:59:46 +02:00
|
|
|
echo (float) array(\'hello\');',
|
|
|
|
'error_message' => 'RiskyCast',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-04-21 20:23:09 +02:00
|
|
|
}
|
2016-12-09 18:48:02 +01:00
|
|
|
}
|