2017-02-08 04:02:46 +01:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2017-02-08 04:02:46 +01:00
|
|
|
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;
|
2021-12-03 20:29:06 +01:00
|
|
|
use Psalm\Exception\CodeException;
|
2021-12-04 03:37:19 +01:00
|
|
|
use Psalm\Exception\ConfigException;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
2020-07-11 23:17:22 +02:00
|
|
|
use Psalm\Internal\IncludeCollector;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Internal\Provider\Providers;
|
2021-12-04 21:55:53 +01:00
|
|
|
use Psalm\Tests\Internal\Provider\FakeParserCacheProvider;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
2020-01-12 17:33:38 +01:00
|
|
|
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
|
|
|
{
|
2021-12-04 21:55:53 +01:00
|
|
|
use 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');
|
2021-12-03 20:29:06 +01:00
|
|
|
$this->expectException(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) {
|
|
|
|
}
|
2022-12-18 17:15:15 +01: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
|
|
|
/**
|
2021-12-04 03:37:19 +01:00
|
|
|
* @throws 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>
|
2022-12-18 17:15:15 +01:00
|
|
|
</psalm>',
|
|
|
|
),
|
2020-01-12 17:33:38 +01:00
|
|
|
);
|
|
|
|
|
2023-10-09 22:40:21 +02:00
|
|
|
$file_path = (string) getcwd() . '/src/somefile.php';
|
2020-01-12 17:33:38 +01:00
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file_path,
|
|
|
|
'<?php
|
|
|
|
variadic2(16, 30);
|
2022-12-18 17:15:15 +01:00
|
|
|
',
|
2020-01-12 17:33:38 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, new Context());
|
|
|
|
}
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
2022-11-12 02:14:21 +01:00
|
|
|
* @return iterable<string,array{code: 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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @param mixed $req
|
|
|
|
* @param mixed $opt
|
|
|
|
* @param mixed ...$params
|
|
|
|
* @return array<mixed>
|
|
|
|
*/
|
|
|
|
function f($req, $opt = null, ...$params) {
|
|
|
|
return $params;
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2022-01-13 19:49:37 +01:00
|
|
|
f(1);
|
|
|
|
f(1, 2);
|
|
|
|
f(1, 2, 3);
|
|
|
|
f(1, 2, 3, 4);
|
|
|
|
f(1, 2, 3, 4, 5);',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-11-11 07:06:39 +01:00
|
|
|
'funcNumArgsVariadic' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
2019-04-09 19:58:49 +02:00
|
|
|
* @param int ...$a_list
|
2021-03-23 00:58:22 +01:00
|
|
|
* @return array<array-key, int>
|
2017-04-25 05:45:02 +02:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
private function getProjectAnalyzerWithConfig(Config $config): ProjectAnalyzer
|
2020-01-12 17:33:38 +01:00
|
|
|
{
|
2021-12-03 20:11:20 +01:00
|
|
|
$project_analyzer = new ProjectAnalyzer(
|
2020-01-12 17:33:38 +01:00
|
|
|
$config,
|
2021-12-03 20:11:20 +01:00
|
|
|
new Providers(
|
2020-01-12 17:33:38 +01:00
|
|
|
$this->file_provider,
|
2022-12-18 17:15:15 +01:00
|
|
|
new FakeParserCacheProvider(),
|
|
|
|
),
|
2020-01-12 17:33:38 +01:00
|
|
|
);
|
2021-11-27 01:06:33 +01:00
|
|
|
$project_analyzer->setPhpVersion('7.3', 'tests');
|
2020-01-12 17:33:38 +01:00
|
|
|
|
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
|
|
|
}
|