Skip to content

Commit

Permalink
flag usage of get_class outside class without args (#7043)
Browse files Browse the repository at this point in the history
  • Loading branch information
orklah committed Dec 1, 2021
1 parent bc11b10 commit a7e9eea
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Psalm/Context.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit a7e9eea

Please sign in to comment.