2016-12-09 18:48:02 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class ToStringTest extends TestCase
|
2016-12-09 18:48:02 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
|
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
2017-01-02 21:31:18 +01:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2017-01-13 20:07:23 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerFileCheckerValidCodeParse()
|
2016-12-09 18:48:02 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'validToString' => [
|
|
|
|
'<?php
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
function __toString() {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class B {
|
|
|
|
function __toString() {
|
|
|
|
return "goodbye";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class C extends B {}
|
|
|
|
|
|
|
|
$c = new C();
|
|
|
|
echo (string) $c',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'goodCast' => [
|
|
|
|
'<?php
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
$a = fopen("php://memory", "r");
|
|
|
|
if ($a === false) exit;
|
|
|
|
$b = (string) $a;',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2016-12-09 18:48:02 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2017-01-13 20:07:23 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerFileCheckerInvalidCodeParse()
|
2016-12-09 18:48:02 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'echoClass' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
echo (new A);',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidArgument',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-10-07 17:27:54 +02:00
|
|
|
'echoCastClass' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
echo (string)(new A);',
|
|
|
|
'error_message' => 'InvalidCast',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'invalidToStringReturnType' => [
|
|
|
|
'<?php
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
function __toString() { }
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidToString',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2018-08-28 23:42:39 +02:00
|
|
|
'implicitCastWithStrictTypes' => [
|
|
|
|
'<?php declare(strict_types=1);
|
|
|
|
class A {
|
|
|
|
public function __toString(): string
|
|
|
|
{
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function fooFoo(string $b): void {}
|
|
|
|
fooFoo(new A());',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
|
|
|
'implicitCast' => [
|
2017-04-25 05:45:02 +02:00
|
|
|
'<?php
|
|
|
|
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',
|
|
|
|
],
|
2017-06-12 01:20:07 +02:00
|
|
|
'implicitCastFromInterface' => [
|
|
|
|
'<?php
|
|
|
|
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-03-18 19:42:23 +01:00
|
|
|
'implicitConcatenation' => [
|
|
|
|
'<?php
|
|
|
|
interface I {
|
|
|
|
public function __toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesI(I $i): void
|
|
|
|
{
|
|
|
|
$a = $i . "hello";
|
|
|
|
}',
|
|
|
|
'error_message' => 'ImplicitToStringCast',
|
|
|
|
[],
|
|
|
|
true
|
|
|
|
],
|
2018-04-30 06:19:35 +02:00
|
|
|
'resourceCannotBeCoercedToString' => [
|
|
|
|
'<?php
|
|
|
|
function takesString(string $s) : void {}
|
|
|
|
$a = fopen("php://memory", "r");
|
|
|
|
takesString($a);',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-04-21 20:23:09 +02:00
|
|
|
}
|
2016-12-09 18:48:02 +01:00
|
|
|
}
|