2017-02-07 22:02:46 -05:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2020-01-12 20:33:38 +04:00
|
|
|
use Psalm\Config;
|
2017-02-07 22:02:46 -05:00
|
|
|
use Psalm\Context;
|
2020-07-12 00:17:22 +03:00
|
|
|
use Psalm\Internal\IncludeCollector;
|
2020-01-12 20:33:38 +04:00
|
|
|
use Psalm\Tests\Internal\Provider;
|
|
|
|
use function dirname;
|
|
|
|
use function getcwd;
|
2017-02-07 22:02:46 -05:00
|
|
|
|
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
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testVariadicArrayBadParam(): void
|
2017-02-07 22:02:46 -05:00
|
|
|
{
|
2019-05-16 18:36:36 -04:00
|
|
|
$this->expectExceptionMessage('InvalidScalarArgument');
|
|
|
|
$this->expectException(\Psalm\Exception\CodeException::class);
|
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
|
|
|
|
2020-01-12 20:33:38 +04:00
|
|
|
/**
|
|
|
|
* @throws \Psalm\Exception\ConfigException
|
2020-08-23 17:32:07 +03:00
|
|
|
* @runInSeparateProcess
|
2020-01-12 20:33:38 +04:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testVariadicFunctionFromAutoloadFile(): void
|
2020-01-12 20:33:38 +04:00
|
|
|
{
|
|
|
|
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
|
|
|
|
TestConfig::loadFromXML(
|
|
|
|
dirname(__DIR__),
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm
|
2021-02-14 00:42:34 +01:00
|
|
|
autoloader="tests/fixtures/stubs/custom_functions.phpstub"
|
2020-01-12 20:33:38 +04:00
|
|
|
>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
</psalm>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_path = getcwd() . '/src/somefile.php';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file_path,
|
|
|
|
'<?php
|
|
|
|
variadic2(16, 30);
|
|
|
|
'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, new Context());
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
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
|
|
|
];
|
|
|
|
}
|
2020-01-12 20:33:38 +04:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
private function getProjectAnalyzerWithConfig(Config $config): \Psalm\Internal\Analyzer\ProjectAnalyzer
|
2020-01-12 20:33:38 +04:00
|
|
|
{
|
|
|
|
$project_analyzer = new \Psalm\Internal\Analyzer\ProjectAnalyzer(
|
|
|
|
$config,
|
|
|
|
new \Psalm\Internal\Provider\Providers(
|
|
|
|
$this->file_provider,
|
|
|
|
new Provider\FakeParserCacheProvider()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$project_analyzer->setPhpVersion('7.3');
|
|
|
|
|
2020-07-12 00:17:22 +03:00
|
|
|
$config->setIncludeCollector(new IncludeCollector());
|
2020-01-12 20:33:38 +04:00
|
|
|
$config->visitComposerAutoloadFiles($project_analyzer, null);
|
|
|
|
|
|
|
|
return $project_analyzer;
|
|
|
|
}
|
2017-02-07 22:02:46 -05:00
|
|
|
}
|