2020-05-18 21:13:27 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
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;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Issue\InvalidArgument;
|
2020-05-18 21:13:27 +02:00
|
|
|
use Psalm\IssueBuffer;
|
|
|
|
use Psalm\Type;
|
|
|
|
|
2022-01-03 07:55:32 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-05-18 21:13:27 +02:00
|
|
|
class EmptyAnalyzer
|
|
|
|
{
|
|
|
|
public static function analyze(
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
PhpParser\Node\Expr\Empty_ $stmt,
|
|
|
|
Context $context
|
2021-12-05 18:51:26 +01:00
|
|
|
): void {
|
2020-05-18 21:13:27 +02:00
|
|
|
IssetAnalyzer::analyzeIssetVar($statements_analyzer, $stmt->expr, $context);
|
|
|
|
|
2021-09-02 20:39:03 +02:00
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
|
|
|
|
2021-09-02 20:32:05 +02:00
|
|
|
if (isset($codebase->config->forbidden_functions['empty'])) {
|
2021-11-29 20:54:17 +01:00
|
|
|
IssueBuffer::maybeAdd(
|
2021-09-02 20:32:05 +02:00
|
|
|
new ForbiddenCode(
|
|
|
|
'You have forbidden the use of empty',
|
2022-12-18 17:15:15 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
2021-09-02 20:32:05 +02:00
|
|
|
),
|
2022-12-18 17:15:15 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues(),
|
2021-11-29 20:54:17 +01:00
|
|
|
);
|
2021-09-02 20:32:05 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
) {
|
2021-11-29 20:54:17 +01:00
|
|
|
IssueBuffer::maybeAdd(
|
2021-12-03 20:11:20 +01:00
|
|
|
new InvalidArgument(
|
2020-05-18 21:13:27 +02:00
|
|
|
'Calling empty on a boolean value is almost certainly unintended',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->expr),
|
2022-12-18 17:15:15 +01:00
|
|
|
'empty',
|
2020-05-18 21:13:27 +02:00
|
|
|
),
|
2022-12-18 17:15:15 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues(),
|
2021-11-29 20:54:17 +01:00
|
|
|
);
|
2020-05-18 21:13:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getBool());
|
|
|
|
}
|
|
|
|
}
|