1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-02 17:52:45 +01:00
psalm/examples/plugins/PreventFloatAssignmentChecker.php

46 lines
1.2 KiB
PHP
Raw Normal View History

<?php
2022-12-17 05:00:34 +01:00
namespace Psalm\Example\Plugin;
use PhpParser;
use Psalm\CodeLocation;
use Psalm\Issue\PluginIssue;
use Psalm\IssueBuffer;
use Psalm\Plugin\EventHandler\AfterExpressionAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AfterExpressionAnalysisEvent;
/**
* Prevents any assignment to a float value
*/
class PreventFloatAssignmentChecker implements AfterExpressionAnalysisInterface
{
/**
* Called after an expression has been checked
2022-12-17 15:13:32 +01:00
*
* @return null
*/
2022-12-18 17:15:15 +01:00
public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $event): ?bool
{
$expr = $event->getExpr();
$statements_source = $event->getStatementsSource();
if ($expr instanceof PhpParser\Node\Expr\Assign
2019-11-25 17:27:53 +01:00
&& ($expr_type = $statements_source->getNodeTypeProvider()->getType($expr->expr))
&& $expr_type->hasFloat()
) {
2022-12-17 05:00:34 +01:00
IssueBuffer::maybeAdd(
new NoFloatAssignment(
'Dont assign to floats',
2022-12-18 17:15:15 +01:00
new CodeLocation($statements_source, $expr),
),
2022-12-18 17:15:15 +01:00
$statements_source->getSuppressedIssues(),
2022-12-17 05:00:34 +01:00
);
}
return null;
}
}
2022-12-18 17:15:15 +01:00
class NoFloatAssignment extends PluginIssue
{
}