1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00
psalm/tests/ClosureTest.php

186 lines
5.6 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests;
class ClosureTest extends TestCase
{
use Traits\FileCheckerInvalidCodeParseTestTrait;
use Traits\FileCheckerValidCodeParseTestTrait;
2017-01-13 20:07:23 +01:00
/**
* @return array
2017-01-13 20:07:23 +01:00
*/
public function providerFileCheckerValidCodeParse()
{
return [
'byRefUseVar' => [
'<?php
/** @return void */
function run_function(\Closure $fnc) {
$fnc();
}
2017-06-29 16:22:49 +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();',
],
'inferredArg' => [
'<?php
$bar = ["foo", "bar"];
2017-06-29 16:22:49 +02:00
$bam = array_map(
/**
* @psalm-suppress MissingClosureReturnType
*/
function(string $a) {
return $a . "blah";
},
$bar
2017-05-27 02:05:57 +02:00
);',
],
'varReturnType' => [
'<?php
$add_one = function(int $a) : int {
return $a + 1;
};
2017-06-29 16:22:49 +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
],
],
'callableToClosure' => [
'<?php
/**
* @return callable
*/
function foo() {
return function(string $a) : string {
return $a . "blah";
};
2017-05-27 02:05:57 +02:00
}',
],
'callable' => [
'<?php
function foo(callable $c) : void {
echo (string)$c();
2017-05-27 02:05:57 +02:00
}',
],
'callableClass' => [
'<?php
class C {
public function __invoke() : string {
return "You ran?";
}
}
2017-06-29 16:22:49 +02:00
function foo(callable $c) : void {
echo (string)$c();
}
2017-06-29 16:22:49 +02:00
foo(new C());
2017-06-29 16:22:49 +02:00
$c2 = new C();
2017-05-27 02:05:57 +02:00
$c2();',
],
'correctParamType' => [
'<?php
$take_string = function(string $s) : string { return $s; };
2017-05-27 02:05:57 +02:00
$take_string("string");',
],
];
}
/**
* @return array
*/
public function providerFileCheckerInvalidCodeParse()
{
return [
'wrongArg' => [
'<?php
$bar = ["foo", "bar"];
2017-06-29 16:22:49 +02:00
$bam = array_map(
function(int $a) : int {
return $a + 1;
},
$bar
);',
2017-05-27 02:05:57 +02:00
'error_message' => 'InvalidScalarArgument',
],
'noReturn' => [
'<?php
$bar = ["foo", "bar"];
2017-06-29 16:22:49 +02:00
$bam = array_map(
function(string $a) : string {
},
$bar
);',
2017-05-27 02:05:57 +02:00
'error_message' => 'InvalidReturnType',
],
'undefinedCallableClass' => [
'<?php
class A {
public function getFoo() : Foo
{
return new Foo([]);
}
2017-06-29 16:22:49 +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'],
],
'possiblyNullFunctionCall' => [
'<?php
/**
* @var Closure|null $foo
*/
$foo = null;
2017-06-29 16:22:49 +02:00
$foo = function ($bar) use (&$foo) : string
{
if (is_array($bar)) {
return $foo($bar);
}
2017-06-29 16:22:49 +02:00
return $bar;
};',
2017-05-27 02:05:57 +02:00
'error_message' => 'PossiblyNullFunctionCall',
],
'stringFunctionCall' => [
'<?php
$bad_one = "hello";
$a = $bad_one(1);',
2017-05-27 02:05:57 +02:00
'error_message' => 'InvalidFunctionCall',
],
'wrongParamType' => [
'<?php
$take_string = function(string $s) : string { return $s; };
$take_string(42);',
2017-05-27 02:05:57 +02:00
'error_message' => 'InvalidScalarArgument',
],
];
}
}