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