2017-02-08 04:02:46 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2020-01-12 17:33:38 +01:00
|
|
|
use Psalm\Config;
|
2017-02-08 04:02:46 +01:00
|
|
|
use Psalm\Context;
|
2020-07-11 23:17:22 +02:00
|
|
|
use Psalm\Internal\IncludeCollector;
|
2020-01-12 17:33:38 +01:00
|
|
|
use Psalm\Tests\Internal\Provider;
|
|
|
|
use function dirname;
|
|
|
|
use function getcwd;
|
2017-02-08 04:02:46 +01:00
|
|
|
|
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
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testVariadicArrayBadParam(): void
|
2017-02-08 04:02:46 +01:00
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectExceptionMessage('InvalidScalarArgument');
|
|
|
|
$this->expectException(\Psalm\Exception\CodeException::class);
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/**
|
2019-04-09 19:58:49 +02:00
|
|
|
* @param int ...$a_list
|
2017-07-25 22:11:02 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function f(int ...$a_list) {
|
|
|
|
}
|
2019-04-09 19:58:49 +02:00
|
|
|
f(1, 2, "3");'
|
2017-07-25 22:11:02 +02:00
|
|
|
);
|
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
|
|
|
|
2020-01-12 17:33:38 +01:00
|
|
|
/**
|
|
|
|
* @throws \Psalm\Exception\ConfigException
|
2020-08-23 16:32:07 +02:00
|
|
|
* @runInSeparateProcess
|
2020-01-12 17:33:38 +01:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testVariadicFunctionFromAutoloadFile(): void
|
2020-01-12 17:33:38 +01: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 17:33:38 +01: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-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
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
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
|
2019-04-09 19:58:49 +02:00
|
|
|
* @param 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
|
|
|
|
/**
|
2019-04-09 19:58:49 +02:00
|
|
|
* @param int ...$a_list
|
2017-04-25 05:45:02 +02: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 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
|
|
|
];
|
|
|
|
}
|
2020-01-12 17:33:38 +01:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
private function getProjectAnalyzerWithConfig(Config $config): \Psalm\Internal\Analyzer\ProjectAnalyzer
|
2020-01-12 17:33:38 +01: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-11 23:17:22 +02:00
|
|
|
$config->setIncludeCollector(new IncludeCollector());
|
2020-01-12 17:33:38 +01:00
|
|
|
$config->visitComposerAutoloadFiles($project_analyzer, null);
|
|
|
|
|
|
|
|
return $project_analyzer;
|
|
|
|
}
|
2017-02-08 04:02:46 +01:00
|
|
|
}
|