2020-05-18 21:13:27 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Internal\Analyzer\Statements\Expression;
|
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
use Psalm\CodeLocation;
|
|
|
|
use Psalm\Context;
|
2021-06-08 04:55:21 +02:00
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
2021-09-02 20:37:21 +02:00
|
|
|
use Psalm\Issue\ForbiddenCode;
|
2020-05-18 21:13:27 +02:00
|
|
|
use Psalm\IssueBuffer;
|
|
|
|
use Psalm\Type;
|
|
|
|
|
|
|
|
class EmptyAnalyzer
|
|
|
|
{
|
|
|
|
public static function analyze(
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
PhpParser\Node\Expr\Empty_ $stmt,
|
|
|
|
Context $context
|
|
|
|
) : void {
|
|
|
|
IssetAnalyzer::analyzeIssetVar($statements_analyzer, $stmt->expr, $context);
|
|
|
|
|
2021-09-02 20:32:05 +02:00
|
|
|
if (isset($codebase->config->forbidden_functions['empty'])) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ForbiddenCode(
|
|
|
|
'You have forbidden the use of empty',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
if (($stmt_expr_type = $statements_analyzer->node_data->getType($stmt->expr))
|
|
|
|
&& $stmt_expr_type->hasBool()
|
|
|
|
&& $stmt_expr_type->isSingle()
|
|
|
|
&& !$stmt_expr_type->from_docblock
|
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new \Psalm\Issue\InvalidArgument(
|
|
|
|
'Calling empty on a boolean value is almost certainly unintended',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->expr),
|
|
|
|
'empty'
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getBool());
|
|
|
|
}
|
|
|
|
}
|