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

46 lines
1.3 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Example\Plugin;
use PhpParser;
use Psalm\Checker;
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
*
* @return null|false
*/
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()
) {
if (IssueBuffer::accepts(
new NoFloatAssignment(
'Dont assign to floats',
new CodeLocation($statements_source, $expr)
),
$statements_source->getSuppressedIssues()
)) {
// fall through
}
}
return null;
}
}
class NoFloatAssignment extends PluginIssue {
}