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

Add more tests (interface contained by still faulty)

This commit is contained in:
Olle 2020-07-12 20:06:03 +00:00
parent 13fef43ec8
commit 88081d61b3

View File

@ -27,23 +27,86 @@ class IfThisIsTest extends TestCase
* @psalm-self-out I * @psalm-self-out I
* @return void * @return void
*/ */
public function convert() public function convert() {}
{
}
/** /**
* @psalm-if-this-is I * @psalm-if-this-is I
* @return void * @return void
*/ */
public function test() public function test() {}
{
}
} }
$f = new F(); $f = new F();
$f->convert(); $f->convert();
$f->test(); $f->test();
' '
],
'withTemplate' => [
'<?php
class Frozen {}
class Unfrozen {}
/**
* @template T of Frozen|Unfrozen
*/
class Foo
{
/**
* @var T
*/
private $state;
/**
* @param T $state
*/
public function __construct($state)
{
$this->state = $state;
}
/**
* @param string $name
* @param mixed $val
* @psalm-if-this-is Foo<Unfrozen>
* @return void
*/
public function set($name, $val)
{
}
/**
* @return Foo<Frozen>
*/
public function freeze()
{
/** @var Foo<Frozen> */
$f = clone $this;
return $f;
}
}
$f = new Foo(new Unfrozen());
$f->set("asd", 10);
'
],
'subclass' => [
'<?php
class G
{
/**
* @psalm-if-this-is G
* @return void
*/
public function test() {}
}
class F extends G
{
}
$f = new F();
$f->test();
'
] ]
]; ];
} }
@ -65,28 +128,67 @@ class IfThisIsTest extends TestCase
class F implements I class F implements I
{ {
/**
* @psalm-self-out I
* @return void
*/
public function convert()
{
}
/** /**
* @psalm-if-this-is I * @psalm-if-this-is I
* @return void * @return void
*/ */
public function test() public function test() {}
{
}
} }
$f = new F(); $f = new F();
$f->test(); $f->test();
', ',
'error_message' => 'IfThisIsMismatch' 'error_message' => 'IfThisIsMismatch'
] ],
'failsWithWrongTemplate' => [
'<?php
class Frozen {}
class Unfrozen {}
/**
* @template T of Frozen|Unfrozen
*/
class Foo
{
/**
* @var T
*/
private $state;
/**
* @param T $state
*/
public function __construct($state)
{
$this->state = $state;
}
/**
* @param string $name
* @param mixed $val
* @psalm-if-this-is Foo<Unfrozen>
* @return void
*/
public function set($name, $val) {}
/**
* @return Foo<Frozen>
*/
public function freeze()
{
/** @var Foo<Frozen> */
$f = clone $this;
return $f;
}
}
$f = new Foo(new Unfrozen());
$f->set("asd", 10);
$g = $f->freeze();
$g->set("asd", 20); // Fails
',
'error_message' => 'IfThisIsMismatch'
],
]; ];
} }
} }