2017-02-08 04:02:46 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
use Psalm\Context;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class VariadicTest extends TestCase
|
2017-02-08 04:02:46 +01:00
|
|
|
{
|
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @dataProvider providerTestValidVariadic
|
|
|
|
* @param string $code
|
2017-02-08 04:02:46 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function testVariadic($code)
|
2017-02-08 04:02:46 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
$stmts = self::$parser->parse($code);
|
2017-02-08 04:02:46 +01:00
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$context = new Context();
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InvalidScalarArgument
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testVariadicArrayBadParam()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
/**
|
|
|
|
* @param array<int, int> $a_list
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function f(int ...$a_list) {
|
|
|
|
}
|
|
|
|
f(1, 2, "3");
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$context = new Context();
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
}
|
2017-04-25 05:45:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function providerTestValidVariadic()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'variadic' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return array<mixed>
|
|
|
|
*/
|
|
|
|
function f($req, $opt = null, ...$params) {
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
|
|
|
f(1);
|
|
|
|
f(1, 2);
|
|
|
|
f(1, 2, 3);
|
|
|
|
f(1, 2, 3, 4);
|
2017-05-27 02:05:57 +02:00
|
|
|
f(1, 2, 3, 4, 5);',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'variadicArray' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<int, int> $a_list
|
|
|
|
* @return array<int, int>
|
|
|
|
*/
|
|
|
|
function f(int ...$a_list) {
|
|
|
|
return array_map(
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function (int $a) {
|
|
|
|
return $a + 1;
|
|
|
|
},
|
|
|
|
$a_list
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
f(1);
|
|
|
|
f(1, 2);
|
|
|
|
f(1, 2, 3);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string ...$a_list
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function g(string ...$a_list) {
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
|
|
|
}
|
2017-02-08 04:02:46 +01:00
|
|
|
}
|