2016-12-15 01:24:33 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2018-01-21 16:22:04 +01: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
|
|
|
{
|
2017-11-09 03:27:23 +01:00
|
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
2017-04-25 05:45:02 +02:00
|
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
2017-02-09 23:49:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testExtendDocblockParamType()
|
|
|
|
{
|
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
|
|
|
|
* @param array<mixed> $input_headers default null
|
|
|
|
* @param array<mixed> $output_headers default null
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function __soapCall(
|
|
|
|
$function_name,
|
|
|
|
$arguments,
|
|
|
|
$options = [],
|
|
|
|
$input_headers = [],
|
|
|
|
&$output_headers = []
|
|
|
|
) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage MethodSignatureMismatch
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-02-09 23:49:13 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testExtendDocblockParamTypeWithWrongParam()
|
|
|
|
{
|
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
|
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function providerFileCheckerValidCodeParse()
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
public function unserialize($serialized) : void
|
|
|
|
{
|
|
|
|
[
|
|
|
|
$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
|
|
|
|
class Event {}
|
|
|
|
class AClass
|
|
|
|
{
|
|
|
|
public function get(): Event
|
|
|
|
{
|
|
|
|
return new Event;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2017-11-09 03:27:23 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function providerFileCheckerInvalidCodeParse()
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'Method B::fooFoo has more arguments 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
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'Method B::fooFoo has fewer arguments 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
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo($bar = null, $bat): void {}',
|
2017-12-30 16:54:01 +01:00
|
|
|
'error_message' => 'MisplacedRequiredParam',
|
|
|
|
],
|
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',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
|
|
|
}
|
2016-12-15 01:24:33 +01:00
|
|
|
}
|