2019-09-25 16:39:16 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2019-09-25 16:39:16 +02:00
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2021-12-04 21:55:53 +01:00
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
|
2019-09-25 16:39:16 +02:00
|
|
|
class DocblockInheritanceTest extends TestCase
|
|
|
|
{
|
2021-12-04 21:55:53 +01:00
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
2019-09-25 16:39:16 +02:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2019-09-25 16:39:16 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'inheritParentReturnDocbblock' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-09-25 16:39:16 +02:00
|
|
|
class Foo {
|
|
|
|
/**
|
|
|
|
* @return int[]
|
|
|
|
*/
|
|
|
|
public function doFoo() {
|
|
|
|
return [1, 2, 3];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Bar extends Foo {
|
|
|
|
public function doFoo(): array {
|
|
|
|
return [4, 5, 6];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$b = (new Bar)->doFoo();',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [
|
2019-09-25 16:39:16 +02:00
|
|
|
'$b' => 'array<array-key, int>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'inheritedSelfAnnotation' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-09-25 16:39:16 +02:00
|
|
|
interface I {
|
|
|
|
/**
|
|
|
|
* @param self $i
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
function foo(self $i) : self;
|
|
|
|
}
|
|
|
|
|
|
|
|
class C implements I {
|
|
|
|
public function foo(I $i) : I {
|
|
|
|
return $i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function takeI(I $i) : I {
|
|
|
|
return (new C)->foo($i);
|
|
|
|
}',
|
|
|
|
],
|
2019-09-25 18:02:50 +02:00
|
|
|
'inheritTwice' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-09-25 18:02:50 +02:00
|
|
|
class Foo {
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function aa() {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Bar extends Foo {
|
|
|
|
public function aa() {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Baz extends Bar {
|
|
|
|
public function aa() {
|
|
|
|
return [];
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-09-25 18:02:50 +02:00
|
|
|
],
|
|
|
|
'inheritTwiceWithArrayType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-09-25 18:02:50 +02:00
|
|
|
class Foo {
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function aa() : array {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Bar extends Foo {
|
|
|
|
public function aa() : array {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Baz extends Bar {
|
|
|
|
public function aa() : array {
|
|
|
|
return [];
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-09-25 18:02:50 +02:00
|
|
|
],
|
2020-11-12 19:54:27 +01:00
|
|
|
'inheritCorrectReturnTypeOnInterface' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-11-12 19:54:27 +01:00
|
|
|
interface A {
|
|
|
|
/**
|
|
|
|
* @return A
|
|
|
|
*/
|
|
|
|
public function map(): A;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface B extends A {
|
|
|
|
/**
|
|
|
|
* @return B
|
|
|
|
*/
|
|
|
|
public function map(): A;
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesB(B $f) : B {
|
|
|
|
return $f->map();
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-11-12 19:54:27 +01:00
|
|
|
],
|
|
|
|
'inheritCorrectReturnTypeOnClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-11-12 19:54:27 +01:00
|
|
|
interface A {
|
|
|
|
/**
|
|
|
|
* @return A
|
|
|
|
*/
|
|
|
|
public function map(): A;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface B extends A {
|
|
|
|
/**
|
|
|
|
* @return B
|
|
|
|
*/
|
|
|
|
public function map(): A;
|
|
|
|
}
|
|
|
|
|
|
|
|
class F implements B {
|
|
|
|
public function map(): A {
|
|
|
|
return new F();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesF(F $f) : B {
|
|
|
|
return $f->map();
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-11-12 19:54:27 +01:00
|
|
|
],
|
2019-09-25 16:39:16 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2019-09-25 16:39:16 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'automaticInheritDoc' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-09-25 16:39:16 +02:00
|
|
|
class Y {
|
|
|
|
/**
|
|
|
|
* @param string[] $arr
|
|
|
|
*/
|
|
|
|
public function boo(array $arr) : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class X extends Y {
|
|
|
|
public function boo(array $arr) : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
(new X())->boo([1, 2]);',
|
2021-12-19 12:17:25 +01:00
|
|
|
'error_message' => 'InvalidArgument',
|
2019-09-25 16:39:16 +02:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|