From 39a822759d0dc5b1a2600d1f9c27ef4f3077415d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A2=D0=B0=D0=BB=D1=8C=D0=B3=D0=B0=D1=82=20=D0=A5=D0=B0?= =?UTF-8?q?=D0=B9=D1=80=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sun, 12 Jan 2020 20:33:38 +0400 Subject: [PATCH] #2579 TooManyArguments was triggered if the variadic function declared at the autoloader file. Fixed. (#2580) --- .../Statements/Expression/CallAnalyzer.php | 9 ++- .../Internal/Analyzer/StatementsAnalyzer.php | 2 +- tests/VariadicTest.php | 57 +++++++++++++++++++ tests/fixtures/stubs/custom_functions.php | 5 ++ 4 files changed, 70 insertions(+), 3 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php index eae87fe4e..2b8d52721 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php @@ -1180,14 +1180,19 @@ class CallAnalyzer $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); } } diff --git a/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php b/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php index be045f898..78f38cf68 100644 --- a/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php @@ -2317,7 +2317,7 @@ class StatementsAnalyzer extends SourceAnalyzer implements StatementsSource $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; diff --git a/tests/VariadicTest.php b/tests/VariadicTest.php index 67a7b2917..306f098bd 100644 --- a/tests/VariadicTest.php +++ b/tests/VariadicTest.php @@ -1,7 +1,11 @@ analyzeFile('somefile.php', new Context()); } + /** + * @throws \Psalm\Exception\ConfigException + * @return void + */ + public function testVariadicFunctionFromAutoloadFile() + { + $this->project_analyzer = $this->getProjectAnalyzerWithConfig( + TestConfig::loadFromXML( + dirname(__DIR__), + ' + + + + + ' + ) + ); + + $file_path = getcwd() . '/src/somefile.php'; + + $this->addFile( + $file_path, + 'analyzeFile($file_path, new Context()); + } + /** * @return iterable,2?:string[]}> */ @@ -91,4 +127,25 @@ class VariadicTest extends TestCase ], ]; } + + /** + * @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; + } } diff --git a/tests/fixtures/stubs/custom_functions.php b/tests/fixtures/stubs/custom_functions.php index 32d21d535..b9a1c76c3 100644 --- a/tests/fixtures/stubs/custom_functions.php +++ b/tests/fixtures/stubs/custom_functions.php @@ -10,3 +10,8 @@ function barBar(string $a) : string function variadic() { } + +function variadic2() : array +{ + return func_get_args(); +}