Skip to content

Commit

Permalink
#2579 TooManyArguments was triggered if the variadic function declare…
Browse files Browse the repository at this point in the history
…d at the autoloader file. Fixed.
  • Loading branch information
incrize committed Jan 10, 2020
1 parent 03030d4 commit 1ba036d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
Expand Up @@ -1180,14 +1180,19 @@ protected static function checkFunctionLikeArgumentsMatch(
$codebase = $statements_analyzer->getCodebase();

if ($method_id) {
if ($in_call_map || !strpos($method_id, '::')) {
if (!$in_call_map && strpos($method_id, '::')) {
$fq_class_name = explode('::', $method_id)[0];
}

if ($function_storage) {
$is_variadic = $function_storage->variadic;
} elseif ($fq_class_name === null) {
$is_variadic = $codebase->functions->isVariadic(
$codebase,
strtolower($method_id),
$statements_analyzer->getRootFilePath()
);
} else {
$fq_class_name = explode('::', $method_id)[0];
$is_variadic = $codebase->methods->isVariadic($method_id);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Analyzer/StatementsAnalyzer.php
Expand Up @@ -2317,7 +2317,7 @@ public function getUncaughtThrows(Context $context)
$is_expected = false;

foreach ($ignored_exceptions_and_descendants as $expected_exception => $_) {
if ($expected_exception === $possibly_thrown_exception
if ($expected_exception === strtolower($possibly_thrown_exception)
|| $this->codebase->classExtends($possibly_thrown_exception, $expected_exception)
) {
$is_expected = true;
Expand Down
57 changes: 57 additions & 0 deletions tests/VariadicTest.php
@@ -1,7 +1,11 @@
<?php
namespace Psalm\Tests;

use Psalm\Config;
use Psalm\Context;
use Psalm\Tests\Internal\Provider;
use function dirname;
use function getcwd;

class VariadicTest extends TestCase
{
Expand Down Expand Up @@ -29,6 +33,38 @@ function f(int ...$a_list) {
$this->analyzeFile('somefile.php', new Context());
}

/**
* @throws \Psalm\Exception\ConfigException
* @return void
*/
public function testVariadicFunctionFromAutoloadFile()
{
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
TestConfig::loadFromXML(
dirname(__DIR__),
'<?xml version="1.0"?>
<psalm
autoloader="tests/fixtures/stubs/custom_functions.php"
>
<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());
}

/**
* @return iterable<string,array{string,1?:array<string,string>,2?:string[]}>
*/
Expand Down Expand Up @@ -91,4 +127,25 @@ function g(string ...$a_list) {
],
];
}

/**
* @param Config $config
*
* @return \Psalm\Internal\Analyzer\ProjectAnalyzer
*/
private function getProjectAnalyzerWithConfig(Config $config)
{
$project_analyzer = new \Psalm\Internal\Analyzer\ProjectAnalyzer(
$config,
new \Psalm\Internal\Provider\Providers(
$this->file_provider,
new Provider\FakeParserCacheProvider()
)
);
$project_analyzer->setPhpVersion('7.3');

$config->visitComposerAutoloadFiles($project_analyzer, null);

return $project_analyzer;
}
}
5 changes: 5 additions & 0 deletions tests/fixtures/stubs/custom_functions.php
Expand Up @@ -10,3 +10,8 @@ function barBar(string $a) : string
function variadic()
{
}

function variadic2() : array
{
return func_get_args();
}

0 comments on commit 1ba036d

Please sign in to comment.