2016-12-15 01:24:33 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2018-01-21 16:22:04 +01:00
|
|
|
use Psalm\Context;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function class_exists;
|
|
|
|
use const DIRECTORY_SEPARATOR;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerValidCodeParse()
|
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 {
|
|
|
|
/** @return self */
|
|
|
|
public function foo() {
|
|
|
|
return new A();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function foo() {
|
|
|
|
return new A();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-01-26 19:51:00 +01:00
|
|
|
'staticReturnShouldBeStatic' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @return static */
|
|
|
|
public static function foo() {
|
|
|
|
return new A();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public static function foo() {
|
|
|
|
return new B();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$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-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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
/**
|
|
|
|
* @return static
|
|
|
|
*/
|
|
|
|
public static function foo() {
|
|
|
|
return parent::foo();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
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
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerInvalidCodeParse()
|
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
|
|
|
],
|
|
|
|
'differentArguments' => [
|
|
|
|
'<?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(bool $b, int $a): void {
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'Argument 1 of B::fooFoo has wrong type \'bool\', expecting \'int\' as defined ' .
|
2018-01-11 23:38:24 +01:00
|
|
|
'by A::fooFoo',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
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;
|
|
|
|
}
|
|
|
|
}',
|
2018-01-11 23:38:24 +01:00
|
|
|
'error_message' => 'Argument 1 of B::foo has wrong type \'string\', expecting \'string|null\' as',
|
2017-11-13 22:31:33 +01:00
|
|
|
],
|
2017-11-26 22:03:17 +01:00
|
|
|
'mismatchingCovariantReturn' => [
|
|
|
|
'<?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 {}',
|
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
|
|
|
],
|
|
|
|
'mismatchingCovariantReturnWithSelf' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
function foo(): self {
|
|
|
|
return new A();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class B extends A {
|
|
|
|
function foo(): self {
|
|
|
|
return new B();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
|
|
|
],
|
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;
|
|
|
|
}',
|
2019-03-29 19:09:06 +01:00
|
|
|
'error_message' => 'TraitMethodSignatureMismatch',
|
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;
|
|
|
|
}',
|
|
|
|
'error_message' => 'TraitMethodSignatureMismatch',
|
|
|
|
],
|
|
|
|
'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) {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
|
|
|
],
|
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-04-09 19:58:49 +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',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
|
|
|
}
|
2016-12-15 01:24:33 +01:00
|
|
|
}
|