1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 12:55:26 +01:00
psalm/tests/ToStringTest.php

138 lines
3.9 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests;
class ToStringTest extends TestCase
{
use Traits\FileCheckerInvalidCodeParseTestTrait;
use Traits\FileCheckerValidCodeParseTestTrait;
2017-01-13 20:07:23 +01:00
/**
* @return array
2017-01-13 20:07:23 +01:00
*/
public function providerFileCheckerValidCodeParse()
{
return [
'validToString' => [
'<?php
class A {
function __toString() {
return "hello";
}
}
2017-05-27 02:05:57 +02:00
echo (new A);',
],
'inheritedToString' => [
'<?php
class A {
function __toString() {
return "hello";
}
}
class B {
function __toString() {
return "goodbye";
}
}
class C extends B {}
$c = new C();
echo (string) $c',
],
'goodCast' => [
'<?php
class A {
2018-01-11 21:50:45 +01:00
public function __toString(): string
{
return "hello";
}
}
2017-06-12 01:20:07 +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
/** @param A|string $b */
2018-01-11 21:50:45 +01:00
function barBar($b): void {}
2017-06-12 01:20:07 +02:00
fooFoo(new A());
2017-05-27 02:05:57 +02:00
barBar(new A());',
],
];
}
2017-01-13 20:07:23 +01:00
/**
* @return array
2017-01-13 20:07:23 +01:00
*/
public function providerFileCheckerInvalidCodeParse()
{
return [
'echoClass' => [
'<?php
class A {}
echo (new A);',
2017-05-27 02:05:57 +02:00
'error_message' => 'InvalidArgument',
],
'echoCastClass' => [
'<?php
class A {}
echo (string)(new A);',
'error_message' => 'InvalidCast',
],
'invalidToStringReturnType' => [
'<?php
class A {
2018-01-11 21:50:45 +01:00
function __toString(): void { }
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'InvalidToString',
],
'invalidInferredToStringReturnType' => [
'<?php
class A {
function __toString() { }
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'InvalidToString',
],
'implicitCost' => [
'<?php
class A {
2018-01-11 21:50:45 +01:00
public function __toString(): string
{
return "hello";
}
}
2017-06-12 01:20:07 +02:00
2018-01-11 21:50:45 +01:00
function fooFoo(string $b): void {}
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',
],
'implicitConcatenation' => [
'<?php
interface I {
public function __toString();
}
function takesI(I $i): void
{
$a = $i . "hello";
}',
'error_message' => 'ImplicitToStringCast',
[],
true
],
];
}
}