2016-12-15 01:24:33 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2019-06-26 22:52:29 +02:00
|
|
|
use function class_exists;
|
|
|
|
use const DIRECTORY_SEPARATOR;
|
2019-07-05 22:24:00 +02:00
|
|
|
use Psalm\Context;
|
2016-12-15 01:24:33 +01:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class MethodSignatureTest extends TestCase
|
2016-12-15 01:24:33 +01:00
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
use Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
use Traits\InvalidCodeAnalysisTestTrait;
|
2017-02-09 23:49:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-05-14 21:44:46 +02:00
|
|
|
public function testExtendSoapClientWithDocblockTypes()
|
2017-02-09 23:49:13 +01:00
|
|
|
{
|
2017-04-11 22:02:18 +02:00
|
|
|
if (class_exists('SoapClient') === false) {
|
2017-04-25 05:45:02 +02:00
|
|
|
$this->markTestSkipped('Cannot run test, base class "SoapClient" does not exist!');
|
2017-05-25 04:07:49 +02:00
|
|
|
|
2017-04-11 22:02:18 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-04-25 05:45:02 +02:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class A extends SoapClient
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param string $function_name
|
|
|
|
* @param array<mixed> $arguments
|
|
|
|
* @param array<mixed> $options default null
|
2019-05-14 21:44:46 +02:00
|
|
|
* @param array|SoapHeader $input_headers default null
|
2017-07-25 22:11:02 +02:00
|
|
|
* @param array<mixed> $output_headers default null
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function __soapCall(
|
|
|
|
$function_name,
|
|
|
|
$arguments,
|
|
|
|
$options = [],
|
|
|
|
$input_headers = [],
|
|
|
|
&$output_headers = []
|
|
|
|
) {
|
2018-07-17 02:32:16 +02:00
|
|
|
return $_GET["foo"];
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
2019-05-14 21:44:46 +02:00
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testExtendSoapClientWithNoDocblockTypes()
|
|
|
|
{
|
|
|
|
if (class_exists('SoapClient') === false) {
|
|
|
|
$this->markTestSkipped('Cannot run test, base class "SoapClient" does not exist!');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2019-03-25 16:49:05 +01:00
|
|
|
|
2019-05-14 21:44:46 +02:00
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class C extends SoapClient
|
2019-03-25 16:49:05 +01:00
|
|
|
{
|
|
|
|
public function __soapCall(
|
2019-05-14 21:44:46 +02:00
|
|
|
string $function_name,
|
2019-03-25 16:49:05 +01:00
|
|
|
$arguments,
|
|
|
|
$options = [],
|
|
|
|
$input_headers = [],
|
|
|
|
&$output_headers = []
|
|
|
|
) {
|
|
|
|
return $_GET["foo"];
|
|
|
|
}
|
2019-05-14 21:44:46 +02:00
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
2019-03-25 16:49:05 +01:00
|
|
|
|
2019-05-14 21:44:46 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testExtendSoapClientWithParamType()
|
|
|
|
{
|
|
|
|
if (class_exists('SoapClient') === false) {
|
|
|
|
$this->markTestSkipped('Cannot run test, base class "SoapClient" does not exist!');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
2019-03-25 16:49:05 +01:00
|
|
|
class C extends SoapClient
|
|
|
|
{
|
|
|
|
public function __soapCall(
|
|
|
|
string $function_name,
|
|
|
|
$arguments,
|
|
|
|
$options = [],
|
|
|
|
$input_headers = [],
|
|
|
|
&$output_headers = []
|
|
|
|
) {
|
|
|
|
return $_GET["foo"];
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
}'
|
|
|
|
);
|
2017-02-09 23:49:13 +01:00
|
|
|
|
2018-01-21 16:22:04 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2017-02-09 23:49:13 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testMismatchingCovariantReturnIn73(): void
|
2019-11-27 06:19:36 +01:00
|
|
|
{
|
|
|
|
$this->expectExceptionMessage('MethodSignatureMismatch');
|
|
|
|
$this->expectException(\Psalm\Exception\CodeException::class);
|
|
|
|
|
|
|
|
$this->project_analyzer->setPhpVersion('7.3');
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
function foo(): C {
|
|
|
|
return new C();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class B extends A {
|
|
|
|
function foo(): D {
|
|
|
|
return new D();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class C {}
|
|
|
|
class D extends C {}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testMismatchingCovariantReturnIn74(): void
|
2019-11-27 06:19:36 +01:00
|
|
|
{
|
|
|
|
$this->project_analyzer->setPhpVersion('7.4');
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
function foo(): C {
|
|
|
|
return new C();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class B extends A {
|
|
|
|
function foo(): D {
|
|
|
|
return new D();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class C {}
|
|
|
|
class D extends C {}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testMismatchingCovariantReturnIn73WithSelf(): void
|
2019-11-27 06:19:36 +01:00
|
|
|
{
|
|
|
|
$this->expectExceptionMessage('MethodSignatureMismatch');
|
|
|
|
$this->expectException(\Psalm\Exception\CodeException::class);
|
|
|
|
|
|
|
|
$this->project_analyzer->setPhpVersion('7.3');
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
function foo(): self {
|
|
|
|
return new A();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class B extends A {
|
|
|
|
function foo(): self {
|
|
|
|
return new B();
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testMismatchingCovariantReturnIn74WithSelf(): void
|
2019-11-27 06:19:36 +01:00
|
|
|
{
|
|
|
|
$this->project_analyzer->setPhpVersion('7.4');
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
function foo(): self {
|
|
|
|
return new A();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class B extends A {
|
|
|
|
function foo(): self {
|
|
|
|
return new B();
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testMismatchingCovariantParamIn73(): void
|
2019-11-27 06:19:36 +01:00
|
|
|
{
|
|
|
|
$this->expectExceptionMessage('MethodSignatureMismatch');
|
|
|
|
$this->expectException(\Psalm\Exception\CodeException::class);
|
|
|
|
|
|
|
|
$this->project_analyzer->setPhpVersion('7.3');
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function foo(D $d) : void {}
|
|
|
|
}
|
|
|
|
class B extends A {
|
|
|
|
public function foo(C $c): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class C {}
|
|
|
|
class D extends C {}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testMismatchingCovariantParamIn74(): void
|
2019-11-27 06:19:36 +01:00
|
|
|
{
|
|
|
|
$this->project_analyzer->setPhpVersion('7.4');
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function foo(D $d) : void {}
|
|
|
|
}
|
|
|
|
class B extends A {
|
|
|
|
public function foo(C $c): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class C {}
|
|
|
|
class D extends C {}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
2017-02-09 23:49:13 +01:00
|
|
|
/**
|
2019-05-17 00:36:36 +02:00
|
|
|
* @return void
|
2017-02-09 23:49:13 +01:00
|
|
|
*/
|
2019-05-14 21:44:46 +02:00
|
|
|
public function testExtendDocblockParamTypeWithWrongDocblockParam()
|
2017-02-09 23:49:13 +01:00
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectExceptionMessage('ImplementedParamTypeMismatch');
|
|
|
|
$this->expectException(\Psalm\Exception\CodeException::class);
|
2017-04-11 22:02:18 +02:00
|
|
|
if (class_exists('SoapClient') === false) {
|
2017-04-25 05:45:02 +02:00
|
|
|
$this->markTestSkipped('Cannot run test, base class "SoapClient" does not exist!');
|
2017-05-25 04:07:49 +02:00
|
|
|
|
2017-04-11 22:02:18 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-04-25 05:45:02 +02:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class A extends SoapClient
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param string $function_name
|
|
|
|
* @param string $arguments
|
|
|
|
* @param array<mixed> $options default null
|
|
|
|
* @param array<mixed> $input_headers default null
|
|
|
|
* @param array<mixed> $output_headers default null
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2019-05-14 21:44:46 +02:00
|
|
|
public function __soapCall(
|
|
|
|
$function_name,
|
|
|
|
$arguments,
|
|
|
|
$options = [],
|
|
|
|
$input_headers = [],
|
|
|
|
&$output_headers = []
|
|
|
|
) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
2019-05-17 00:36:36 +02:00
|
|
|
public function testExtendDocblockParamTypeWithWrongParam() : void
|
2019-05-14 21:44:46 +02:00
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectException(\Psalm\Exception\CodeException::class);
|
|
|
|
$this->expectExceptionMessage('MethodSignatureMismatch');
|
|
|
|
|
2019-05-14 21:44:46 +02:00
|
|
|
if (class_exists('SoapClient') === false) {
|
|
|
|
$this->markTestSkipped('Cannot run test, base class "SoapClient" does not exist!');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class A extends SoapClient
|
|
|
|
{
|
2017-07-25 22:11:02 +02:00
|
|
|
public function __soapCall(
|
|
|
|
$function_name,
|
|
|
|
string $arguments,
|
|
|
|
$options = [],
|
|
|
|
$input_headers = [],
|
|
|
|
&$output_headers = []
|
|
|
|
) {
|
2017-02-09 23:49:13 +01:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
2018-01-21 16:22:04 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2017-02-09 23:49:13 +01:00
|
|
|
}
|
2017-04-25 05:45:02 +02:00
|
|
|
|
2017-11-09 03:27:23 +01:00
|
|
|
/**
|
2019-03-01 21:55:20 +01:00
|
|
|
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
|
2017-11-09 03:27:23 +01:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2017-11-09 03:27:23 +01:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'privateArgs' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
private function foo(): void {}
|
2017-11-09 03:27:23 +01:00
|
|
|
}
|
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
private function foo(int $arg): void {}
|
2017-11-09 03:27:23 +01:00
|
|
|
}',
|
|
|
|
],
|
2017-11-13 22:31:33 +01:00
|
|
|
'nullableSubclassParam' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(string $s): ?string {
|
2017-11-29 05:09:09 +01:00
|
|
|
return rand(0, 1) ? $s : null;
|
2017-11-13 22:31:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(?string $s): string {
|
2017-11-13 22:31:33 +01:00
|
|
|
return $s ?: "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo (new B)->foo(null);',
|
|
|
|
],
|
|
|
|
'nullableSubclassParamWithDefault' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(string $s): string {
|
2017-11-13 22:31:33 +01:00
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(string $s = null): string {
|
2017-11-13 22:31:33 +01:00
|
|
|
return $s ?: "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo (new B)->foo();',
|
|
|
|
],
|
2018-01-05 06:19:35 +01:00
|
|
|
'allowSubclassesForNonInheritedMethodParams' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function bar(): void {}
|
2018-01-05 06:19:35 +01:00
|
|
|
}
|
|
|
|
class C extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function bar(): void {}
|
2018-01-05 06:19:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @param B|C $a */
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(A $a): void {
|
2018-01-05 06:19:35 +01:00
|
|
|
$a->bar();
|
|
|
|
}',
|
|
|
|
],
|
2018-01-08 23:17:49 +01:00
|
|
|
'allowNoReturnInSubclassWithNullableReturnType' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @return ?int */
|
|
|
|
public function foo() {
|
|
|
|
if (rand(0, 1)) return 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function foo() {}
|
|
|
|
}',
|
|
|
|
],
|
2018-01-26 16:59:30 +01:00
|
|
|
'selfReturnShouldBeParent' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2020-03-24 23:58:15 +01:00
|
|
|
/** @return self */
|
|
|
|
public function foo() {
|
|
|
|
return new A();
|
|
|
|
}
|
2018-01-26 16:59:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
2020-03-24 23:58:15 +01:00
|
|
|
public function foo() {
|
|
|
|
return new A();
|
|
|
|
}
|
2018-01-26 16:59:30 +01:00
|
|
|
}',
|
|
|
|
],
|
2018-01-26 19:51:00 +01:00
|
|
|
'staticReturnShouldBeStatic' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2020-03-24 23:58:15 +01:00
|
|
|
/** @return static */
|
|
|
|
public static function foo() {
|
|
|
|
return new static();
|
|
|
|
}
|
2020-08-06 01:39:27 +02:00
|
|
|
|
|
|
|
final public function __construct() {}
|
2018-01-26 19:51:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
2020-03-24 23:58:15 +01:00
|
|
|
public static function foo() {
|
|
|
|
return new static();
|
|
|
|
}
|
2018-01-26 19:51:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$b = B::foo();',
|
|
|
|
'assertions' => [
|
|
|
|
'$b' => 'B',
|
|
|
|
],
|
|
|
|
],
|
2018-02-01 01:24:34 +01:00
|
|
|
'allowSomeCovariance' => [
|
2018-01-30 04:29:49 +01:00
|
|
|
'<?php
|
|
|
|
interface I1 {
|
|
|
|
public function test(string $s) : ?string;
|
|
|
|
public function testIterable(array $a) : ?iterable;
|
|
|
|
}
|
|
|
|
|
|
|
|
class A1 implements I1 {
|
|
|
|
public function test(?string $s) : string {
|
|
|
|
return "value";
|
|
|
|
}
|
|
|
|
public function testIterable(?iterable $i) : array {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-02-01 01:24:34 +01:00
|
|
|
'allowVoidToNullConversion' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @return ?string */
|
|
|
|
public function foo() {
|
|
|
|
return rand(0, 1) ? "hello" : null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function foo(): void {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class C extends A {
|
|
|
|
/** @return void */
|
|
|
|
public function foo() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class D extends A {
|
|
|
|
/** @return null */
|
|
|
|
public function foo() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-02-21 17:57:52 +01:00
|
|
|
'allowNoChildClassPropertyWhenMixed' => [
|
|
|
|
'<?php
|
|
|
|
class A implements Serializable {
|
|
|
|
/** @var int */
|
|
|
|
private $id = 1;
|
|
|
|
|
2019-05-14 21:44:46 +02:00
|
|
|
/**
|
|
|
|
* @param string $serialized
|
|
|
|
*/
|
|
|
|
public function unserialize($serialized) : void
|
2018-02-21 17:57:52 +01:00
|
|
|
{
|
|
|
|
[
|
|
|
|
$this->id,
|
|
|
|
] = (array) \unserialize((string) $serialized);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function serialize() : string
|
|
|
|
{
|
|
|
|
return serialize([$this->id]);
|
|
|
|
}
|
2018-02-22 00:59:31 +01:00
|
|
|
}',
|
2018-02-21 17:57:52 +01:00
|
|
|
],
|
2018-02-22 19:42:34 +01:00
|
|
|
'clashWithCallMapClass' => [
|
|
|
|
'<?php
|
2018-04-07 17:38:41 +02:00
|
|
|
class HaruDestination {}
|
2018-02-22 19:42:34 +01:00
|
|
|
class AClass
|
|
|
|
{
|
2018-04-07 17:38:41 +02:00
|
|
|
public function get(): HaruDestination
|
2018-02-22 19:42:34 +01:00
|
|
|
{
|
2018-04-07 17:38:41 +02:00
|
|
|
return new HaruDestination;
|
2018-02-22 19:42:34 +01:00
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-03-13 23:11:57 +01:00
|
|
|
'classWithTraitExtendsNonAbstractWithMethod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
trait T {
|
|
|
|
abstract public function foo() : void;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
use T;
|
|
|
|
}',
|
|
|
|
],
|
2018-09-20 05:12:35 +02:00
|
|
|
'inheritsSplClasses' => [
|
|
|
|
'<?php
|
|
|
|
namespace App;
|
|
|
|
|
|
|
|
use SplObserver;
|
|
|
|
use SplSubject;
|
|
|
|
|
|
|
|
class Observer implements \SplObserver
|
|
|
|
{
|
|
|
|
public function update(SplSubject $subject)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Subject implements \SplSubject
|
|
|
|
{
|
|
|
|
public function attach(SplObserver $observer)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function detach(SplObserver $observer)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function notify()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-10-06 20:00:45 +02:00
|
|
|
'noMixedIssueWhenInheritParamTypes' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @param string $bar
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function foo($bar) {
|
|
|
|
echo $bar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function foo($bar) {
|
|
|
|
echo "hello " . $bar;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-01-05 14:43:37 +01:00
|
|
|
'inheritDocumentedSelf' => [
|
|
|
|
'<?php
|
|
|
|
interface I {
|
|
|
|
/**
|
|
|
|
* @param self $f
|
|
|
|
*/
|
|
|
|
public function foo(self $f) : self;
|
|
|
|
}
|
|
|
|
|
|
|
|
class C implements I {
|
|
|
|
public function foo(I $i) : I {
|
|
|
|
return new C();
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-01-05 14:43:37 +01:00
|
|
|
],
|
2019-01-16 21:08:11 +01:00
|
|
|
'allowInterfaceImplementation' => [
|
|
|
|
'<?php
|
|
|
|
abstract class A {
|
|
|
|
/** @return static */
|
|
|
|
public function foo() {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface I {
|
|
|
|
/** @return I */
|
|
|
|
public function foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
class C extends A implements I {}',
|
|
|
|
],
|
2019-01-19 18:42:46 +01:00
|
|
|
'enforceParameterInheritanceWithInheritDocAndParam' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
class X {
|
|
|
|
/**
|
|
|
|
* @param B $class
|
|
|
|
*/
|
|
|
|
public function boo(A $class): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Y extends X {
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
* @param A $class
|
|
|
|
*/
|
|
|
|
public function boo(A $class): void {}
|
|
|
|
}
|
|
|
|
|
2019-03-08 00:04:02 +01:00
|
|
|
class Z extends X {
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @param A $class
|
|
|
|
*/
|
|
|
|
public function boo(A $class): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
(new Y())->boo(new A());
|
|
|
|
(new Z())->boo(new A());',
|
2019-01-19 18:42:46 +01:00
|
|
|
],
|
2019-01-24 21:03:13 +01:00
|
|
|
'allowMixedExtensionOfIteratorAggregate' => [
|
|
|
|
'<?php
|
|
|
|
class C implements IteratorAggregate {
|
|
|
|
public function getIterator(): Iterator {
|
|
|
|
return new ArrayIterator([]);
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-01-24 21:03:13 +01:00
|
|
|
],
|
2019-03-02 21:26:18 +01:00
|
|
|
'allowExtraVariadic' => [
|
|
|
|
'<?php
|
|
|
|
interface I {
|
|
|
|
public function f(string $a, int $b): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
class C implements I {
|
|
|
|
public function f(string $a = "a", int $b = 1, float ...$rest): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
(new C)->f();
|
|
|
|
(new C)->f("b");
|
|
|
|
(new C)->f("b", 3);
|
|
|
|
(new C)->f("b", 3, 0.5);
|
2019-03-23 19:27:54 +01:00
|
|
|
(new C)->f("b", 3, 0.5, 0.8);',
|
2019-03-02 21:26:18 +01:00
|
|
|
],
|
2019-05-09 16:41:30 +02:00
|
|
|
'allowLessSpecificDocblockTypeOnParent' => [
|
|
|
|
'<?php
|
|
|
|
abstract class Foo {
|
|
|
|
/**
|
|
|
|
* @return array|string
|
|
|
|
*/
|
|
|
|
abstract public function getTargets();
|
|
|
|
}
|
|
|
|
|
|
|
|
class Bar extends Foo {
|
|
|
|
public function getTargets(): string {
|
|
|
|
return "baz";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = (new Bar)->getTargets();',
|
|
|
|
[
|
|
|
|
'$a' => 'string',
|
2019-07-05 22:24:00 +02:00
|
|
|
],
|
2019-05-09 16:41:30 +02:00
|
|
|
],
|
2019-05-25 17:51:09 +02:00
|
|
|
'parentIsKnown' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function returnSelf() : self {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function returnSelf() : parent {
|
|
|
|
return parent::returnSelf();
|
|
|
|
}
|
|
|
|
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'returnStaticParent' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @return static
|
|
|
|
*/
|
|
|
|
public static function foo() {
|
|
|
|
return new static();
|
|
|
|
}
|
2020-08-06 01:39:27 +02:00
|
|
|
|
|
|
|
final public function __construct() {}
|
2019-05-25 17:51:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
/**
|
|
|
|
* @return static
|
|
|
|
*/
|
|
|
|
public static function foo() {
|
|
|
|
return parent::foo();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2020-01-24 17:15:35 +01:00
|
|
|
'selfInTraitAbstractIsFine' => [
|
|
|
|
'<?php
|
|
|
|
trait SomeTrait {
|
|
|
|
abstract public function a(self $b): self;
|
|
|
|
}
|
|
|
|
|
|
|
|
class SomeClass {
|
|
|
|
use SomeTrait;
|
|
|
|
|
|
|
|
public function a(self $b): self {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2020-04-03 21:11:37 +02:00
|
|
|
'allowMatchIn74' => [
|
|
|
|
'<?php
|
|
|
|
trait FooTrait {
|
|
|
|
/**
|
|
|
|
* @return static
|
|
|
|
*/
|
|
|
|
public function bar(): self {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface FooInterface {
|
|
|
|
/**
|
|
|
|
* @return static
|
|
|
|
*/
|
|
|
|
public function bar(): self;
|
|
|
|
}
|
|
|
|
|
|
|
|
class FooClass implements FooInterface {
|
|
|
|
use FooTrait;
|
|
|
|
}',
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
'7.4'
|
|
|
|
],
|
2020-04-20 23:17:47 +02:00
|
|
|
'allowOverridingThrowable' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @psalm-immutable
|
|
|
|
*/
|
|
|
|
interface MyException extends \Throwable
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Informative comment
|
|
|
|
*/
|
|
|
|
public function getMessage(): string;
|
|
|
|
public function getCode();
|
|
|
|
public function getFile(): string;
|
|
|
|
public function getLine(): int;
|
|
|
|
public function getTrace(): array;
|
|
|
|
public function getPrevious(): ?\Throwable;
|
|
|
|
public function getTraceAsString(): string;
|
|
|
|
}'
|
2020-07-02 20:09:56 +02:00
|
|
|
],
|
|
|
|
'allowExecptionToStringWithNoType' => [
|
|
|
|
'<?php
|
|
|
|
class E extends Exception {
|
|
|
|
public function __toString() {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
|
|
|
'allowExecptionToStringIn71' => [
|
|
|
|
'<?php
|
|
|
|
class E extends Exception {
|
|
|
|
public function __toString() : string {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
'7.1'
|
2020-08-06 01:39:27 +02:00
|
|
|
],
|
|
|
|
'consistentConstructor' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @psalm-consistent-constructor
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
public function getInstance() : self {
|
|
|
|
return new static();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class AChild extends A {
|
|
|
|
public function __construct() {}
|
|
|
|
}'
|
|
|
|
],
|
2020-10-04 05:34:29 +02:00
|
|
|
'allowStaticInheritance' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function method(): static {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class B extends A {
|
|
|
|
public function method(): static {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
'8.0'
|
|
|
|
],
|
2017-11-09 03:27:23 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
2019-03-01 21:55:20 +01:00
|
|
|
* @return iterable<string,array{string,error_message:string,2?:string[],3?:bool,4?:string}>
|
2017-04-25 05:45:02 +02:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'moreArguments' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(int $a, bool $b): void {
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(int $a, bool $b, array $c): void {
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}',
|
2018-07-14 02:17:29 +02:00
|
|
|
'error_message' => 'Method B::fooFoo has more required parameters than parent method A::fooFoo',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'fewerArguments' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(int $a, bool $b): void {
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(int $a): void {
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}',
|
2018-07-14 02:17:29 +02:00
|
|
|
'error_message' => 'Method B::fooFoo has fewer parameters than parent method A::fooFoo',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2020-08-10 15:58:43 +02:00
|
|
|
'differentArgumentTypes' => [
|
2017-04-25 05:45:02 +02:00
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(int $a, bool $b): void {
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A {
|
2020-08-10 15:58:43 +02:00
|
|
|
public function fooFoo(int $a, int $b): void {
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}',
|
2020-08-10 15:58:43 +02:00
|
|
|
'error_message' => 'Argument 2 of B::fooFoo has wrong type \'int\', expecting \'bool\' as defined ' .
|
2018-01-11 23:38:24 +01:00
|
|
|
'by A::fooFoo',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2020-08-10 15:58:43 +02:00
|
|
|
'differentArgumentNames' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function fooFoo(int $a, bool $b): void {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function fooFoo(int $a, bool $c): void {
|
|
|
|
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'ParamNameMismatch',
|
|
|
|
],
|
2017-11-13 22:31:33 +01:00
|
|
|
'nonNullableSubclassParam' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(?string $s): string {
|
2017-11-13 22:31:33 +01:00
|
|
|
return $s ?: "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(string $s): string {
|
2017-11-13 22:31:33 +01:00
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
}',
|
2019-10-17 07:14:33 +02:00
|
|
|
'error_message' => 'Argument 1 of B::foo has wrong type \'string\', expecting \'null|string\' as',
|
2017-11-13 22:31:33 +01:00
|
|
|
],
|
2017-12-30 16:54:01 +01:00
|
|
|
'misplacedRequiredParam' => [
|
|
|
|
'<?php
|
2019-06-07 15:25:03 +02:00
|
|
|
function foo(string $bar = null, int $bat): void {}
|
|
|
|
foo();',
|
|
|
|
'error_message' => 'TooFewArguments',
|
2017-12-30 16:54:01 +01:00
|
|
|
],
|
2018-01-04 20:01:17 +01:00
|
|
|
'clasginByRef' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(string $a): void {
|
2018-01-04 20:01:17 +01:00
|
|
|
echo $a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(string &$a): void {
|
2018-01-04 20:01:17 +01:00
|
|
|
echo $a;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
|
|
|
],
|
2018-01-05 06:19:35 +01:00
|
|
|
'disallowSubclassesForNonInheritedMethodParams' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function bar(): void {}
|
2018-01-05 06:19:35 +01:00
|
|
|
}
|
|
|
|
class C extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function bar(): void {}
|
2018-01-05 06:19:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class D {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(A $a): void {}
|
2018-01-05 06:19:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class E extends D {
|
|
|
|
/** @param B|C $a */
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(A $a): void {
|
2018-01-05 06:19:35 +01:00
|
|
|
$a->bar();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MoreSpecificImplementedParamType',
|
|
|
|
],
|
2018-02-01 01:24:34 +01:00
|
|
|
'disallowVoidToNullConversionSignature' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function foo(): ?string {
|
|
|
|
return rand(0, 1) ? "hello" : null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function foo(): void {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
|
|
|
],
|
2018-03-13 17:52:00 +01:00
|
|
|
'abstractExtendsNonAbstractWithMethod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class B extends A {
|
|
|
|
abstract public function foo() : void;
|
|
|
|
}',
|
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
|
|
|
],
|
2018-03-13 23:11:57 +01:00
|
|
|
'traitReturnTypeMismatch' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
trait T {
|
|
|
|
abstract public function foo() : string;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
use T;
|
|
|
|
}',
|
2020-01-17 04:36:30 +01:00
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
2019-01-13 19:27:07 +01:00
|
|
|
],
|
|
|
|
'abstractTraitMethodWithDifferentReturnType' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
trait T {
|
|
|
|
abstract public function foo() : A;
|
|
|
|
}
|
|
|
|
|
|
|
|
class C {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
public function foo() : B{
|
|
|
|
return new B();
|
|
|
|
}
|
|
|
|
}',
|
2019-03-29 19:09:06 +01:00
|
|
|
'error_message' => 'TraitMethodSignatureMismatch',
|
|
|
|
],
|
|
|
|
'traitMoreParams' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
trait T {
|
|
|
|
abstract public function foo(string $s) : string;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
use T;
|
|
|
|
}',
|
2020-01-17 04:36:30 +01:00
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
2019-03-29 19:09:06 +01:00
|
|
|
],
|
|
|
|
'abstractTraitMethodWithDifferentParamType' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
trait T {
|
|
|
|
abstract public function foo(A $a) : void;
|
|
|
|
}
|
|
|
|
|
|
|
|
class C {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
public function foo(B $b) : void {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'TraitMethodSignatureMismatch',
|
2018-03-13 23:11:57 +01:00
|
|
|
],
|
2018-04-21 21:59:16 +02:00
|
|
|
'mustOmitReturnType' => [
|
|
|
|
'<?php
|
|
|
|
class A
|
|
|
|
{
|
|
|
|
public function __construct(): void
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MethodSignatureMustOmitReturnType',
|
|
|
|
],
|
2018-07-14 02:17:29 +02:00
|
|
|
'requireParam' => [
|
|
|
|
'<?php
|
|
|
|
interface I {
|
|
|
|
function foo(bool $b = false): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
class C implements I {
|
|
|
|
public function foo(bool $b): void {}
|
|
|
|
}',
|
2019-02-27 22:00:44 +01:00
|
|
|
'error_message' => 'MethodSignatureMismatch - src' . DIRECTORY_SEPARATOR . 'somefile.php:6:27 - Method C::foo has more required',
|
2018-07-14 02:17:29 +02:00
|
|
|
],
|
2018-10-06 20:00:45 +02:00
|
|
|
'inheritParamTypes' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @param string $bar
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function foo($bar) {
|
|
|
|
echo $bar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function foo($bar) {
|
|
|
|
echo "hello " . $bar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(new B)->foo(new stdClass);',
|
2019-03-23 19:27:54 +01:00
|
|
|
'error_message' => 'InvalidArgument',
|
2018-10-06 20:00:45 +02:00
|
|
|
],
|
2018-12-13 04:35:27 +01:00
|
|
|
'interfaceHasFewerConstructorArgs' => [
|
|
|
|
'<?php
|
|
|
|
interface Foo {
|
|
|
|
public function __construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
class Bar implements Foo {
|
|
|
|
public function __construct(bool $foo) {}
|
|
|
|
}',
|
2020-08-10 18:26:25 +02:00
|
|
|
'error_message' => 'ConstructorSignatureMismatch',
|
2018-12-13 04:35:27 +01:00
|
|
|
],
|
2019-01-19 18:19:07 +01:00
|
|
|
'enforceParameterInheritanceWithInheritDoc' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
class X {
|
|
|
|
/**
|
|
|
|
* @param B $class
|
|
|
|
*/
|
|
|
|
public function boo(A $class): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Y extends X {
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function boo(A $class): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
(new Y())->boo(new A());',
|
2019-04-26 00:02:19 +02:00
|
|
|
'error_message' => 'ArgumentTypeCoercion',
|
2019-01-19 18:19:07 +01:00
|
|
|
],
|
2019-03-08 00:04:02 +01:00
|
|
|
'enforceParameterInheritanceWithCapitalizedInheritDoc' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
class X {
|
|
|
|
/**
|
|
|
|
* @param B $class
|
|
|
|
*/
|
|
|
|
public function boo(A $class): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Y extends X {
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function boo(A $class): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
(new Y())->boo(new A());',
|
2019-04-26 00:02:19 +02:00
|
|
|
'error_message' => 'ArgumentTypeCoercion',
|
2019-03-08 00:04:02 +01:00
|
|
|
],
|
2019-01-19 18:19:07 +01:00
|
|
|
'warnAboutMismatchingClassParamDoc' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
class X {
|
|
|
|
/**
|
|
|
|
* @param B $class
|
|
|
|
*/
|
|
|
|
public function boo(A $class): void {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MismatchingDocblockParamType',
|
|
|
|
],
|
|
|
|
'warnAboutMismatchingInterfaceParamDoc' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
interface X {
|
|
|
|
/**
|
|
|
|
* @param B $class
|
|
|
|
*/
|
|
|
|
public function boo(A $class): void {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MismatchingDocblockParamType',
|
2019-01-24 16:58:59 +01:00
|
|
|
],
|
|
|
|
'interfaceInsertDocblockTypes' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {}
|
|
|
|
class Bar {}
|
|
|
|
|
|
|
|
interface I {
|
|
|
|
/** @return array<int, Foo> */
|
|
|
|
public function getFoos() : array;
|
|
|
|
}
|
|
|
|
|
|
|
|
class A implements I {
|
|
|
|
public function getFoos() : array {
|
|
|
|
return [new Bar()];
|
|
|
|
}
|
|
|
|
}',
|
2019-01-24 17:55:59 +01:00
|
|
|
'error_message' => 'InvalidReturnStatement',
|
|
|
|
],
|
|
|
|
'classInsertDocblockTypesFromParent' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {}
|
|
|
|
class Bar {}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
/** @return array<int, Foo> */
|
|
|
|
public function getFoos() : array {
|
|
|
|
return [new Foo()];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class A extends B {
|
|
|
|
public function getFoos() : array {
|
|
|
|
return [new Bar()];
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidReturnStatement',
|
2019-01-24 16:58:59 +01:00
|
|
|
],
|
2019-03-02 21:29:43 +01:00
|
|
|
'preventInterfaceOverload' => [
|
|
|
|
'<?php
|
|
|
|
interface I {
|
|
|
|
public function f(float ...$rest): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
class C implements I {
|
|
|
|
/** @param array<int,float> $f */
|
|
|
|
public function f($f): void {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
2019-07-05 22:24:00 +02:00
|
|
|
['MoreSpecificImplementedParamType'],
|
2019-03-02 21:29:43 +01:00
|
|
|
],
|
2019-05-14 22:15:31 +02:00
|
|
|
'preventOneOfUnionMoreSpecific' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @param string|int $s */
|
|
|
|
public function foo($s) : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
/** @param string $s */
|
|
|
|
public function foo($s) : void {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MoreSpecificImplementedParamType',
|
|
|
|
],
|
2019-05-14 21:44:46 +02:00
|
|
|
'preventImplementingSerializableWithType' => [
|
|
|
|
'<?php
|
|
|
|
class Foo implements \Serializable {
|
|
|
|
public function unserialize(string $serialized) {}
|
|
|
|
public function serialize() {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
|
|
|
],
|
|
|
|
'preventImplementingSerializableWithWrongDocblockType' => [
|
|
|
|
'<?php
|
|
|
|
class Foo implements \Serializable {
|
|
|
|
/** @param int $serialized */
|
|
|
|
public function unserialize($serialized) {}
|
|
|
|
public function serialize() {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'ImplementedParamTypeMismatch',
|
|
|
|
],
|
2019-11-11 06:21:43 +01:00
|
|
|
'returnsParentWithNoParent' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
public function f(): parent {}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'InvalidParent',
|
|
|
|
],
|
|
|
|
'returnsParentWithNoParentAndInvalidParentSuppressed' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
public function f(): parent {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'InvalidReturnType',
|
|
|
|
2 => ['InvalidParent'],
|
|
|
|
],
|
|
|
|
// not sure how to handle it
|
|
|
|
'SKIPPED-returnsParentWithNoParentAndInvalidParentSuppressedMismatchingReturn' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
public function f(): parent {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'InvalidReturnType',
|
|
|
|
2 => ['InvalidParent'],
|
|
|
|
],
|
2020-01-17 04:36:30 +01:00
|
|
|
'regularMethodMismatchFromParentUse' => [
|
|
|
|
'<?php
|
|
|
|
trait T2 {
|
|
|
|
abstract public function test(int $x) : void;
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class P2 {
|
|
|
|
use T2;
|
|
|
|
}
|
|
|
|
|
|
|
|
class C2 extends P2 {
|
|
|
|
public function test(string $x) : void {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
|
|
|
],
|
|
|
|
'regularMethodMismatchFromChildUse' => [
|
|
|
|
'<?php
|
|
|
|
trait T3 {
|
|
|
|
abstract public function test(int $x) : void;
|
|
|
|
}
|
|
|
|
|
|
|
|
class P3 {
|
|
|
|
public function test(string $x) : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class C3 extends P3 {
|
|
|
|
use T3;
|
|
|
|
}',
|
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
|
|
|
],
|
2020-01-17 06:28:08 +01:00
|
|
|
'traitMethodAccessLevel' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
trait T1 {
|
|
|
|
abstract protected static function test(A $x) : void;
|
|
|
|
}
|
|
|
|
|
|
|
|
class C1 {
|
|
|
|
use T1;
|
|
|
|
|
|
|
|
private static function test(B $x) : void {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'TraitMethodSignatureMismatch',
|
|
|
|
],
|
2020-01-21 17:46:51 +01:00
|
|
|
'abstractClassReturnMismatch' => [
|
|
|
|
'<?php
|
|
|
|
interface I {
|
|
|
|
function foo(): array;
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class C implements I {
|
|
|
|
public function foo(): void {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
|
|
|
],
|
|
|
|
'abstractClassParamMismatch' => [
|
|
|
|
'<?php
|
|
|
|
interface I {
|
|
|
|
function foo(int $s): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class C implements I {
|
|
|
|
public function foo(string $s): void {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
|
|
|
],
|
2020-04-03 21:11:37 +02:00
|
|
|
'preventTraitMatchIn73' => [
|
|
|
|
'<?php
|
|
|
|
trait FooTrait {
|
|
|
|
/**
|
|
|
|
* @return static
|
|
|
|
*/
|
|
|
|
public function bar(): self {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface FooInterface {
|
|
|
|
/**
|
|
|
|
* @return static
|
|
|
|
*/
|
|
|
|
public function bar(): self;
|
|
|
|
}
|
|
|
|
|
|
|
|
class FooClass implements FooInterface {
|
|
|
|
use FooTrait;
|
|
|
|
}',
|
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
|
|
|
[],
|
|
|
|
false,
|
|
|
|
'7.3'
|
|
|
|
],
|
2020-08-10 18:26:25 +02:00
|
|
|
'inconsistentConstructorExplicitParentConstructorArgCount' => [
|
2020-08-06 01:39:27 +02:00
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @psalm-consistent-constructor
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
public function getInstance() : self
|
|
|
|
{
|
|
|
|
return new static();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __construct() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class BadAChild extends A {
|
|
|
|
public function __construct(string $s) {}
|
|
|
|
}',
|
2020-08-10 18:26:25 +02:00
|
|
|
'error_message' => 'ConstructorSignatureMismatch',
|
|
|
|
],
|
|
|
|
'inconsistentConstructorExplicitParentConstructorType' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @psalm-consistent-constructor
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
public function getInstance() : self
|
|
|
|
{
|
|
|
|
return new static(5);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __construct(int $s) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class BadAChild extends A {
|
|
|
|
public function __construct(string $s) {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'ConstructorSignatureMismatch',
|
2020-08-06 01:39:27 +02:00
|
|
|
],
|
|
|
|
'inconsistentConstructorImplicitParentConstructor' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @psalm-consistent-constructor
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
public function getInstance() : self {
|
|
|
|
return new static();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class BadAChild extends A {
|
|
|
|
public function __construct(string $s) {}
|
|
|
|
}',
|
2020-08-10 18:26:25 +02:00
|
|
|
'error_message' => 'ConstructorSignatureMismatch',
|
2020-08-06 01:39:27 +02:00
|
|
|
],
|
2020-09-07 22:42:25 +02:00
|
|
|
'inheritDocblockReturnFromInterface' => [
|
|
|
|
'<?php
|
|
|
|
interface A {
|
|
|
|
/** @return ?string */
|
|
|
|
function foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
class C implements A {
|
|
|
|
public function foo() : ?string {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidReturnType',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
|
|
|
}
|
2016-12-15 01:24:33 +01:00
|
|
|
}
|