2016-12-15 01:24:33 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
|
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
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
$this->project_checker->checkClassReferences();
|
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
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
$this->project_checker->checkClassReferences();
|
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 {
|
|
|
|
private function foo() : void {}
|
|
|
|
}
|
|
|
|
class B extends A {
|
|
|
|
private function foo(int $arg) : void {}
|
|
|
|
}',
|
|
|
|
],
|
2017-11-13 22:31:33 +01:00
|
|
|
'nullableSubclassParam' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2017-11-29 05:09:09 +01:00
|
|
|
public function foo(string $s) : ?string {
|
|
|
|
return rand(0, 1) ? $s : null;
|
2017-11-13 22:31:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function foo(?string $s) : string {
|
|
|
|
return $s ?: "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo (new B)->foo(null);',
|
|
|
|
],
|
|
|
|
'nullableSubclassParamWithDefault' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function foo(string $s) : string {
|
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function foo(string $s = null) : string {
|
|
|
|
return $s ?: "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo (new B)->foo();',
|
|
|
|
],
|
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 {
|
|
|
|
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 {
|
|
|
|
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 {
|
|
|
|
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 {
|
|
|
|
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 {
|
|
|
|
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 {
|
|
|
|
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 ' .
|
2017-05-27 02:05:57 +02:00
|
|
|
'by A::foo',
|
|
|
|
],
|
2017-11-13 22:31:33 +01:00
|
|
|
'nonNullableSubclassParam' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function foo(?string $s) : string {
|
|
|
|
return $s ?: "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function foo(string $s) : string {
|
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'Argument 1 of B::foo has wrong type \'string\', expecting \'string|null\'',
|
|
|
|
],
|
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
|
|
|
|
function foo($bar = null, $bat) : void {}',
|
|
|
|
'error_message' => 'MisplacedRequiredParam',
|
|
|
|
],
|
2018-01-04 20:01:17 +01:00
|
|
|
'clasginByRef' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function foo(string $a) : void {
|
|
|
|
echo $a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class B extends A {
|
|
|
|
public function foo(string &$a) : void {
|
|
|
|
echo $a;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
|
|
|
}
|
2016-12-15 01:24:33 +01:00
|
|
|
}
|