1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-10 15:09:04 +01:00
psalm/src/Psalm/Internal/Analyzer/Statements/Expression/BooleanNotAnalyzer.php

50 lines
1.3 KiB
PHP
Raw Normal View History

2020-05-18 21:13:27 +02:00
<?php
2020-05-18 21:13:27 +02:00
namespace Psalm\Internal\Analyzer\Statements\Expression;
use PhpParser;
2021-06-08 04:55:21 +02:00
use Psalm\Context;
2020-05-18 21:13:27 +02:00
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Type;
2022-01-03 07:55:32 +01:00
/**
* @internal
*/
2020-05-18 21:13:27 +02:00
class BooleanNotAnalyzer
{
public static function analyze(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Expr\BooleanNot $stmt,
Context $context
): bool {
2021-09-22 16:49:16 +02:00
2020-05-18 21:13:27 +02:00
$inside_negation = $context->inside_negation;
$context->inside_negation = !$inside_negation;
$result = ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context);
$context->inside_negation = $inside_negation;
$expr_type = $statements_analyzer->node_data->getType($stmt->expr);
2021-09-22 16:51:53 +02:00
$stmt_type = Type::getBool();
if ($expr_type) {
2021-09-22 16:49:16 +02:00
if ($expr_type->isAlwaysTruthy()) {
$stmt_type = Type::getFalse();
} elseif ($expr_type->isAlwaysFalsy()) {
$stmt_type = Type::getTrue();
}
$stmt_type->from_docblock = $expr_type->from_docblock;
$stmt_type->parent_nodes = $expr_type->parent_nodes;
}
2021-09-22 16:51:53 +02:00
$statements_analyzer->node_data->setType($stmt, $stmt_type);
2020-05-18 21:13:27 +02:00
return $result;
}
}