mirror of
https://github.com/danog/psalm.git
synced 2024-11-30 04:39:00 +01:00
164 lines
4.9 KiB
PHP
164 lines
4.9 KiB
PHP
<?php
|
|
namespace Psalm\Tests;
|
|
|
|
class ToStringTest extends TestCase
|
|
{
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function providerFileCheckerValidCodeParse()
|
|
{
|
|
return [
|
|
'validToString' => [
|
|
'<?php
|
|
class A {
|
|
function __toString() {
|
|
return "hello";
|
|
}
|
|
}
|
|
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 {
|
|
public function __toString(): string
|
|
{
|
|
return "hello";
|
|
}
|
|
}
|
|
|
|
/** @param string|A $b */
|
|
function fooFoo($b): void {}
|
|
|
|
/** @param A|string $b */
|
|
function barBar($b): void {}
|
|
|
|
fooFoo(new A());
|
|
barBar(new A());',
|
|
],
|
|
'resourceToString' => [
|
|
'<?php
|
|
$a = fopen("php://memory", "r");
|
|
if ($a === false) exit;
|
|
$b = (string) $a;',
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function providerFileCheckerInvalidCodeParse()
|
|
{
|
|
return [
|
|
'echoClass' => [
|
|
'<?php
|
|
class A {}
|
|
echo (new A);',
|
|
'error_message' => 'InvalidArgument',
|
|
],
|
|
'echoCastClass' => [
|
|
'<?php
|
|
class A {}
|
|
echo (string)(new A);',
|
|
'error_message' => 'InvalidCast',
|
|
],
|
|
'invalidToStringReturnType' => [
|
|
'<?php
|
|
class A {
|
|
function __toString(): void { }
|
|
}',
|
|
'error_message' => 'InvalidToString',
|
|
],
|
|
'invalidInferredToStringReturnType' => [
|
|
'<?php
|
|
class A {
|
|
function __toString() { }
|
|
}',
|
|
'error_message' => 'InvalidToString',
|
|
],
|
|
'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' => [
|
|
'<?php
|
|
class A {
|
|
public function __toString(): string
|
|
{
|
|
return "hello";
|
|
}
|
|
}
|
|
|
|
function fooFoo(string $b): void {}
|
|
fooFoo(new A());',
|
|
'error_message' => 'ImplicitToStringCast',
|
|
],
|
|
'implicitCastFromInterface' => [
|
|
'<?php
|
|
interface I {
|
|
public function __toString();
|
|
}
|
|
|
|
function takesString(string $str): void { }
|
|
|
|
function takesI(I $i): void
|
|
{
|
|
takesString($i);
|
|
}',
|
|
'error_message' => 'ImplicitToStringCast',
|
|
],
|
|
'implicitConcatenation' => [
|
|
'<?php
|
|
interface I {
|
|
public function __toString();
|
|
}
|
|
|
|
function takesI(I $i): void
|
|
{
|
|
$a = $i . "hello";
|
|
}',
|
|
'error_message' => 'ImplicitToStringCast',
|
|
[],
|
|
true
|
|
],
|
|
'resourceCannotBeCoercedToString' => [
|
|
'<?php
|
|
function takesString(string $s) : void {}
|
|
$a = fopen("php://memory", "r");
|
|
takesString($a);',
|
|
'error_message' => 'InvalidArgument',
|
|
],
|
|
];
|
|
}
|
|
}
|