2017-02-08 04:02:46 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use Psalm\Context;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class VariadicTest extends TestCase
|
2017-02-08 04:02:46 +01:00
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
use Traits\ValidCodeAnalysisTestTrait;
|
2017-02-08 04:02:46 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InvalidScalarArgument
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-02-08 04:02:46 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testVariadicArrayBadParam()
|
|
|
|
{
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<int, int> $a_list
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function f(int ...$a_list) {
|
|
|
|
}
|
|
|
|
f(1, 2, "3");
|
|
|
|
'
|
|
|
|
);
|
2017-02-08 04:02:46 +01:00
|
|
|
|
2018-01-21 16:22:04 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2017-02-08 04:02:46 +01:00
|
|
|
}
|
2017-04-25 05:45:02 +02:00
|
|
|
|
|
|
|
/**
|
2019-03-01 21:55:20 +01:00
|
|
|
* @return iterable<string,array{string,1?:array<string,string>,2?:string[]}>
|
2017-04-25 05:45:02 +02:00
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerValidCodeParse()
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'variadic' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
2017-09-02 17:18:56 +02:00
|
|
|
* @param mixed $req
|
|
|
|
* @param mixed $opt
|
|
|
|
* @param array<int, mixed> $params
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array<mixed>
|
|
|
|
*/
|
|
|
|
function f($req, $opt = null, ...$params) {
|
|
|
|
return $params;
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
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
|
|
|
],
|
2017-11-11 07:06:39 +01:00
|
|
|
'funcNumArgsVariadic' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function test(): array {
|
2017-11-11 07:06:39 +01:00
|
|
|
return func_get_args();
|
|
|
|
}
|
|
|
|
var_export(test(2));',
|
|
|
|
],
|
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
|
|
|
|
);
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
f(1);
|
|
|
|
f(1, 2);
|
|
|
|
f(1, 2, 3);
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
}
|