2016-10-30 03:17:46 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class ClosureTest extends TestCase
|
2016-10-30 03:17:46 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
|
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
2017-01-13 18:40:01 +01:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2017-01-13 20:07:23 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerFileCheckerValidCodeParse()
|
2017-01-13 18:40:01 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'byRefUseVar' => [
|
|
|
|
'<?php
|
|
|
|
/** @return void */
|
|
|
|
function run_function(\Closure $fnc) {
|
|
|
|
$fnc();
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
// here we have to make sure $data exists as a side-effect of calling `run_function`
|
|
|
|
// because it could exist depending on how run_function is implemented
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
* @psalm-suppress MixedArgument
|
|
|
|
*/
|
|
|
|
function fn() {
|
|
|
|
run_function(
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function() use(&$data) {
|
|
|
|
$data = 1;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
echo $data;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
fn();',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'inferredArg' => [
|
|
|
|
'<?php
|
|
|
|
$bar = ["foo", "bar"];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$bam = array_map(
|
|
|
|
/**
|
|
|
|
* @psalm-suppress MissingClosureReturnType
|
|
|
|
*/
|
|
|
|
function(string $a) {
|
|
|
|
return $a . "blah";
|
|
|
|
},
|
|
|
|
$bar
|
2017-05-27 02:05:57 +02:00
|
|
|
);',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'varReturnType' => [
|
|
|
|
'<?php
|
|
|
|
$add_one = function(int $a) : int {
|
|
|
|
return $a + 1;
|
|
|
|
};
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = $add_one(1);',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$a' => 'int',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'callableToClosure' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return callable
|
|
|
|
*/
|
|
|
|
function foo() {
|
|
|
|
return function(string $a) : string {
|
|
|
|
return $a . "blah";
|
|
|
|
};
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'callable' => [
|
|
|
|
'<?php
|
|
|
|
function foo(callable $c) : void {
|
|
|
|
echo (string)$c();
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'callableClass' => [
|
|
|
|
'<?php
|
|
|
|
class C {
|
|
|
|
public function __invoke() : string {
|
|
|
|
return "You ran?";
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
function foo(callable $c) : void {
|
|
|
|
echo (string)$c();
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
foo(new C());
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$c2 = new C();
|
2017-05-27 02:05:57 +02:00
|
|
|
$c2();',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'correctParamType' => [
|
|
|
|
'<?php
|
|
|
|
$take_string = function(string $s) : string { return $s; };
|
2017-05-27 02:05:57 +02:00
|
|
|
$take_string("string");',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-01-13 18:40:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2017-01-13 18:40:01 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerFileCheckerInvalidCodeParse()
|
2017-01-13 18:40:01 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'wrongArg' => [
|
|
|
|
'<?php
|
|
|
|
$bar = ["foo", "bar"];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$bam = array_map(
|
|
|
|
function(int $a) : int {
|
|
|
|
return $a + 1;
|
|
|
|
},
|
|
|
|
$bar
|
|
|
|
);',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidScalarArgument',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'noReturn' => [
|
|
|
|
'<?php
|
|
|
|
$bar = ["foo", "bar"];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$bam = array_map(
|
|
|
|
function(string $a) : string {
|
|
|
|
},
|
|
|
|
$bar
|
|
|
|
);',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidReturnType',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'undefinedCallableClass' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function getFoo() : Foo
|
|
|
|
{
|
|
|
|
return new Foo([]);
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
public function bar($argOne, $argTwo)
|
|
|
|
{
|
|
|
|
$this->getFoo()($argOne, $argTwo);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidFunctionCall',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_levels' => ['UndefinedClass'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'possiblyNullFunctionCall' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @var Closure|null $foo
|
|
|
|
*/
|
|
|
|
$foo = null;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$foo = function ($bar) use (&$foo) : string
|
|
|
|
{
|
|
|
|
if (is_array($bar)) {
|
|
|
|
return $foo($bar);
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
return $bar;
|
|
|
|
};',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'PossiblyNullFunctionCall',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'stringFunctionCall' => [
|
|
|
|
'<?php
|
|
|
|
$bad_one = "hello";
|
|
|
|
$a = $bad_one(1);',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidFunctionCall',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'wrongParamType' => [
|
|
|
|
'<?php
|
|
|
|
$take_string = function(string $s) : string { return $s; };
|
|
|
|
$take_string(42);',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidScalarArgument',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-01-13 18:40:01 +01:00
|
|
|
}
|
2016-10-30 03:17:46 +01:00
|
|
|
}
|