diff --git a/src/Psalm/Context.php b/src/Psalm/Context.php index e4d57d3cd9f..e62a04fe1e8 100644 --- a/src/Psalm/Context.php +++ b/src/Psalm/Context.php @@ -107,6 +107,7 @@ class Context /** * @var string|null + * The name of the current class. Null if outside a class. */ public $self; diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentsAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentsAnalyzer.php index 7c2b3d0d3df..debc0eaa0ad 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentsAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentsAnalyzer.php @@ -868,6 +868,22 @@ public static function checkArgumentsMatch( return null; } + if ($method_id === 'get_class' && $args === []) { + //get_class without args only works when inside a class + if (!$context->self) { + IssueBuffer::maybeAdd( + new TooFewArguments( + 'Cannot call get_class() without argument outside of class scope', + $code_location, + $method_id + ), + $statements_analyzer->getSuppressedIssues() + ); + + return null; + } + } + self::checkArgCount( $statements_analyzer, $codebase, diff --git a/tests/FunctionCallTest.php b/tests/FunctionCallTest.php index 19b40baf1bb..4896559321f 100644 --- a/tests/FunctionCallTest.php +++ b/tests/FunctionCallTest.php @@ -2273,6 +2273,12 @@ function takesAString(string $s): void{ takesAString(false);', 'error_message' => 'InvalidArgument' ], + 'getClassWithoutArgsOutsideClass' => [ + ' 'TooFewArguments', + ], ]; }