Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flag usage of get_class outside class without args #7043

Merged
merged 2 commits into from Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Psalm/Context.php
Expand Up @@ -107,6 +107,7 @@ class Context

/**
* @var string|null
* The name of the current class. Null if outside a class.
*/
public $self;

Expand Down
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions tests/FunctionCallTest.php
Expand Up @@ -2273,6 +2273,12 @@ function takesAString(string $s): void{
takesAString(false);',
'error_message' => 'InvalidArgument'
],
'getClassWithoutArgsOutsideClass' => [
'<?php

echo get_class();',
'error_message' => 'TooFewArguments',
],
];
}

Expand Down