2016-12-14 19:24:33 -05:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2019-06-26 22:52:29 +02:00
|
|
|
use function class_exists;
|
|
|
|
use const DIRECTORY_SEPARATOR;
|
2019-07-05 16:24:00 -04:00
|
|
|
use Psalm\Context;
|
2016-12-14 19:24:33 -05:00
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
class MethodSignatureTest extends TestCase
|
2016-12-14 19:24:33 -05:00
|
|
|
{
|
2018-11-05 21:57:36 -05:00
|
|
|
use Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
use Traits\InvalidCodeAnalysisTestTrait;
|
2017-02-09 17:49:13 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-05-14 15:44:46 -04:00
|
|
|
public function testExtendSoapClientWithDocblockTypes()
|
2017-02-09 17:49:13 -05:00
|
|
|
{
|
2017-04-11 21:02:18 +01:00
|
|
|
if (class_exists('SoapClient') === false) {
|
2017-04-24 23:45:02 -04:00
|
|
|
$this->markTestSkipped('Cannot run test, base class "SoapClient" does not exist!');
|
2017-05-24 22:07:49 -04:00
|
|
|
|
2017-04-11 21:02:18 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-04-24 23:45:02 -04:00
|
|
|
|
2017-07-25 16:11:02 -04: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 15:44:46 -04:00
|
|
|
* @param array|SoapHeader $input_headers default null
|
2017-07-25 16:11:02 -04:00
|
|
|
* @param array<mixed> $output_headers default null
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function __soapCall(
|
|
|
|
$function_name,
|
|
|
|
$arguments,
|
|
|
|
$options = [],
|
|
|
|
$input_headers = [],
|
|
|
|
&$output_headers = []
|
|
|
|
) {
|
2018-07-16 20:32:16 -04:00
|
|
|
return $_GET["foo"];
|
2017-07-25 16:11:02 -04:00
|
|
|
}
|
2019-05-14 15:44:46 -04: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 11:49:05 -04:00
|
|
|
|
2019-05-14 15:44:46 -04:00
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class C extends SoapClient
|
2019-03-25 11:49:05 -04:00
|
|
|
{
|
|
|
|
public function __soapCall(
|
2019-05-14 15:44:46 -04:00
|
|
|
string $function_name,
|
2019-03-25 11:49:05 -04:00
|
|
|
$arguments,
|
|
|
|
$options = [],
|
|
|
|
$input_headers = [],
|
|
|
|
&$output_headers = []
|
|
|
|
) {
|
|
|
|
return $_GET["foo"];
|
|
|
|
}
|
2019-05-14 15:44:46 -04:00
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
2019-03-25 11:49:05 -04:00
|
|
|
|
2019-05-14 15:44:46 -04: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 11:49:05 -04:00
|
|
|
class C extends SoapClient
|
|
|
|
{
|
|
|
|
public function __soapCall(
|
|
|
|
string $function_name,
|
|
|
|
$arguments,
|
|
|
|
$options = [],
|
|
|
|
$input_headers = [],
|
|
|
|
&$output_headers = []
|
|
|
|
) {
|
|
|
|
return $_GET["foo"];
|
|
|
|
}
|
2017-07-25 16:11:02 -04:00
|
|
|
}'
|
|
|
|
);
|
2017-02-09 17:49:13 -05:00
|
|
|
|
2018-01-21 10:22:04 -05:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2017-02-09 17:49:13 -05:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testMismatchingCovariantReturnIn73(): void
|
2019-11-27 00:19:36 -05: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 00:19:36 -05: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 00:19:36 -05: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 00:19:36 -05: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 00:19:36 -05: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 00:19:36 -05: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 17:49:13 -05:00
|
|
|
/**
|
2019-05-16 18:36:36 -04:00
|
|
|
* @return void
|
2017-02-09 17:49:13 -05:00
|
|
|
*/
|
2019-05-14 15:44:46 -04:00
|
|
|
public function testExtendDocblockParamTypeWithWrongDocblockParam()
|
2017-02-09 17:49:13 -05:00
|
|
|
{
|
2019-05-16 18:36:36 -04:00
|
|
|
$this->expectExceptionMessage('ImplementedParamTypeMismatch');
|
|
|
|
$this->expectException(\Psalm\Exception\CodeException::class);
|
2017-04-11 21:02:18 +01:00
|
|
|
if (class_exists('SoapClient') === false) {
|
2017-04-24 23:45:02 -04:00
|
|
|
$this->markTestSkipped('Cannot run test, base class "SoapClient" does not exist!');
|
2017-05-24 22:07:49 -04:00
|
|
|
|
2017-04-11 21:02:18 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-04-24 23:45:02 -04:00
|
|
|
|
2017-07-25 16:11:02 -04: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 15:44:46 -04:00
|
|
|
public function __soapCall(
|
|
|
|
$function_name,
|
|
|
|
$arguments,
|
|
|
|
$options = [],
|
|
|
|
$input_headers = [],
|
|
|
|
&$output_headers = []
|
|
|
|
) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
2019-05-16 18:36:36 -04:00
|
|
|
public function testExtendDocblockParamTypeWithWrongParam() : void
|
2019-05-14 15:44:46 -04:00
|
|
|
{
|
2019-05-16 18:36:36 -04:00
|
|
|
$this->expectException(\Psalm\Exception\CodeException::class);
|
|
|
|
$this->expectExceptionMessage('MethodSignatureMismatch');
|
|
|
|
|
2019-05-14 15:44:46 -04: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 16:11:02 -04:00
|
|
|
public function __soapCall(
|
|
|
|
$function_name,
|
|
|
|
string $arguments,
|
|
|
|
$options = [],
|
|
|
|
$input_headers = [],
|
|
|
|
&$output_headers = []
|
|
|
|
) {
|
2017-02-09 17:49:13 -05:00
|
|
|
|
2017-07-25 16:11:02 -04:00
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
2018-01-21 10:22:04 -05:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2017-02-09 17:49:13 -05:00
|
|
|
}
|
2017-04-24 23:45:02 -04:00
|
|
|
|
2017-11-08 21:27:23 -05:00
|
|
|
/**
|
2019-03-01 22:55:20 +02:00
|
|
|
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
|
2017-11-08 21:27:23 -05:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2017-11-08 21:27:23 -05:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'privateArgs' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 15:50:45 -05:00
|
|
|
private function foo(): void {}
|
2017-11-08 21:27:23 -05:00
|
|
|
}
|
|
|
|
class B extends A {
|
2018-01-11 15:50:45 -05:00
|
|
|
private function foo(int $arg): void {}
|
2017-11-08 21:27:23 -05:00
|
|
|
}',
|
|
|
|
],
|
2017-11-13 16:31:33 -05:00
|
|
|
'nullableSubclassParam' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 15:50:45 -05:00
|
|
|
public function foo(string $s): ?string {
|
2017-11-28 23:09:09 -05:00
|
|
|
return rand(0, 1) ? $s : null;
|
2017-11-13 16:31:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
2018-01-11 15:50:45 -05:00
|
|
|
public function foo(?string $s): string {
|
2017-11-13 16:31:33 -05:00
|
|
|
return $s ?: "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo (new B)->foo(null);',
|
|
|
|
],
|
|
|
|
'nullableSubclassParamWithDefault' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 15:50:45 -05:00
|
|
|
public function foo(string $s): string {
|
2017-11-13 16:31:33 -05:00
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
2018-01-11 15:50:45 -05:00
|
|
|
public function foo(string $s = null): string {
|
2017-11-13 16:31:33 -05:00
|
|
|
return $s ?: "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo (new B)->foo();',
|
|
|
|
],
|
2018-01-05 00:19:35 -05:00
|
|
|
'allowSubclassesForNonInheritedMethodParams' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A {
|
2018-01-11 15:50:45 -05:00
|
|
|
public function bar(): void {}
|
2018-01-05 00:19:35 -05:00
|
|
|
}
|
|
|
|
class C extends A {
|
2018-01-11 15:50:45 -05:00
|
|
|
public function bar(): void {}
|
2018-01-05 00:19:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @param B|C $a */
|
2018-01-11 15:50:45 -05:00
|
|
|
function foo(A $a): void {
|
2018-01-05 00:19:35 -05:00
|
|
|
$a->bar();
|
|
|
|
}',
|
|
|
|
],
|
2018-01-08 17:17:49 -05: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 10:59:30 -05:00
|
|
|
'selfReturnShouldBeParent' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2020-03-24 18:58:15 -04:00
|
|
|
/** @return self */
|
|
|
|
public function foo() {
|
|
|
|
return new A();
|
|
|
|
}
|
2018-01-26 10:59:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
2020-03-24 18:58:15 -04:00
|
|
|
public function foo() {
|
|
|
|
return new A();
|
|
|
|
}
|
2018-01-26 10:59:30 -05:00
|
|
|
}',
|
|
|
|
],
|
2018-01-26 13:51:00 -05:00
|
|
|
'staticReturnShouldBeStatic' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2020-03-24 18:58:15 -04:00
|
|
|
/** @return static */
|
|
|
|
public static function foo() {
|
|
|
|
return new static();
|
|
|
|
}
|
2020-08-05 19:39:27 -04:00
|
|
|
|
|
|
|
final public function __construct() {}
|
2018-01-26 13:51:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
2020-03-24 18:58:15 -04:00
|
|
|
public static function foo() {
|
|
|
|
return new static();
|
|
|
|
}
|
2018-01-26 13:51:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$b = B::foo();',
|
|
|
|
'assertions' => [
|
|
|
|
'$b' => 'B',
|
|
|
|
],
|
|
|
|
],
|
2018-01-31 19:24:34 -05:00
|
|
|
'allowSomeCovariance' => [
|
2018-01-29 22:29:49 -05: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";
|
|
|
|
}
|
2021-05-09 01:33:48 +02:00
|
|
|
public function testIterable(?iterable $a) : array {
|
2018-01-29 22:29:49 -05:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-01-31 19:24:34 -05:00
|
|
|
'allowVoidToNullConversion' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2020-11-12 13:54:27 -05:00
|
|
|
/** @return ?string */
|
|
|
|
public function foo() {
|
|
|
|
return rand(0, 1) ? "hello" : null;
|
|
|
|
}
|
2018-01-31 19:24:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
2020-11-12 13:54:27 -05:00
|
|
|
public function foo(): void {
|
|
|
|
return;
|
|
|
|
}
|
2018-01-31 19:24:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
class C extends A {
|
2020-11-12 13:54:27 -05:00
|
|
|
/** @return void */
|
|
|
|
public function foo() {
|
|
|
|
return;
|
|
|
|
}
|
2018-01-31 19:24:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
class D extends A {
|
2020-11-12 13:54:27 -05:00
|
|
|
/** @return null */
|
|
|
|
public function foo() {
|
|
|
|
return null;
|
|
|
|
}
|
2018-01-31 19:24:34 -05:00
|
|
|
}',
|
|
|
|
],
|
2018-02-21 11:57:52 -05:00
|
|
|
'allowNoChildClassPropertyWhenMixed' => [
|
|
|
|
'<?php
|
|
|
|
class A implements Serializable {
|
|
|
|
/** @var int */
|
|
|
|
private $id = 1;
|
|
|
|
|
2019-05-14 15:44:46 -04:00
|
|
|
/**
|
|
|
|
* @param string $serialized
|
|
|
|
*/
|
|
|
|
public function unserialize($serialized) : void
|
2018-02-21 11:57:52 -05:00
|
|
|
{
|
|
|
|
[
|
|
|
|
$this->id,
|
2020-11-25 18:04:48 +01:00
|
|
|
] = (array) \unserialize($serialized);
|
2018-02-21 11:57:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function serialize() : string
|
|
|
|
{
|
|
|
|
return serialize([$this->id]);
|
|
|
|
}
|
2018-02-21 18:59:31 -05:00
|
|
|
}',
|
2018-02-21 11:57:52 -05:00
|
|
|
],
|
2018-02-22 13:42:34 -05:00
|
|
|
'clashWithCallMapClass' => [
|
|
|
|
'<?php
|
2018-04-07 11:38:41 -04:00
|
|
|
class HaruDestination {}
|
2018-02-22 13:42:34 -05:00
|
|
|
class AClass
|
|
|
|
{
|
2018-04-07 11:38:41 -04:00
|
|
|
public function get(): HaruDestination
|
2018-02-22 13:42:34 -05:00
|
|
|
{
|
2018-04-07 11:38:41 -04:00
|
|
|
return new HaruDestination;
|
2018-02-22 13:42:34 -05:00
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-03-13 18:11:57 -04:00
|
|
|
'classWithTraitExtendsNonAbstractWithMethod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
trait T {
|
|
|
|
abstract public function foo() : void;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
use T;
|
|
|
|
}',
|
|
|
|
],
|
2018-09-19 23:12:35 -04: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 14:00:45 -04: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 08:43:37 -05:00
|
|
|
'inheritDocumentedSelf' => [
|
|
|
|
'<?php
|
|
|
|
interface I {
|
|
|
|
/**
|
|
|
|
* @param self $f
|
|
|
|
*/
|
|
|
|
public function foo(self $f) : self;
|
|
|
|
}
|
|
|
|
|
|
|
|
class C implements I {
|
2021-05-09 01:33:48 +02:00
|
|
|
public function foo(I $f) : I {
|
2019-01-05 08:43:37 -05:00
|
|
|
return new C();
|
|
|
|
}
|
2019-03-23 14:27:54 -04:00
|
|
|
}',
|
2019-01-05 08:43:37 -05:00
|
|
|
],
|
2019-01-16 15:08:11 -05: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 12:42:46 -05: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-07 17:04:02 -06: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 12:42:46 -05:00
|
|
|
],
|
2019-01-24 15:03:13 -05:00
|
|
|
'allowMixedExtensionOfIteratorAggregate' => [
|
|
|
|
'<?php
|
|
|
|
class C implements IteratorAggregate {
|
|
|
|
public function getIterator(): Iterator {
|
|
|
|
return new ArrayIterator([]);
|
|
|
|
}
|
2019-03-23 14:27:54 -04:00
|
|
|
}',
|
2019-01-24 15:03:13 -05:00
|
|
|
],
|
2019-03-02 15:26:18 -05: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 14:27:54 -04:00
|
|
|
(new C)->f("b", 3, 0.5, 0.8);',
|
2019-03-02 15:26:18 -05:00
|
|
|
],
|
2019-05-09 10:41:30 -04: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 16:24:00 -04:00
|
|
|
],
|
2019-05-09 10:41:30 -04:00
|
|
|
],
|
2019-05-25 11:51:09 -04: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-05 19:39:27 -04:00
|
|
|
|
|
|
|
final public function __construct() {}
|
2019-05-25 11:51:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
/**
|
|
|
|
* @return static
|
|
|
|
*/
|
|
|
|
public static function foo() {
|
|
|
|
return parent::foo();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2020-01-24 11:15:35 -05: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 15:11:37 -04: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 14:09:56 -04: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-05 19:39:27 -04:00
|
|
|
],
|
|
|
|
'consistentConstructor' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @psalm-consistent-constructor
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
public function getInstance() : self {
|
|
|
|
return new static();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class AChild extends A {
|
|
|
|
public function __construct() {}
|
|
|
|
}'
|
|
|
|
],
|
2020-10-03 23:34:29 -04:00
|
|
|
'allowStaticInheritance' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function method(): static {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class B extends A {
|
|
|
|
public function method(): static {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
'8.0'
|
|
|
|
],
|
2020-12-02 15:13:28 -05:00
|
|
|
'suppressDocblockFinal' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @final
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
public function foo(): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-suppress InvalidExtendClass
|
|
|
|
*/
|
|
|
|
class B extends A {
|
|
|
|
/**
|
|
|
|
* @psalm-suppress MethodSignatureMismatch
|
|
|
|
*/
|
|
|
|
public function foo(): void {}
|
|
|
|
}'
|
|
|
|
],
|
2020-12-10 00:15:37 -05:00
|
|
|
'inheritParamTypeWhenSignatureReturnTypeChanged' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function __construct(string $s) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class AChild extends A {}
|
|
|
|
|
|
|
|
interface B {
|
|
|
|
/** @param string $data */
|
|
|
|
public function create($data): A;
|
|
|
|
}
|
|
|
|
|
|
|
|
class C implements B {
|
|
|
|
public function create($data): AChild {
|
|
|
|
return new AChild($data);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
'7.4'
|
|
|
|
],
|
2021-01-22 09:58:09 -05:00
|
|
|
'extendStaticReturnTypeInFinal' => [
|
|
|
|
'<?php
|
|
|
|
final class B extends A
|
|
|
|
{
|
|
|
|
public static function doCretate1(): self
|
|
|
|
{
|
|
|
|
return self::create1();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function doCretate2(): self
|
|
|
|
{
|
|
|
|
return self::create2();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class A
|
|
|
|
{
|
|
|
|
final private function __construct() {}
|
|
|
|
|
|
|
|
final protected static function create1(): static
|
|
|
|
{
|
|
|
|
return new static();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @return static */
|
|
|
|
final protected static function create2()
|
|
|
|
{
|
|
|
|
return new static();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
'8.0'
|
|
|
|
],
|
2021-02-23 19:06:45 -05:00
|
|
|
'notExtendedStaticReturntypeInFinal' => [
|
|
|
|
'<?php
|
|
|
|
final class X
|
|
|
|
{
|
|
|
|
public static function create(): static
|
|
|
|
{
|
|
|
|
return new self();
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2021-02-23 20:48:22 -05:00
|
|
|
'callParentMethodFromTrait' => [
|
|
|
|
'<?php
|
|
|
|
class MyParentClass
|
|
|
|
{
|
|
|
|
/** @return static */
|
|
|
|
public function myMethod()
|
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trait MyTrait
|
|
|
|
{
|
|
|
|
final public function myMethod() : self
|
|
|
|
{
|
|
|
|
return parent::myMethod();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyChildClass extends MyParentClass
|
|
|
|
{
|
|
|
|
use MyTrait;
|
|
|
|
}'
|
|
|
|
],
|
2021-05-15 02:23:54 +02:00
|
|
|
'MixedParamInImplementation' => [
|
|
|
|
'<?php
|
|
|
|
interface I
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param mixed $a
|
|
|
|
*/
|
|
|
|
public function a($a): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
final class B implements I
|
|
|
|
{
|
|
|
|
public function a(mixed $a): void {}
|
|
|
|
}'
|
|
|
|
],
|
2017-11-08 21:27:23 -05:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
/**
|
2021-03-19 20:44:44 -05:00
|
|
|
* @return iterable<string,array{string,error_message:string,1?:string[],2?:bool,3?:string}>
|
2017-04-24 23:45:02 -04:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2017-04-24 23:45:02 -04:00
|
|
|
{
|
|
|
|
return [
|
2021-05-09 01:33:48 +02:00
|
|
|
'oneParam' => [
|
|
|
|
'<?php
|
|
|
|
interface I {
|
|
|
|
/**
|
|
|
|
* @param array $i
|
|
|
|
*/
|
|
|
|
public function foo(array $i) : void;
|
|
|
|
}
|
|
|
|
|
|
|
|
class C implements I {
|
|
|
|
public function foo(array $c) : void {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'Argument 1 of C::foo has wrong name $c, expecting $i as defined by I::foo',
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
'moreArguments' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 15:50:45 -05:00
|
|
|
public function fooFoo(int $a, bool $b): void {
|
2017-07-25 16:11:02 -04:00
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
}
|
|
|
|
}
|
2017-07-25 16:11:02 -04:00
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
class B extends A {
|
2018-01-11 15:50:45 -05:00
|
|
|
public function fooFoo(int $a, bool $b, array $c): void {
|
2017-07-25 16:11:02 -04:00
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
}
|
|
|
|
}',
|
2018-07-13 20:17:29 -04:00
|
|
|
'error_message' => 'Method B::fooFoo has more required parameters than parent method A::fooFoo',
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
|
|
|
'fewerArguments' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 15:50:45 -05:00
|
|
|
public function fooFoo(int $a, bool $b): void {
|
2017-07-25 16:11:02 -04:00
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
}
|
|
|
|
}
|
2017-07-25 16:11:02 -04:00
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
class B extends A {
|
2018-01-11 15:50:45 -05:00
|
|
|
public function fooFoo(int $a): void {
|
2017-07-25 16:11:02 -04:00
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
}
|
|
|
|
}',
|
2018-07-13 20:17:29 -04:00
|
|
|
'error_message' => 'Method B::fooFoo has fewer parameters than parent method A::fooFoo',
|
2017-04-24 23:45:02 -04:00
|
|
|
],
|
2020-08-10 09:58:43 -04:00
|
|
|
'differentArgumentTypes' => [
|
2017-04-24 23:45:02 -04:00
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 15:50:45 -05:00
|
|
|
public function fooFoo(int $a, bool $b): void {
|
2017-07-25 16:11:02 -04:00
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
}
|
|
|
|
}
|
2017-07-25 16:11:02 -04:00
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
class B extends A {
|
2020-08-10 09:58:43 -04:00
|
|
|
public function fooFoo(int $a, int $b): void {
|
2017-07-25 16:11:02 -04:00
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
}
|
|
|
|
}',
|
2020-08-10 09:58:43 -04:00
|
|
|
'error_message' => 'Argument 2 of B::fooFoo has wrong type \'int\', expecting \'bool\' as defined ' .
|
2018-01-11 14:38:24 -08:00
|
|
|
'by A::fooFoo',
|
2017-05-26 20:05:57 -04:00
|
|
|
],
|
2020-08-10 09:58:43 -04: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 16:31:33 -05:00
|
|
|
'nonNullableSubclassParam' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 15:50:45 -05:00
|
|
|
public function foo(?string $s): string {
|
2017-11-13 16:31:33 -05:00
|
|
|
return $s ?: "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
2018-01-11 15:50:45 -05:00
|
|
|
public function foo(string $s): string {
|
2017-11-13 16:31:33 -05:00
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
}',
|
2019-10-16 22:14:33 -07:00
|
|
|
'error_message' => 'Argument 1 of B::foo has wrong type \'string\', expecting \'null|string\' as',
|
2017-11-13 16:31:33 -05:00
|
|
|
],
|
2017-12-30 10:54:01 -05:00
|
|
|
'misplacedRequiredParam' => [
|
|
|
|
'<?php
|
2019-06-07 09:25:03 -04:00
|
|
|
function foo(string $bar = null, int $bat): void {}
|
|
|
|
foo();',
|
|
|
|
'error_message' => 'TooFewArguments',
|
2017-12-30 10:54:01 -05:00
|
|
|
],
|
2018-01-04 14:01:17 -05:00
|
|
|
'clasginByRef' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 15:50:45 -05:00
|
|
|
public function foo(string $a): void {
|
2018-01-04 14:01:17 -05:00
|
|
|
echo $a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class B extends A {
|
2018-01-11 15:50:45 -05:00
|
|
|
public function foo(string &$a): void {
|
2018-01-04 14:01:17 -05:00
|
|
|
echo $a;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
|
|
|
],
|
2018-01-05 00:19:35 -05:00
|
|
|
'disallowSubclassesForNonInheritedMethodParams' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A {
|
2018-01-11 15:50:45 -05:00
|
|
|
public function bar(): void {}
|
2018-01-05 00:19:35 -05:00
|
|
|
}
|
|
|
|
class C extends A {
|
2018-01-11 15:50:45 -05:00
|
|
|
public function bar(): void {}
|
2018-01-05 00:19:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
class D {
|
2018-01-11 15:50:45 -05:00
|
|
|
public function foo(A $a): void {}
|
2018-01-05 00:19:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
class E extends D {
|
|
|
|
/** @param B|C $a */
|
2018-01-11 15:50:45 -05:00
|
|
|
public function foo(A $a): void {
|
2018-01-05 00:19:35 -05:00
|
|
|
$a->bar();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MoreSpecificImplementedParamType',
|
|
|
|
],
|
2020-11-12 13:54:27 -05:00
|
|
|
'preventVoidToNullConversionSignature' => [
|
2018-01-31 19:24:34 -05:00
|
|
|
'<?php
|
|
|
|
class A {
|
2020-11-12 13:54:27 -05:00
|
|
|
public function foo(): ?string {
|
|
|
|
return rand(0, 1) ? "hello" : null;
|
|
|
|
}
|
2018-01-31 19:24:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
2020-11-12 13:54:27 -05:00
|
|
|
public function foo(): void {
|
|
|
|
return;
|
|
|
|
}
|
2018-01-31 19:24:34 -05:00
|
|
|
}',
|
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
|
|
|
],
|
2018-03-13 12:52:00 -04: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 18:11:57 -04:00
|
|
|
'traitReturnTypeMismatch' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
trait T {
|
|
|
|
abstract public function foo() : string;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
use T;
|
|
|
|
}',
|
2020-01-16 22:36:30 -05:00
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
2019-01-13 13:27:07 -05: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 14:09:06 -04: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-16 22:36:30 -05:00
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
2019-03-29 14:09:06 -04:00
|
|
|
],
|
|
|
|
'abstractTraitMethodWithDifferentParamType' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
trait T {
|
|
|
|
abstract public function foo(A $a) : void;
|
|
|
|
}
|
|
|
|
|
|
|
|
class C {
|
|
|
|
use T;
|
|
|
|
|
2021-05-09 01:33:48 +02:00
|
|
|
public function foo(B $a) : void {}
|
2019-03-29 14:09:06 -04:00
|
|
|
}',
|
|
|
|
'error_message' => 'TraitMethodSignatureMismatch',
|
2018-03-13 18:11:57 -04:00
|
|
|
],
|
2018-04-21 21:59:16 +02:00
|
|
|
'mustOmitReturnType' => [
|
|
|
|
'<?php
|
|
|
|
class A
|
|
|
|
{
|
|
|
|
public function __construct(): void
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MethodSignatureMustOmitReturnType',
|
|
|
|
],
|
2018-07-13 20:17:29 -04:00
|
|
|
'requireParam' => [
|
|
|
|
'<?php
|
|
|
|
interface I {
|
|
|
|
function foo(bool $b = false): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
class C implements I {
|
|
|
|
public function foo(bool $b): void {}
|
|
|
|
}',
|
2019-02-27 16:00:44 -05:00
|
|
|
'error_message' => 'MethodSignatureMismatch - src' . DIRECTORY_SEPARATOR . 'somefile.php:6:27 - Method C::foo has more required',
|
2018-07-13 20:17:29 -04:00
|
|
|
],
|
2018-10-06 14:00:45 -04: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 14:27:54 -04:00
|
|
|
'error_message' => 'InvalidArgument',
|
2018-10-06 14:00:45 -04:00
|
|
|
],
|
2018-12-12 22:35:27 -05:00
|
|
|
'interfaceHasFewerConstructorArgs' => [
|
|
|
|
'<?php
|
|
|
|
interface Foo {
|
|
|
|
public function __construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
class Bar implements Foo {
|
|
|
|
public function __construct(bool $foo) {}
|
|
|
|
}',
|
2020-08-10 12:26:25 -04:00
|
|
|
'error_message' => 'ConstructorSignatureMismatch',
|
2018-12-12 22:35:27 -05:00
|
|
|
],
|
2019-01-19 12:19:07 -05: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-25 18:02:19 -04:00
|
|
|
'error_message' => 'ArgumentTypeCoercion',
|
2019-01-19 12:19:07 -05:00
|
|
|
],
|
2019-03-07 17:04:02 -06: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-25 18:02:19 -04:00
|
|
|
'error_message' => 'ArgumentTypeCoercion',
|
2019-03-07 17:04:02 -06:00
|
|
|
],
|
2019-01-19 12:19:07 -05: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 10:58:59 -05: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 11:55:59 -05: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 10:58:59 -05:00
|
|
|
],
|
2019-03-02 15:29:43 -05:00
|
|
|
'preventInterfaceOverload' => [
|
|
|
|
'<?php
|
|
|
|
interface I {
|
|
|
|
public function f(float ...$rest): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
class C implements I {
|
2021-05-09 01:33:48 +02:00
|
|
|
/**
|
|
|
|
* @param array<int,float> $f
|
|
|
|
* @psalm-suppress ParamNameMismatch
|
|
|
|
*/
|
2019-03-02 15:29:43 -05:00
|
|
|
public function f($f): void {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
2019-07-05 16:24:00 -04:00
|
|
|
['MoreSpecificImplementedParamType'],
|
2019-03-02 15:29:43 -05:00
|
|
|
],
|
2019-05-14 16:15:31 -04: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 15:44:46 -04:00
|
|
|
'preventImplementingSerializableWithType' => [
|
|
|
|
'<?php
|
|
|
|
class Foo implements \Serializable {
|
|
|
|
public function unserialize(string $serialized) {}
|
|
|
|
public function serialize() {}
|
|
|
|
}',
|
2020-12-08 11:24:15 -05:00
|
|
|
'error_message' => 'InvalidReturnType',
|
2019-05-14 15:44:46 -04:00
|
|
|
],
|
|
|
|
'preventImplementingSerializableWithWrongDocblockType' => [
|
|
|
|
'<?php
|
|
|
|
class Foo implements \Serializable {
|
|
|
|
/** @param int $serialized */
|
|
|
|
public function unserialize($serialized) {}
|
|
|
|
public function serialize() {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'ImplementedParamTypeMismatch',
|
|
|
|
],
|
2019-11-11 07:21:43 +02:00
|
|
|
'returnsParentWithNoParent' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
public function f(): parent {}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'InvalidParent',
|
|
|
|
],
|
|
|
|
'returnsParentWithNoParentAndInvalidParentSuppressed' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
public function f(): parent {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'InvalidReturnType',
|
2021-03-19 20:44:44 -05:00
|
|
|
['InvalidParent'],
|
2019-11-11 07:21:43 +02:00
|
|
|
],
|
|
|
|
// not sure how to handle it
|
|
|
|
'SKIPPED-returnsParentWithNoParentAndInvalidParentSuppressedMismatchingReturn' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
public function f(): parent {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'InvalidReturnType',
|
2021-03-19 20:44:44 -05:00
|
|
|
['InvalidParent'],
|
2019-11-11 07:21:43 +02:00
|
|
|
],
|
2020-01-16 22:36:30 -05: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 00:28:08 -05: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 11:46:51 -05: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 15:11:37 -04: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 12:26:25 -04:00
|
|
|
'inconsistentConstructorExplicitParentConstructorArgCount' => [
|
2020-08-05 19:39:27 -04: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 12:26:25 -04: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-05 19:39:27 -04: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 12:26:25 -04:00
|
|
|
'error_message' => 'ConstructorSignatureMismatch',
|
2020-08-05 19:39:27 -04:00
|
|
|
],
|
2020-09-07 16:42:25 -04:00
|
|
|
'inheritDocblockReturnFromInterface' => [
|
|
|
|
'<?php
|
|
|
|
interface A {
|
|
|
|
/** @return ?string */
|
|
|
|
function foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
class C implements A {
|
|
|
|
public function foo() : ?string {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidReturnType',
|
|
|
|
],
|
2021-04-15 13:55:13 +03:00
|
|
|
'disableNamedArgumentsInDescendant' => [
|
|
|
|
'<?php
|
|
|
|
interface Foo {
|
|
|
|
public function bar(string ...$_args): void;
|
|
|
|
}
|
|
|
|
final class Baz implements Foo {
|
|
|
|
/** @no-named-arguments */
|
|
|
|
public function bar(string ...$_args): void {}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
|
|
|
],
|
2017-04-24 23:45:02 -04:00
|
|
|
];
|
|
|
|
}
|
2016-12-14 19:24:33 -05:00
|
|
|
}
|